Comparison

mod_http_muc_log/mod_http_muc_log.lua @ 1750:3b839db88412

mod_http_muc_log: Template engine thing moved into util.interpolation
author Kim Alvefur <zash@zash.se>
date Tue, 19 May 2015 16:13:00 +0200
parent 1671:c813b69ae279
child 1832:48125f2c358b
comparison
equal deleted inserted replaced
1749:39a0a35f02bc 1750:3b839db88412
1 local st = require "util.stanza";
2 local mt = require"util.multitable"; 1 local mt = require"util.multitable";
3 local datetime = require"util.datetime"; 2 local datetime = require"util.datetime";
4 local jid_split = require"util.jid".split; 3 local jid_split = require"util.jid".split;
5 local nodeprep = require"util.encodings".stringprep.nodeprep; 4 local nodeprep = require"util.encodings".stringprep.nodeprep;
6 local uuid = require"util.uuid".generate; 5 local uuid = require"util.uuid".generate;
7 local it = require"util.iterators"; 6 local it = require"util.iterators";
8 local gettime = require"socket".gettime; 7 local gettime = require"socket".gettime;
9 local url = require"socket.url"; 8 local url = require"socket.url";
10 local xml_escape = st.xml_escape;
11 local t_concat = table.concat;
12 local os_time, os_date = os.time, os.date; 9 local os_time, os_date = os.time, os.date;
10 local render = require"util.interpolation".new("%b{}", require"util.stanza".xml_escape);
13 11
14 local archive = module:open_store("muc_log", "archive"); 12 local archive = module:open_store("muc_log", "archive");
15 13
16 -- Support both old and new MUC code 14 -- Support both old and new MUC code
17 local mod_muc = module:depends"muc"; 15 local mod_muc = module:depends"muc";
30 local jid = name .. '@' .. module.host; 28 local jid = name .. '@' .. module.host;
31 return get_room_from_jid(jid); 29 return get_room_from_jid(jid);
32 end 30 end
33 31
34 module:depends"http"; 32 module:depends"http";
35
36 local function render(template, values)
37 -- This function takes a string template and a table of values.
38 -- Sequences like {name} in the template string are substituted
39 -- with values from the table, optionally depending on a modifier
40 -- symbol.
41 --
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
48 return (template:gsub("%b{}", function (block)
49 local name, opt, e = block:sub(2, -2):match("^([%a_][%w_]*)(%p?)()");
50 local value = values[name];
51 if opt == '#' then
52 if not value or not value[1] then return ""; end
53 local out, subtpl = {}, block:sub(e+1, -2);
54 for i=1, #value do
55 out[i] = render(subtpl, value[i]);
56 end
57 return t_concat(out);
58 elseif opt == '&' 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
64 if type(value) ~= "string" then
65 value = tostring(value);
66 end
67 if opt ~= '!' then
68 return xml_escape(value);
69 end
70 return value;
71 end
72 end));
73 end
74 33
75 local template; 34 local template;
76 do 35 do
77 local template_file = module:get_option_string(module.name .. "_template", module.name .. ".html"); 36 local template_file = module:get_option_string(module.name .. "_template", module.name .. ".html");
78 template_file = assert(module:load_resource(template_file)); 37 template_file = assert(module:load_resource(template_file));