From 6bb0a34965cfb0f57f3e2a19ba8f4584f1e799ed Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 13 Nov 2008 03:47:44 +0000 Subject: Add new top_tag() method to stanzas --- util/stanza.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'util/stanza.lua') diff --git a/util/stanza.lua b/util/stanza.lua index 52f372cc..5339b91e 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -105,6 +105,14 @@ function stanza_mt.__tostring(t) return s_format("<%s%s>%s", t.name, attr_string, children_text, t.name); end +function stanza_mt.top_tag(t) + local attr_string = ""; + if t.attr then + 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 + end + return s_format("<%s%s>", t.name, attr_string); +end + function stanza_mt.__add(s1, s2) return s1:add_direct_child(s2); end -- cgit v1.2.3 From a3018d64216b7df0b18deaa5bb0bc1625c1ed670 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 13 Nov 2008 12:07:53 +0500 Subject: Fixed stanza deserialization --- util/stanza.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'util/stanza.lua') diff --git a/util/stanza.lua b/util/stanza.lua index 52f372cc..79d3167e 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -149,6 +149,9 @@ function deserialize(stanza) end stanza.tags = tags; end + if not stanza.last_add then + stanza.last_add = {}; + end end return stanza; -- cgit v1.2.3 From d67940a1d7700c0389341c42f845b91e8b32eea6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 16:03:33 +0000 Subject: A treat for Linux users ;) --- util/stanza.lua | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'util/stanza.lua') diff --git a/util/stanza.lua b/util/stanza.lua index cfa33c5b..5a6ba8c5 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -10,8 +10,11 @@ local next = next; local print = print; local unpack = unpack; local s_gsub = string.gsub; +local os = os; + +local do_pretty_printing = not os.getenv("WINDIR"); +local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; -local debug = debug; local log = require "util.logger".init("stanza"); module "stanza" @@ -157,9 +160,6 @@ function deserialize(stanza) end stanza.tags = tags; end - if not stanza.last_add then - stanza.last_add = {}; - end end return stanza; @@ -195,4 +195,44 @@ function presence(attr) return stanza("presence", attr); end +if do_pretty_printing then + local style_attrk = getstyle("yellow"); + local style_attrv = getstyle("red"); + local style_tagname = getstyle("red"); + local style_punc = getstyle("magenta"); + + local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); + local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); + --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, ""); + local tag_format = top_tag_format.."%s"..getstring(style_punc, ""); + function stanza_mt.pretty_print(t) + local children_text = ""; + for n, child in ipairs(t) do + if type(child) == "string" then + children_text = children_text .. xml_escape(child); + else + children_text = children_text .. child:pretty_print(); + end + end + + local attr_string = ""; + if t.attr then + 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 + end + return s_format(tag_format, t.name, attr_string, children_text, t.name); + end + + function stanza_mt.pretty_top_tag(t) + local attr_string = ""; + if t.attr then + 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 + end + return s_format(top_tag_format, t.name, attr_string); + end +else + -- Sorry, fresh out of colours for you guys ;) + stanza_mt.pretty_print = stanza_mt.__tostring; + stanza_mt.pretty_top_tag = stanza_mt.top_tag; +end + return _M; -- cgit v1.2.3