From fa3cdddf8dbd82b48d4b2740964edafb56e4a135 Mon Sep 17 00:00:00 2001 From: pixel Date: Wed, 30 May 2007 09:46:37 +0000 Subject: Fixing xml markup a bit. --- lib/xmllib.lua | 63 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/lib/xmllib.lua b/lib/xmllib.lua index 27f36a0..d7ac9b5 100644 --- a/lib/xmllib.lua +++ b/lib/xmllib.lua @@ -43,7 +43,7 @@ local function process_lf(xm) } end -local function burrow(t) +local function burrow(xm, t) local k, v if type(t) ~= "table" then return end @@ -69,14 +69,18 @@ local function process_tag(xm, key, a) a = { a } end + if type(a) == "nil" then + a = {} + end + if type(a) ~= "table" then - error "Wrong argument type." + error("Wrong argument type: " .. type(a)) end if a.__objname == nil then for k, v in ipairs(a) do if type(v) == "table" then - burrow(v) + burrow(xm, v) has_nodes = true table.insert(nodes, v) elseif type(v) == "string" then @@ -123,31 +127,41 @@ local function render_attrs(t) return r end -local function do_render_r(v) +local function space_indent(depth) + local r, i = "" + + for i = 1, depth do + r = r .. " " + end + + return r +end + +local function do_render_r(v, depth) local middle, i, n = "" if v.__objname == "Handle" then local r = "" - while not v.isclosed() do - r = r .. v.read() .. "\n" + while not v:isclosed() do + r = r .. v:read() .. "\n" end return r elseif v.type == "comment" then - return "" + return space_indent(depth) .. "\n" elseif v.type == "lf" then return "\n" elseif type(v.key) == "string" then if type(v.nodes) == "table" then for i, n in ipairs(v.nodes) do if type(n) == "string" then - middle = middle .. n + middle = middle .. "\n" .. n elseif type(n) == "table" then - middle = middle .. do_render_r(n) + middle = middle .. "\n" .. do_render_r(n, depth + 1) .. "\n" .. space_indent(depth) end end - return "<" .. v.key .. render_attrs(v.attrs) .. ">" .. middle .. "" + return space_indent(depth) .. "<" .. v.key .. render_attrs(v.attrs) .. ">" .. middle .. "" else - return "<" .. v.key .. render_attrs(v.attrs) .. " />" + return space_indent(depth) .. "<" .. v.key .. render_attrs(v.attrs) .. " />" end else error "Inconsistancy in the XML structure." @@ -160,10 +174,10 @@ local function do_render(xm) local r, i, n = "" for i, n in pairs(xm.tops) do - r = r .. do_render_r(n) + r = r .. do_render_r(n, 0) end - return xm.doctype .. "\n" .. r + return xm.doctype .. "\n" .. r .. "\n" end local xmlmarkup_metatable = { @@ -190,9 +204,26 @@ end --[[ example of use: --- result is: --- TestNormal test
Bold test. - +result = [[ + + + + +Test + + + +Normal test +
+ + +Bold test. + + + + + +]] xm = NewXmlMarkup() -- cgit v1.2.3