From b8d1d533fdf9b9e0d348a4f8d4204e6a1f87a5bd Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 27 Oct 2017 13:27:35 +0200 Subject: mod_http_errors: Use util.interpolation to render HTML template --- plugins/mod_http_errors.lua | 61 +++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/plugins/mod_http_errors.lua b/plugins/mod_http_errors.lua index d8646268..13473219 100644 --- a/plugins/mod_http_errors.lua +++ b/plugins/mod_http_errors.lua @@ -2,6 +2,8 @@ module:set_global(); 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 @@ local html = [[ - - + +{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 -- cgit v1.2.3