# HG changeset patch # User Kim Alvefur # Date 1586703785 -7200 # Node ID 05e4645fc9b3c3a7d3afe5b66b0bacf7e796d98d # Parent 29575fe648600003aa999c828d1af5aee8428c1b util.stanza: Add method returning stanza with added indentation Adds indentation and line breaks to stanzas, to make stanzas easier to read for humans. diff -r 29575fe64860 -r 05e4645fc9b3 spec/util_stanza_spec.lua --- a/spec/util_stanza_spec.lua Sat Apr 11 19:31:15 2020 +0200 +++ b/spec/util_stanza_spec.lua Sun Apr 12 17:03:05 2020 +0200 @@ -459,4 +459,12 @@ assert.equal("true", s2.attr["my-awesome-ns\1bar"]); end); end); + + describe("indent", function () + local s = st.stanza("foo"):text("\n"):tag("bar"):tag("baz"):up():text_tag("cow", "moo"); + assert.equal("\n\t\n\t\t\n\t\tmoo\n\t\n", tostring(s:indent())); + assert.equal("\n \n \n moo\n \n", tostring(s:indent(1, " "))); + assert.equal("\n\t\t\n\t\t\t\n\t\t\tmoo\n\t\t\n\t", tostring(s:indent(2, "\t"))); + end); + end); diff -r 29575fe64860 -r 05e4645fc9b3 util/stanza.lua --- a/util/stanza.lua Sat Apr 11 19:31:15 2020 +0200 +++ b/util/stanza.lua Sun Apr 12 17:03:05 2020 +0200 @@ -511,6 +511,36 @@ stanza_mt.pretty_top_tag = stanza_mt.top_tag; end +function stanza_mt.indent(t, level, indent) + if #t == 0 or (#t == 1 and type(t[1]) == "string") then + -- Empty nodes wouldn't have any indentation + -- Text-only nodes are preserved as to not alter the text content + -- Optimization: Skip clone of these since we don't alter them + return t; + end + + indent = indent or "\t"; + level = level or 1; + local tag = clone(t, true); + + for child in t:children() do + if type(child) == "string" then + -- Already indented text would look weird but let's ignore that for now. + if child:find("%S") then + tag:text("\n" .. indent:rep(level)); + tag:text(child); + end + elseif is_stanza(child) then + tag:text("\n" .. indent:rep(level)); + tag:add_direct_child(child:indent(level+1, indent)); + end + end + -- before the closing tag + tag:text("\n" .. indent:rep((level-1))); + + return tag; +end + return { stanza_mt = stanza_mt; stanza = new_stanza;