Software /
code /
prosody
Comparison
util/stanza.lua @ 262:8c73fb2ff4a2
A treat for Linux users ;)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 14 Nov 2008 16:03:33 +0000 |
parent | 251:5b6dec537914 |
child | 338:804f5e62a41f |
comparison
equal
deleted
inserted
replaced
261:790cf21e2af7 | 262:8c73fb2ff4a2 |
---|---|
8 local type = type; | 8 local type = type; |
9 local next = next; | 9 local next = next; |
10 local print = print; | 10 local print = print; |
11 local unpack = unpack; | 11 local unpack = unpack; |
12 local s_gsub = string.gsub; | 12 local s_gsub = string.gsub; |
13 | 13 local os = os; |
14 local debug = debug; | 14 |
15 local do_pretty_printing = not os.getenv("WINDIR"); | |
16 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; | |
17 | |
15 local log = require "util.logger".init("stanza"); | 18 local log = require "util.logger".init("stanza"); |
16 | 19 |
17 module "stanza" | 20 module "stanza" |
18 | 21 |
19 stanza_mt = {}; | 22 stanza_mt = {}; |
155 t_insert(tags, child); | 158 t_insert(tags, child); |
156 end | 159 end |
157 end | 160 end |
158 stanza.tags = tags; | 161 stanza.tags = tags; |
159 end | 162 end |
160 if not stanza.last_add then | |
161 stanza.last_add = {}; | |
162 end | |
163 end | 163 end |
164 | 164 |
165 return stanza; | 165 return stanza; |
166 end | 166 end |
167 | 167 |
193 | 193 |
194 function presence(attr) | 194 function presence(attr) |
195 return stanza("presence", attr); | 195 return stanza("presence", attr); |
196 end | 196 end |
197 | 197 |
198 if do_pretty_printing then | |
199 local style_attrk = getstyle("yellow"); | |
200 local style_attrv = getstyle("red"); | |
201 local style_tagname = getstyle("red"); | |
202 local style_punc = getstyle("magenta"); | |
203 | |
204 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); | |
205 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); | |
206 --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); | |
207 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); | |
208 function stanza_mt.pretty_print(t) | |
209 local children_text = ""; | |
210 for n, child in ipairs(t) do | |
211 if type(child) == "string" then | |
212 children_text = children_text .. xml_escape(child); | |
213 else | |
214 children_text = children_text .. child:pretty_print(); | |
215 end | |
216 end | |
217 | |
218 local attr_string = ""; | |
219 if t.attr then | |
220 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end | |
221 end | |
222 return s_format(tag_format, t.name, attr_string, children_text, t.name); | |
223 end | |
224 | |
225 function stanza_mt.pretty_top_tag(t) | |
226 local attr_string = ""; | |
227 if t.attr then | |
228 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end | |
229 end | |
230 return s_format(top_tag_format, t.name, attr_string); | |
231 end | |
232 else | |
233 -- Sorry, fresh out of colours for you guys ;) | |
234 stanza_mt.pretty_print = stanza_mt.__tostring; | |
235 stanza_mt.pretty_top_tag = stanza_mt.top_tag; | |
236 end | |
237 | |
198 return _M; | 238 return _M; |