Comparison

util/interpolation.lua @ 10348:3852fc91b2fc 0.11

util.interpolation: Support unescaped variables with more modifiers (fixes #1452) Tests will be added in trunk.
author Kim Alvefur <zash@zash.se>
date Sun, 20 Oct 2019 20:52:14 +0200
parent 6772:805baeca56b6
child 11064:af1e3b7d9ea3
child 11306:5798ab735619
comparison
equal deleted inserted replaced
10341:4e406944ff85 10348:3852fc91b2fc
30 local function render(template, values) 30 local function render(template, values)
31 -- assert(type(template) == "string", "bad argument #1 to 'render' (string expected)"); 31 -- assert(type(template) == "string", "bad argument #1 to 'render' (string expected)");
32 -- assert(type(values) == "table", "bad argument #2 to 'render' (table expected)"); 32 -- assert(type(values) == "table", "bad argument #2 to 'render' (table expected)");
33 return (s_gsub(template, pat, function (block) 33 return (s_gsub(template, pat, function (block)
34 block = s_sub(block, 2, -2); 34 block = s_sub(block, 2, -2);
35 local name, opt, e = s_match(block, "^([%a_][%w_.]*)(%p?)()"); 35 local name, raw, opt, e = s_match(block, "^([%a_][%w_.]*)(!?)(%p?)()");
36 if not name then return end 36 if not name then return end
37 local value = values[name]; 37 local value = values[name];
38 if not value and name:find(".", 2, true) then 38 if not value and name:find(".", 2, true) then
39 value = values; 39 value = values;
40 for word in name:gmatch"[^.]+" do 40 for word in name:gmatch"[^.]+" do
43 end 43 end
44 end 44 end
45 if funcs then 45 if funcs then
46 while value ~= nil and opt == '|' do 46 while value ~= nil and opt == '|' do
47 local f; 47 local f;
48 f, opt, e = s_match(block, "^([%a_][%w_.]*)(%p?)()", e); 48 f, raw, opt, e = s_match(block, "^([%a_][%w_.]*)(!?)(%p?)()", e);
49 f = funcs[f]; 49 f = funcs[f];
50 if f then value = f(value); end 50 if f then value = f(value); end
51 end 51 end
52 end 52 end
53 if opt == '#' or opt == '%' then 53 if opt == '#' or opt == '%' then
68 return render(s_sub(block, e), values); 68 return render(s_sub(block, e), values);
69 elseif value ~= nil then 69 elseif value ~= nil then
70 if type(value) ~= "string" then 70 if type(value) ~= "string" then
71 value = tostring(value); 71 value = tostring(value);
72 end 72 end
73 if opt ~= '!' then 73 if raw ~= '!' then
74 return escape(value); 74 return escape(value);
75 end 75 end
76 return value; 76 return value;
77 end 77 end
78 end)); 78 end));