# HG changeset patch # User Kim Alvefur # Date 1509103655 -7200 # Node ID f91ab40a31054e463b8616fad94e58fe7e7ce7c7 # Parent e2460edc2a2feefb18ff4e8969dc9442cca04946 mod_http_errors: Use util.interpolation to render HTML template diff -r e2460edc2a2f -r f91ab40a3105 plugins/mod_http_errors.lua --- a/plugins/mod_http_errors.lua Fri Oct 27 13:28:34 2017 +0200 +++ b/plugins/mod_http_errors.lua Fri Oct 27 13:27:35 2017 +0200 @@ -2,6 +2,8 @@ local server = require "net.http.server"; local codes = require "net.http.codes"; +local xml_escape = require "util.stanza".xml_escape; +local render = require "util.interpolation".new("%b{}", xml_escape); local show_private = module:get_option_boolean("http_errors_detailed", false); local always_serve = module:get_option_boolean("http_errors_always_show", true); @@ -21,53 +23,46 @@ - - + +{title} + -

$title

-

$message

-

$extra

+

{title}

+

{message}

+

{extra?}

]]; -html = html:gsub("%s%s+", ""); - -local entities = { - ["<"] = "<", [">"] = ">", ["&"] = "&", - ["'"] = "'", ["\""] = """, ["\n"] = "
", -}; - -local function tohtml(plain) - return (plain:gsub("[<>&'\"\n]", entities)); - -end local function get_page(code, extra) local message = messages[code]; if always_serve or message then message = message or default_message; - return (html:gsub("$(%a+)", { + return render(html, { title = rawget(codes, code) or ("Code "..tostring(code)); message = message[1]:gsub("%%", function () return message[math.random(2, math.max(#message,2))]; end); - extra = tohtml(extra or ""); - })); + extra = extra; + }); end end