Software /
code /
prosody
Comparison
util/template.lua @ 4495:c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 05 Feb 2012 00:06:20 +0500 |
parent | 3640:4bc88bb748d1 |
child | 5214:1430c6f36621 |
comparison
equal
deleted
inserted
replaced
4494:694491140a67 | 4495:c0f5c78cb817 |
---|---|
5 local pairs = pairs; | 5 local pairs = pairs; |
6 local ipairs = ipairs; | 6 local ipairs = ipairs; |
7 local error = error; | 7 local error = error; |
8 local loadstring = loadstring; | 8 local loadstring = loadstring; |
9 local debug = debug; | 9 local debug = debug; |
10 local t_remove = table.remove; | |
10 | 11 |
11 module("template") | 12 module("template") |
12 | 13 |
13 local parse_xml = (function() | 14 local parse_xml = (function() |
14 local ns_prefixes = { | 15 local ns_prefixes = { |
40 end | 41 end |
41 end | 42 end |
42 stanza:tag(name, attr); | 43 stanza:tag(name, attr); |
43 end | 44 end |
44 function handler:CharacterData(data) | 45 function handler:CharacterData(data) |
45 data = data:gsub("^%s*", ""):gsub("%s*$", ""); | |
46 stanza:text(data); | 46 stanza:text(data); |
47 end | 47 end |
48 function handler:EndElement(tagname) | 48 function handler:EndElement(tagname) |
49 stanza:up(); | 49 stanza:up(); |
50 end | 50 end |
57 else | 57 else |
58 return ok, err.." (line "..line..", col "..col..")"; | 58 return ok, err.." (line "..line..", col "..col..")"; |
59 end | 59 end |
60 end; | 60 end; |
61 end)(); | 61 end)(); |
62 | |
63 local function trim_xml(stanza) | |
64 for i=#stanza,1,-1 do | |
65 local child = stanza[i]; | |
66 if child.name then | |
67 trim_xml(child); | |
68 else | |
69 child = child:gsub("^%s*", ""):gsub("%s*$", ""); | |
70 stanza[i] = child; | |
71 if child == "" then t_remove(stanza, i); end | |
72 end | |
73 end | |
74 end | |
62 | 75 |
63 local function create_string_string(str) | 76 local function create_string_string(str) |
64 str = ("%q"):format(str); | 77 str = ("%q"):format(str); |
65 str = str:gsub("{([^}]*)}", function(s) | 78 str = str:gsub("{([^}]*)}", function(s) |
66 return '"..(data["'..s..'"]or"").."'; | 79 return '"..(data["'..s..'"]or"").."'; |
116 | 129 |
117 local template_mt = { __tostring = function(t) return t.name end }; | 130 local template_mt = { __tostring = function(t) return t.name end }; |
118 local function create_template(templates, text) | 131 local function create_template(templates, text) |
119 local stanza, err = parse_xml(text); | 132 local stanza, err = parse_xml(text); |
120 if not stanza then error(err); end | 133 if not stanza then error(err); end |
134 trim_xml(stanza); | |
121 | 135 |
122 local info = debug.getinfo(3, "Sl"); | 136 local info = debug.getinfo(3, "Sl"); |
123 info = info and ("template(%s:%d)"):format(info.short_src:match("[^\\/]*$"), info.currentline) or "template(unknown)"; | 137 info = info and ("template(%s:%d)"):format(info.short_src:match("[^\\/]*$"), info.currentline) or "template(unknown)"; |
124 | 138 |
125 local template = setmetatable({ apply = create_cloner(stanza, info), name = info, text = text }, template_mt); | 139 local template = setmetatable({ apply = create_cloner(stanza, info), name = info, text = text }, template_mt); |