# HG changeset patch # User Kim Alvefur # Date 1432044780 -7200 # Node ID 3b839db88412f4ee16c92f0ea7dc97fdc6d8521a # Parent 39a0a35f02bcfdabb665c4b832457025c6c309e8 mod_http_muc_log: Template engine thing moved into util.interpolation diff -r 39a0a35f02bc -r 3b839db88412 mod_http_muc_log/mod_http_muc_log.lua --- a/mod_http_muc_log/mod_http_muc_log.lua Mon May 18 22:41:19 2015 +0200 +++ b/mod_http_muc_log/mod_http_muc_log.lua Tue May 19 16:13:00 2015 +0200 @@ -1,4 +1,3 @@ -local st = require "util.stanza"; local mt = require"util.multitable"; local datetime = require"util.datetime"; local jid_split = require"util.jid".split; @@ -7,9 +6,8 @@ local it = require"util.iterators"; local gettime = require"socket".gettime; local url = require"socket.url"; -local xml_escape = st.xml_escape; -local t_concat = table.concat; local os_time, os_date = os.time, os.date; +local render = require"util.interpolation".new("%b{}", require"util.stanza".xml_escape); local archive = module:open_store("muc_log", "archive"); @@ -33,45 +31,6 @@ module:depends"http"; -local function render(template, values) - -- This function takes a string template and a table of values. - -- Sequences like {name} in the template string are substituted - -- with values from the table, optionally depending on a modifier - -- symbol. - -- - -- Variants are: - -- {name} is substituted for values["name"] and is XML escaped - -- {name? sub-template } renders a sub-template if values["name"] is false-ish - -- {name& sub-template } renders a sub-template if values["name"] is true-ish - -- {name# sub-template } renders a sub-template using an array of values - -- {name!} is substituted *without* XML escaping - return (template:gsub("%b{}", function (block) - local name, opt, e = block:sub(2, -2):match("^([%a_][%w_]*)(%p?)()"); - local value = values[name]; - if opt == '#' then - if not value or not value[1] then return ""; end - local out, subtpl = {}, block:sub(e+1, -2); - for i=1, #value do - out[i] = render(subtpl, value[i]); - end - return t_concat(out); - elseif opt == '&' then - if not value then return ""; end - return render(block:sub(e+1, -2), values); - elseif opt == '?' and not value then - return render(block:sub(e+1, -2), values); - elseif value ~= nil then - if type(value) ~= "string" then - value = tostring(value); - end - if opt ~= '!' then - return xml_escape(value); - end - return value; - end - end)); -end - local template; do local template_file = module:get_option_string(module.name .. "_template", module.name .. ".html");