aboutsummaryrefslogtreecommitdiffstats
path: root/util/xtemplate.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util/xtemplate.lua')
-rw-r--r--util/xtemplate.lua26
1 files changed, 15 insertions, 11 deletions
diff --git a/util/xtemplate.lua b/util/xtemplate.lua
index 88baf1f7..446d7d1f 100644
--- a/util/xtemplate.lua
+++ b/util/xtemplate.lua
@@ -3,13 +3,21 @@ local s_match = string.match;
local s_sub = string.sub;
local t_concat = table.concat;
-local st = require("util.stanza");
+local st = require("prosody.util.stanza");
local function render(template, root, escape, filters)
escape = escape or st.xml_escape;
- return (s_gsub(template, "%b{}", function(block)
+ return (s_gsub(template, "(%s*)(%b{})(%s*)", function(pre_blank, block, post_blank)
local inner = s_sub(block, 2, -2);
+ if inner:sub(1, 1) == "-" then
+ pre_blank = "";
+ inner = inner:sub(2);
+ end
+ if inner:sub(-1, -1) == "-" then
+ post_blank = "";
+ inner = inner:sub(1, -2);
+ end
local path, pipe, pos = s_match(inner, "^([^|]+)(|?)()");
if not (type(path) == "string") then return end
local value
@@ -32,7 +40,7 @@ local function render(template, root, escape, filters)
if args then args = s_sub(args, 2, -2); end
if func == "each" and tmpl then
- if not st.is_stanza(value) then return "" end
+ if not st.is_stanza(value) then return pre_blank .. post_blank end
if not args then value, args = root, path; end
local ns, name = s_match(args, "^(%b{})(.*)$");
if ns then
@@ -62,11 +70,7 @@ local function render(template, root, escape, filters)
end
elseif filters and filters[func] then
local f = filters[func];
- if args == nil then
- value, is_escaped = f(value, tmpl);
- else
- value, is_escaped = f(args, value, tmpl);
- end
+ value, is_escaped = f(value, args, tmpl);
else
error("No such filter function: " .. func);
end
@@ -75,12 +79,12 @@ local function render(template, root, escape, filters)
if type(value) == "string" then
if not is_escaped then value = escape(value); end
- return value
+ return pre_blank .. value .. post_blank
elseif st.is_stanza(value) then
value = value:get_text();
- if value then return escape(value) end
+ if value then return pre_blank .. escape(value) .. post_blank end
end
- return ""
+ return pre_blank .. post_blank
end))
end