summaryrefslogtreecommitdiff
path: root/lib/xmllib.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/xmllib.lua')
-rw-r--r--lib/xmllib.lua63
1 files 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 "<!-- " .. v.nodes[1] .. " -->"
+ return space_indent(depth) .. "<!-- " .. v.nodes[1] .. " -->\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 .. "</" .. v.key .. ">"
+ return space_indent(depth) .. "<" .. v.key .. render_attrs(v.attrs) .. ">" .. middle .. "</" .. v.key .. ">"
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:
--- <html><head><title>Test</title></head><body bgcolor="#ffeedd">Normal test<br /><b>Bold test.</b><!-- Hi there... --></body></html>
-
+result = [[
+<?xml version="1.0" encoding="UTF-8"?>
+<html>
+ <head>
+ <title>
+Test</title>
+ </head>
+
+ <body bgcolor="#ffeedd">
+Normal test
+ <br />
+
+ <b>
+Bold test.</b>
+
+ <!-- Hi there... -->
+
+ </body>
+</html>
+]]
xm = NewXmlMarkup()