# HG changeset patch # User Kim Alvefur # Date 1417529521 -3600 # Node ID 9f6cd252d2338272c061a542e45226e98d35cf2c # Parent 63571115302f82adf53ee2e8a1f3c0523f73bc52 mod_http_muc_log: Revamp template system diff -r 63571115302f -r 9f6cd252d233 mod_http_muc_log/mod_http_muc_log.lua --- a/mod_http_muc_log/mod_http_muc_log.lua Tue Dec 02 14:17:08 2014 +0100 +++ b/mod_http_muc_log/mod_http_muc_log.lua Tue Dec 02 15:12:01 2014 +0100 @@ -5,6 +5,9 @@ local uuid = require"util.uuid".generate; 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 archive = module:open_store("muc_log", "archive"); @@ -28,128 +31,114 @@ module:depends"http"; -local function template(data) +local function render(template, values) --[[ DOC - Like util.template, but deals with plain text - Returns a closure that is called with a table of values {name} is substituted for values["name"] and is XML escaped {name!} is substituted without XML escaping {name?} is optional and is replaced with an empty string if no value exists + {name# sub-template } renders a sub-template using an array of values ]] - return function(values) - return (data:gsub("{([^}]-)(%p?)}", function (name, opt) - local value = values[name]; - if value then - if opt ~= "!" then - return st.xml_escape(value); - end - return value; - elseif opt == "?" then - return ""; + 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 - end)); - end + return t_concat(out); + end + if value ~= nil then + if type(value) ~= "string" then + value = tostring(value); + end + if opt ~= '!' then + return xml_escape(value); + end + return value; + elseif opt == '?' then + return block:sub(e+1, -2); + end + end)); end --- TODO Move templates into files -local base = template(template[[ +-- TODO Move template into a file +local template = [=[ - {title}
-

{title}

-{header!} +

{title}

+

-{body!} + +
    {lines# +
  1. + +{nick} +{verb?} +{body?} +
  2. } +

- - -]] { prosody_version = prosody.version }); - -local dates_template = template(base{ - title = "Logs for room {room}"; - header = [[ - -]]; - body = [[ -