Software /
code /
prosody
Annotate
util/template.lua @ 3610:2084959d4096
configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 10 Nov 2010 19:50:07 +0000 |
parent | 3546:cb1600dea3ad |
child | 3637:bd491def3efb |
rev | line source |
---|---|
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 local t_insert = table.insert; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 local st = require "util.stanza"; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 local lxp = require "lxp"; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 local setmetatable = setmetatable; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 local pairs = pairs; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 local error = error; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local s_gsub = string.gsub; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 local print = print; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 module("template") |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 local function process_stanza(stanza, ops) |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 -- process attrs |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 for key, val in pairs(stanza.attr) do |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 if val:match("{[^}]*}") then |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 t_insert(ops, {stanza.attr, key, val}); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 -- process children |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local i = 1; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 while i <= #stanza do |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local child = stanza[i]; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 if child.name then |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 process_stanza(child, ops); |
3546
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
27 elseif child:match("^{[^}]*}$") then -- text |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
28 t_insert(ops, {stanza, i, child:match("^{([^}]*)}$"), true}); |
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 elseif child:match("{[^}]*}") then -- text |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 t_insert(ops, {stanza, i, child}); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 i = i + 1; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 local parse_xml = (function() |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 local ns_prefixes = { |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 ["http://www.w3.org/XML/1998/namespace"] = "xml"; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 }; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local ns_separator = "\1"; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$"; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 return function(xml) |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 local handler = {}; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 local stanza = st.stanza("root"); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 function handler:StartElement(tagname, attr) |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 local curr_ns,name = tagname:match(ns_pattern); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 if name == "" then |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 curr_ns, name = "", curr_ns; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 if curr_ns ~= "" then |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 attr.xmlns = curr_ns; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 for i=1,#attr do |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 local k = attr[i]; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 attr[i] = nil; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 local ns, nm = k:match(ns_pattern); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 if nm ~= "" then |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 ns = ns_prefixes[ns]; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 if ns then |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 attr[ns..":"..nm] = attr[k]; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 attr[k] = nil; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 stanza:tag(name, attr); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 function handler:CharacterData(data) |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 data = data:gsub("^%s*", ""):gsub("%s*$", ""); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 stanza:text(data); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 function handler:EndElement(tagname) |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 stanza:up(); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 local parser = lxp.new(handler, "\1"); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 local ok, err, line, col = parser:parse(xml); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 if ok then ok, err, line, col = parser:parse(); end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 --parser:close(); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 if ok then |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 return stanza.tags[1]; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 else |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 return ok, err.." (line "..line..", col "..col..")"; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 end; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 end)(); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 |
3546
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
86 local stanza_mt = st.stanza_mt; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
87 local function fast_st_clone(stanza, lookup) |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
88 local stanza_attr = stanza.attr; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
89 local stanza_tags = stanza.tags; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
90 local tags, attr = {}, {}; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
91 local clone = { name = stanza.name, attr = attr, tags = tags, last_add = {} }; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
92 for k,v in pairs(stanza_attr) do attr[k] = v; end |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
93 lookup[stanza_attr] = attr; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
94 for i=1,#stanza_tags do |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
95 local child = stanza_tags[i]; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
96 local new = fast_st_clone(child, lookup); |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
97 tags[i] = new; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
98 lookup[child] = new; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
99 end |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
100 for i=1,#stanza do |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
101 local child = stanza[i]; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
102 clone[i] = lookup[child] or child; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
103 end |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
104 return setmetatable(clone, stanza_mt); |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
105 end |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
106 |
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 local function create_template(text) |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 local stanza, err = parse_xml(text); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 if not stanza then error(err); end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 local ops = {}; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 process_stanza(stanza, ops); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 local template = {}; |
3546
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
114 local lookup = {}; |
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 function template.apply(data) |
3546
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
116 local newstanza = fast_st_clone(stanza, lookup); |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
117 for i=1,#ops do |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
118 local op = ops[i]; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
119 local t, k, v, g = op[1], op[2], op[3], op[4]; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
120 if g then |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
121 lookup[t][k] = data[v]; |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
122 else |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
123 lookup[t][k] = s_gsub(v, "{([^}]*)}", data); |
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
124 end |
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 end |
3546
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
126 return newstanza; |
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 return template; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 local templates = setmetatable({}, { __mode = 'k' }); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 return function(text) |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 local template = templates[text]; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 if not template then |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 template = create_template(text); |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 templates[text] = template; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 end |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 return template; |
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 end; |