1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
if not modules then modules = { } end modules ['java-ini'] = {
version = 1.001,
comment = "companion to java-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local format = string.format
local concat = table.concat
local lpegmatch, lpegP, lpegR, lpegS, lpegC = lpeg.match, lpeg.P, lpeg.R, lpeg.S, lpeg.C
local allocate = utilities.storage.allocate
-- todo: don't flush scripts if no JS key
interactions.javascripts = interactions.javascripts or { }
local javascripts = interactions.javascripts
javascripts.codes = allocate()
javascripts.preambles = allocate()
javascripts.functions = allocate()
local codes, preambles, functions = javascripts.codes, javascripts.preambles, javascripts.functions
local preambled = { }
local function storefunction(s)
functions[s] = true
end
local uses = lpegP("uses")
local used = lpegP("used")
local left = lpegP("{")
local right = lpegP("}")
local space = lpegS(" \r\n")
local spaces = space^0
local braced = left * lpegC((1-right-space)^1) * right
local unbraced = lpegC((1-space)^1)
local name = spaces * (braced + unbraced) * spaces
local any = lpegP(1)
local script = lpegC(any^1)
local funct = lpegP("function")
local leftp = lpegP("(")
local rightp = lpegP(")")
local fname = spaces * funct * spaces * (((1-space-left)^1)/storefunction) * spaces * leftp
local parsecode = name * ((uses * name) + lpeg.Cc("")) * spaces * script
local parsepreamble = name * ((used * name) + lpeg.Cc("")) * spaces * script
local parsefunctions = (fname + any)^0
function javascripts.storecode(str)
local name, uses, script = lpegmatch(parsecode,str)
if name and name ~= "" then
codes[name] = { uses, script }
end
end
function javascripts.storepreamble(str) -- now later
local name, used, script = lpegmatch(parsepreamble,str)
if name and name ~= "" then
preambles[#preambles+1] = { name, used, script }
preambled[name] = #preambles
lpegmatch(parsefunctions,script)
end
end
function javascripts.setpreamble(name,script) -- now later
if name and name ~= "" then
preambles[#preambles+1] = { name, "now", script }
preambled[name] = #preambles
lpegmatch(parsefunctions,script)
end
end
function javascripts.addtopreamble(name,script) -- now later
if name and name ~= "" then
local p = preambled[name]
if p then
preambles[p] = { "now", preambles[p] .. " ;\n" .. script }
else
preambles[#preambles+1] = { name, "now", script }
preambled[name] = #preambles
lpegmatch(parsefunctions,script)
end
end
end
function javascripts.usepreamblenow(name) -- now later
if name and name ~= "" and preambled[name] then
preambles[preambled[name]][2] = "now"
end
end
local splitter = lpeg.Ct(lpeg.splitat(lpeg.patterns.commaspacer))
local used = false
function javascripts.code(name,arguments)
local c = codes[name]
if c then
local u, code = c[1], c[2]
if u ~= "" then
local p = preambled[u]
if p then
preambles[p][1] = "now"
end
end
used = true
return code
end
local f = functions[name]
if f then
used = true
if arguments then
local args = lpegmatch(splitter,arguments)
for i=1,#args do -- can be a helper
args[i] = format("%q",args[i])
end
return format("%s(%s)",name,concat(args,","))
else
return format("%s()",name)
end
end
end
function javascripts.flushpreambles()
local t = { }
if used then
for i=1,#preambles do
local preamble = preambles[i]
if preamble[2] == "now" then
t[#t+1] = { preamble[1], preamble[3] }
end
end
end
return t
end
|