summaryrefslogtreecommitdiff
path: root/lualibs-util-tpl.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2015-05-04 23:10:29 +0200
committerPhilipp Gesang <phg@phi-gamma.net>2015-05-04 23:11:06 +0200
commita79c845dba381f543d09526f77dcf2b4687b73a7 (patch)
tree8c42403a55c9c6736eb491331bfa7f650f807414 /lualibs-util-tpl.lua
parent9c35a373aa8e0edf14b686568fffab0e6969f447 (diff)
downloadlualibs-a79c845dba381f543d09526f77dcf2b4687b73a7.tar.gz
sync with Context as of 2015-05-04
Diffstat (limited to 'lualibs-util-tpl.lua')
-rw-r--r--lualibs-util-tpl.lua21
1 files changed, 17 insertions, 4 deletions
diff --git a/lualibs-util-tpl.lua b/lualibs-util-tpl.lua
index bd0e261..468dd42 100644
--- a/lualibs-util-tpl.lua
+++ b/lualibs-util-tpl.lua
@@ -128,6 +128,11 @@ local function replacekeyquoted(s,t,how,recurse) -- ".. \" "
end
end
+local function replaceoptional(l,m,r,t,how,recurse)
+ local v = t[l]
+ return v and v ~= "" and lpegmatch(replacer,r,1,t,how or "lua",recurse or false) or ""
+end
+
local single = P("%") -- test %test% test : resolves test
local double = P("%%") -- test 10%% test : %% becomes %
local lquoted = P("%[") -- test '%[test]%' test : resolves to test with escaped "'s
@@ -143,12 +148,19 @@ local norquoted = rquoted / ''
local nolquotedq = lquotedq / ''
local norquotedq = rquotedq / ''
-local key = nosingle * ((C((1-nosingle )^1) * Carg(1) * Carg(2) * Carg(3)) / replacekey ) * nosingle
-local quoted = nolquotedq * ((C((1-norquotedq)^1) * Carg(1) * Carg(2) * Carg(3)) / replacekeyquoted ) * norquotedq
-local unquoted = nolquoted * ((C((1-norquoted )^1) * Carg(1) * Carg(2) * Carg(3)) / replacekeyunquoted) * norquoted
+local noloptional = P("%?") / ''
+local noroptional = P("?%") / ''
+local nomoptional = P(":") / ''
+
+
+local args = Carg(1) * Carg(2) * Carg(3)
+local key = nosingle * ((C((1-nosingle )^1) * args) / replacekey ) * nosingle
+local quoted = nolquotedq * ((C((1-norquotedq )^1) * args) / replacekeyquoted ) * norquotedq
+local unquoted = nolquoted * ((C((1-norquoted )^1) * args) / replacekeyunquoted) * norquoted
+local optional = noloptional * ((C((1-nomoptional)^1) * nomoptional * C((1-noroptional)^1) * args) / replaceoptional) * noroptional
local any = P(1)
- replacer = Cs((unquoted + quoted + escape + key + any)^0)
+ replacer = Cs((unquoted + quoted + escape + optional + key + any)^0)
local function replace(str,mapping,how,recurse)
if mapping and str then
@@ -164,6 +176,7 @@ end
-- print(replace("test '%[x]%' test",{ x = [[a '%y%'  a]], y = "oeps" },'sql',true))
-- print(replace([[test %[x]% test]],{ x = [[a "x"  a]]}))
-- print(replace([[test %(x)% test]],{ x = [[a "x"  a]]}))
+-- print(replace([[convert %?x: -x "%x%" ?% %?y: -y "%y%" ?%]],{ x = "yes" }))
templates.replace = replace