summaryrefslogtreecommitdiff
path: root/scripts/context/lua/mtxrun.lua
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/context/lua/mtxrun.lua')
-rw-r--r--scripts/context/lua/mtxrun.lua26
1 files changed, 21 insertions, 5 deletions
diff --git a/scripts/context/lua/mtxrun.lua b/scripts/context/lua/mtxrun.lua
index 40fe71c0e..a299e3a2b 100644
--- a/scripts/context/lua/mtxrun.lua
+++ b/scripts/context/lua/mtxrun.lua
@@ -1312,6 +1312,10 @@ patterns.undouble = (dquote/"") * patterns.nodquote * (dquote/"")
patterns.unquoted = patterns.undouble + patterns.unsingle -- more often undouble
patterns.unspacer = ((patterns.spacer^1)/"")^0
+patterns.singlequoted = squote * patterns.nosquote * squote
+patterns.doublequoted = dquote * patterns.nodquote * dquote
+patterns.quoted = patterns.doublequoted + patterns.singlequoted
+
patterns.somecontent = (anything - newline - space)^1 -- (utf8char - newline - space)^1
patterns.beginline = #(1-newline)
@@ -16439,6 +16443,7 @@ local templates = utilities.templates
local trace_template = false trackers.register("templates.trace",function(v) trace_template = v end)
local report_template = logs.reporter("template")
+local format = string.format
local P, C, Cs, Carg, lpegmatch = lpeg.P, lpeg.C, lpeg.Cs, lpeg.Carg, lpeg.match
local replacer
@@ -16462,13 +16467,22 @@ end
----- leftmarker = P("<!-- ") / ""
----- rightmarker = P(" --!>") / ""
-local escape = P("%%") / "%%"
-local leftmarker = P("%") / ""
-local rightmarker = P("%") / ""
+local single = P("%") -- test %test% test : resolves test
+local double = P("%%") -- test 10%% test : %% becomes %
+local lquoted = P("%[") -- test %[test]" test : resolves test with escaped "'s
+local rquoted = P("]%") --
+
+local escape = double / "%%"
+local nosingle = single / ""
+local nodouble = double / ""
+local nolquoted = lquoted / ""
+local norquoted = rquoted / ""
-local key = leftmarker * (C((1-rightmarker)^1 * Carg(1))/replacekey) * rightmarker
+local key = nosingle * (C((1-nosingle)^1 * Carg(1))/replacekey) * nosingle
+local unquoted = nolquoted * ((C((1 - norquoted)^1) * Carg(1))/function(s,t) return format("%q",replacekey(s,t)) end) * norquoted
local any = P(1)
- replacer = Cs((escape + key + any)^0)
+
+ replacer = Cs((unquoted + escape + key + any)^0)
local function replace(str,mapping)
if mapping then
@@ -16478,6 +16492,8 @@ local function replace(str,mapping)
end
end
+-- print(replace("test %[x]% test",{ x = [[a "x" a]] }))
+
templates.replace = replace
function templates.load(filename,mapping)