Comparison

mod_http_muc_log/mod_http_muc_log.lua @ 1625:c427de617ada

mod_http_muc_log: Alter template language to more simply allow optional bits in templates
author Kim Alvefur <zash@zash.se>
date Wed, 11 Mar 2015 13:29:23 +0100
parent 1606:2c8b985ebde5
child 1654:1a6d6221c5f6
comparison
equal deleted inserted replaced
1624:0f20390f6fa5 1625:c427de617ada
32 end 32 end
33 33
34 module:depends"http"; 34 module:depends"http";
35 35
36 local function render(template, values) 36 local function render(template, values)
37 --[[ DOC 37 -- This function takes a string template and a table of values.
38 {name} is substituted for values["name"] and is XML escaped 38 -- Sequences like {name} in the template string are substituted
39 {name!} is substituted without XML escaping 39 -- with values from the table, optionally depending on a modifier
40 {name?} is optional and is replaced with an empty string if no value exists 40 -- symbol.
41 {name# sub-template } renders a sub-template using an array of values 41 --
42 ]] 42 -- Variants are:
43 -- {name} is substituted for values["name"] and is XML escaped
44 -- {name? sub-template } renders a sub-template if values["name"] is false-ish
45 -- {name& sub-template } renders a sub-template if values["name"] is true-ish
46 -- {name# sub-template } renders a sub-template using an array of values
47 -- {name!} is substituted *without* XML escaping
43 return (template:gsub("%b{}", function (block) 48 return (template:gsub("%b{}", function (block)
44 local name, opt, e = block:sub(2, -2):match("([%a_][%w_]*)(%p?)()"); 49 local name, opt, e = block:sub(2, -2):match("^([%a_][%w_]*)(%p?)()");
45 local value = values[name]; 50 local value = values[name];
46 if opt == '#' then 51 if opt == '#' then
47 if not value or not value[1] then return ""; end 52 if not value or not value[1] then return ""; end
48 local out, subtpl = {}, block:sub(e+1, -2); 53 local out, subtpl = {}, block:sub(e+1, -2);
49 for i=1, #value do 54 for i=1, #value do
50 out[i] = render(subtpl, value[i]); 55 out[i] = render(subtpl, value[i]);
51 end 56 end
52 return t_concat(out); 57 return t_concat(out);
53 end 58 elseif opt == '&' then
54 if value ~= nil then 59 if not value then return ""; end
60 return render(block:sub(e+1, -2), values);
61 elseif opt == '?' and not value then
62 return render(block:sub(e+1, -2), values);
63 elseif value ~= nil then
55 if type(value) ~= "string" then 64 if type(value) ~= "string" then
56 value = tostring(value); 65 value = tostring(value);
57 end 66 end
58 if opt ~= '!' then 67 if opt ~= '!' then
59 return xml_escape(value); 68 return xml_escape(value);
60 end 69 end
61 return value; 70 return value;
62 elseif opt == '?' then
63 return block:sub(e+1, -2);
64 end 71 end
65 end)); 72 end));
66 end 73 end
67 74
68 local template = "Could not load template" 75 local template = "Could not load template"
160 if i > 1 and tmp.wday == 2 then 167 if i > 1 and tmp.wday == 2 then
161 days = {}; 168 days = {};
162 weeks[#weeks+1] = { days = days }; 169 weeks[#weeks+1] = { days = days };
163 n = 1; 170 n = 1;
164 end 171 end
165 if d[i] then 172 days[n], n = { wday = tmp.wday, day = i, href = d[i] and datetime.date(d[i]) }, n+1;
166 days[n], n = { wday = tmp.wday, links = {{ href = datetime.date(d[i]), day = i }} }, n+1;
167 else
168 days[n], n = { wday = tmp.wday, plain = i }, n+1;
169 end
170 end 173 end
171 end 174 end
172 table.sort(year, sort_m); 175 table.sort(year, sort_m);
173 end 176 end
174 table.sort(years, sort_Y); 177 table.sort(years, sort_Y);