diff options
author | Taco Hoekwater <taco@elvenkind.com> | 2011-06-01 08:54:21 +0000 |
---|---|---|
committer | Taco Hoekwater <taco@elvenkind.com> | 2011-06-01 08:54:21 +0000 |
commit | d7ccb42582f85acf30568913610ccf4d602023fb (patch) | |
tree | 7292e3545a420676878e7451b68892d360c62cb6 /Master/texmf-dist/tex/context/base/java-ini.lua | |
parent | 2d62a6fe9b80def59c392268022f1f9a2d6e358f (diff) |
commit context 2011.05.18
git-svn-id: svn://tug.org/texlive/trunk@22719 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/base/java-ini.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/java-ini.lua | 168 |
1 files changed, 126 insertions, 42 deletions
diff --git a/Master/texmf-dist/tex/context/base/java-ini.lua b/Master/texmf-dist/tex/context/base/java-ini.lua index f64a8a18fb0..b3b06667815 100644 --- a/Master/texmf-dist/tex/context/base/java-ini.lua +++ b/Master/texmf-dist/tex/context/base/java-ini.lua @@ -7,36 +7,49 @@ if not modules then modules = { } end modules ['java-ini'] = { } local format = string.format -local lpegmatch = lpeg.match +local concat = table.concat +local lpegmatch, lpegP, lpegR, lpegS, lpegC, lpegCarg = lpeg.match, lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Carg -javascripts = javascripts or { } -javascripts.codes = javascripts.codes or { } -javascripts.preambles = javascripts.preambles or { } -javascripts.functions = javascripts.functions or { } +local allocate = utilities.storage.allocate +local variables = interfaces.variables + +-- todo: don't flush scripts if no JS key + +local trace_javascript = false trackers.register("backends.javascript", function(v) trace_javascript = v end) + +local report_javascripts = logs.reporter ("interactions","javascripts") +local status_javascripts = logs.messenger("interactions","javascripts") + +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 +local function storefunction(s,preamble) + functions[s] = preamble end -local uses = lpeg.P("uses") -local used = lpeg.P("used") -local left = lpeg.P("{") -local right = lpeg.P("}") -local space = lpeg.S(" \r\n") +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 * lpeg.C((1-right-space)^1) * right -local unbraced = lpeg.C((1-space)^1) +local braced = left * lpegC((1-right-space)^1) * right +local unbraced = lpegC((1-space)^1) local name = spaces * (braced + unbraced) * spaces -local any = lpeg.P(1) -local script = lpeg.C(any^1) -local funct = lpeg.P("function") -local leftp = lpeg.P("(") -local rightp = lpeg.P(")") -local fname = spaces * funct * spaces * (((1-space-left)^1)/storefunction) * spaces * leftp +local any = lpegP(1) +local script = lpegC(any^1) +local funct = lpegP("function") +local leftp = lpegP("(") +local rightp = lpegP(")") +local fname = spaces * funct * spaces * (lpegC((1-space-left-leftp)^1) * lpegCarg(1) / storefunction) * spaces * leftp local parsecode = name * ((uses * name) + lpeg.Cc("")) * spaces * script local parsepreamble = name * ((used * name) + lpeg.Cc("")) * spaces * script @@ -45,46 +58,74 @@ local parsefunctions = (fname + any)^0 function javascripts.storecode(str) local name, uses, script = lpegmatch(parsecode,str) if name and name ~= "" then - javascripts.codes[name] = { uses, script } + 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) + if name and name ~= "" and not preambled[name] then + local n = #preambles + 1 + preambles[n] = { name, used, script } + preambled[name] = n + if trace_javascript then + report_javascripts("stored: preamble '%s', state '%s', order '%s'",name,used,n) + end + lpegmatch(parsefunctions,script,1,n) 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) + if name and name ~= "" and not preambled[name] then + local n = #preambles + 1 + preambles[n] = { name, "now", script } + preambled[name] = n + if trace_javascript then + report_javascripts("adapted: preamble '%s', state '%s', order '%s'",name,"now",n) + end + lpegmatch(parsefunctions,script,1,n) end end -function javascripts.addtopreamble(name,script) -- now later +function javascripts.addtopreamble(name,script) if name and name ~= "" then local p = preambled[name] if p then preambles[p] = { "now", preambles[p] .. " ;\n" .. script } + if trace_javascript then + report_javascripts("extended: preamble '%s', state '%s', order '%s'",name,"now",p) + end else - preambles[#preambles+1] = { name, "now", script } - preambled[name] = #preambles - lpegmatch(parsefunctions,script) + local n = #preambles + 1 + preambles[n] = { name, "now", script } + preambled[name] = n + if trace_javascript then + report_javascripts("stored: preamble '%s', state '%s', order '%s'",name,"now",n) + end + lpegmatch(parsefunctions,script,1,n) end end end function javascripts.usepreamblenow(name) -- now later - if name and name ~= "" and preambled[name] then - preambles[preambled[name]][2] = "now" + if name and name ~= "" and name ~= variables.reset then -- todo: reset + local names = utilities.parsers.settings_to_array(name) + for i=1,#names do + local somename = names[i] + if not preambled[somename] then + preambles[preambled[somename]][2] = "now" + if trace_javascript then + report_javascripts("used: preamble '%s', state '%s', order '%s'",somename,"now","auto") + end + end + end end end +local splitter = lpeg.Ct(lpeg.splitat(lpeg.patterns.commaspacer)) + +local used, reported = false, { } -- we can cache more + function javascripts.code(name,arguments) local c = codes[name] if c then @@ -92,16 +133,36 @@ function javascripts.code(name,arguments) if u ~= "" then local p = preambled[u] if p then - preambles[p][1] = "now" + preambles[p][2] = "now" + if trace_javascript and not reported[name] then + reported[name] = true + report_javascripts("used: code '%s', preamble '%s'",name,u) + end + elseif trace_javascript and not reported[name] then + reported[name] = true + report_javascripts("used: code '%s'",name) end + elseif trace_javascript and not reported[name] then + reported[name] = true + report_javascripts("used: code '%s'",name) end + used = true return code end local f = functions[name] if f then - -- temporary hack, i need a more clever approach + used = true + if trace_javascript and not reported[name] then + reported[name] = true + report_javascripts("used: function '%s'",name) + end + preambles[f][2] = "now" -- automatically tag preambles that define the function (as later) if arguments then - return format("%s(%s)",name,'"' .. arguments.gsub(arguments,'%s*,%s*','"%1",') .. '"') + 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 @@ -110,11 +171,34 @@ end function javascripts.flushpreambles() local t = { } - for i=1,#preambles do - local preamble = preambles[i] - if preamble[2] == "now" then - t[#t+1] = { preamble[1], preamble[3] } + if used then + for i=1,#preambles do + local preamble = preambles[i] + if preamble[2] == "now" then + if trace_javascript then + report_javascripts("flushed: preamble '%s'",preamble[1]) + end + t[#t+1] = { preamble[1], preamble[3] } + end end end return t end + +local patterns = { "java-imp-%s.mkiv", "java-imp-%s.tex", "java-%s.mkiv", "java-%s.tex" } + +function javascripts.usescripts(name) + -- this will become pure lua, no context + if name ~= variables.reset then -- reset is obsolete + commands.uselibrary(name,patterns,function(name,foundname) + context.startnointerference() + context.startreadingfile() + context.input(foundname) + status_javascripts("loaded: library '%s'",name) + context.stopreadingfile() + context.stopnointerference() + end, function(name) + report_javascripts("unknown: library '%s'",name) + end) + end +end |