util/stanza.lua

changeset 282
80e7de32b618
parent 262
8c73fb2ff4a2
child 338
804f5e62a41f
equal deleted inserted replaced
280:516f4c901991 282:80e7de32b618
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 = {};
101 local attr_string = ""; 104 local attr_string = "";
102 if t.attr then 105 if t.attr then
103 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(" %s='%s'", k, tostring(v)); end end 106 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(" %s='%s'", k, tostring(v)); end end
104 end 107 end
105 return s_format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name); 108 return s_format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name);
109 end
110
111 function stanza_mt.top_tag(t)
112 local attr_string = "";
113 if t.attr then
114 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(" %s='%s'", k, tostring(v)); end end
115 end
116 return s_format("<%s%s>", t.name, attr_string);
106 end 117 end
107 118
108 function stanza_mt.__add(s1, s2) 119 function stanza_mt.__add(s1, s2)
109 return s1:add_direct_child(s2); 120 return s1:add_direct_child(s2);
110 end 121 end
182 193
183 function presence(attr) 194 function presence(attr)
184 return stanza("presence", attr); 195 return stanza("presence", attr);
185 end 196 end
186 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
187 return _M; 238 return _M;

mercurial