Comparison

util/stanza.lua @ 3477:6350b114e0e4

util.stanza: Add stanza:maptags() to apply a function over child tags (return nil to remove tag from stanza)
author Matthew Wild <mwild1@gmail.com>
date Mon, 30 Aug 2010 04:53:41 +0100
parent 3475:0307a3ac3885
child 3478:4621c92d2368
comparison
equal deleted inserted replaced
3476:193bb0936a4e 3477:6350b114e0e4
149 local v = self.tags[i] 149 local v = self.tags[i]
150 if v then return v; end 150 if v then return v; end
151 end, self.tags[1], i; 151 end, self.tags[1], i;
152 end 152 end
153 153
154 function stanza_mt:maptags(callback)
155 local tags, curr_tag = self.tags, 1;
156 local n_children, n_tags = #self, #tags;
157
158 local i = 1;
159 while curr_tag <= n_tags do
160 if self[i] == tags[curr_tag] then
161 local ret = callback(self[i]);
162 if ret == nil then
163 t_remove(self, i);
164 t_remove(tags, curr_tag);
165 n_children = n_children - 1;
166 n_tags = n_tags - 1;
167 else
168 self[i] = ret;
169 tags[i] = ret;
170 end
171 i = i + 1;
172 curr_tag = curr_tag + 1;
173 end
174 end
175 return self;
176 end
177
154 local xml_escape 178 local xml_escape
155 do 179 do
156 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" }; 180 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
157 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end 181 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
158 _M.xml_escape = xml_escape; 182 _M.xml_escape = xml_escape;