# HG changeset patch # User Kim Alvefur # Date 1441318251 -7200 # Node ID 1b5c817cb642f921f7b2904dddbfe2f1e4268353 # Parent 8435e1766054eb790190d30df53126702ab83b05 mod_http_index: Update to use util.interpolation (makes it depend on 0.10+) diff -r 8435e1766054 -r 1b5c817cb642 mod_http_index/mod_http_index.lua --- a/mod_http_index/mod_http_index.lua Thu Sep 03 03:22:31 2015 +0200 +++ b/mod_http_index/mod_http_index.lua Fri Sep 04 00:10:51 2015 +0200 @@ -1,35 +1,10 @@ -local st = require "util.stanza"; local url = require"socket.url"; +local render = require"util.interpolation".new("%b{}", require"util.stanza".xml_escape); module:depends"http"; --- local dump = require"util.serialization".new"dump".serialize; - -local function template(data) - --[[ 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 - ]] - 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 ""; - end - end)); - end -end - -- TODO Move templates into files -local base = template(template[[ +local base_template = [[ @@ -71,39 +46,25 @@

{title}

-{header!}

-{body!} +

-]] { prosody_version = prosody.version, mod_name = module.name }); +]]; local canonical = module:http_url(nil, "/"); -local page_template = template(base{ - canonical = canonical; - title = "HTTP stuff"; - header = ""; - body = [[ - -]]; - footer = ""; -}); -local line_template = template[[ -
  • {name}
  • -]]; local function relative(base, link) base = url.parse(base); @@ -117,24 +78,25 @@ end local function handler(event) - local items = module:get_host_items("http-provider"); - local item; - for i = 1, #items do - item = items[i]; + local host_items = module:get_host_items("http-provider"); + local http_apps = {} + for _, item in ipairs(host_items) do if module.name ~= item._provided_by then - items[i] = line_template{ + table.insert(http_apps, { name = item.name; module = "mod_" .. item._provided_by; url = relative(canonical, module:http_url(item.name, item.default_path)); - }; - else - items[i] = ""; + }); end end event.response.headers.content_type = "text/html"; - return page_template{ - lines = table.concat(items); - }; + return render(base_template, { + title = "HTTP Apps"; + items = http_apps; + prosody_version = prosody.version; + mod_name = module.name; + canonical = canonical; + }); end module:provides("http", {