diff options
author | Waqas Hussain <waqas20@gmail.com> | 2012-02-05 00:06:20 +0500 |
---|---|---|
committer | Waqas Hussain <waqas20@gmail.com> | 2012-02-05 00:06:20 +0500 |
commit | 45bce504d83275ed3b91f1523582f936bac34010 (patch) | |
tree | 9181ec30b770c8c225b057a9fd13466ef793b698 /util/template.lua | |
parent | 3c8c0f2e94c21f8d330120ec43e51acce73c88ba (diff) | |
download | prosody-45bce504d83275ed3b91f1523582f936bac34010.tar.gz prosody-45bce504d83275ed3b91f1523582f936bac34010.zip |
util.template: Refactoring to make the string->stanza conversion code more generic.
Diffstat (limited to 'util/template.lua')
-rw-r--r-- | util/template.lua | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/util/template.lua b/util/template.lua index ebd8be14..5e9b479e 100644 --- a/util/template.lua +++ b/util/template.lua @@ -7,6 +7,7 @@ local ipairs = ipairs; local error = error; local loadstring = loadstring; local debug = debug; +local t_remove = table.remove; module("template") @@ -42,7 +43,6 @@ local parse_xml = (function() stanza:tag(name, attr); end function handler:CharacterData(data) - data = data:gsub("^%s*", ""):gsub("%s*$", ""); stanza:text(data); end function handler:EndElement(tagname) @@ -60,6 +60,19 @@ local parse_xml = (function() end; end)(); +local function trim_xml(stanza) + for i=#stanza,1,-1 do + local child = stanza[i]; + if child.name then + trim_xml(child); + else + child = child:gsub("^%s*", ""):gsub("%s*$", ""); + stanza[i] = child; + if child == "" then t_remove(stanza, i); end + end + end +end + local function create_string_string(str) str = ("%q"):format(str); str = str:gsub("{([^}]*)}", function(s) @@ -118,6 +131,7 @@ local template_mt = { __tostring = function(t) return t.name end }; local function create_template(templates, text) local stanza, err = parse_xml(text); if not stanza then error(err); end + trim_xml(stanza); local info = debug.getinfo(3, "Sl"); info = info and ("template(%s:%d)"):format(info.short_src:match("[^\\/]*$"), info.currentline) or "template(unknown)"; |