summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-05-22 13:26:45 -0500
committersanine <sanine.not@pm.me>2022-05-22 13:26:45 -0500
commitdf73dca1a539387a12da632f2dd48e34f08d70d0 (patch)
tree0f4dc3cce3d05ab455940981f23d9e8d868205e5
parent23e77b891e735b68ed0d83b1b1d4560fb5224785 (diff)
add child rendering
-rw-r--r--marigold.lua15
-rwxr-xr-xtest.lua17
2 files changed, 31 insertions, 1 deletions
diff --git a/marigold.lua b/marigold.lua
index 37cbdfb..ac0e061 100644
--- a/marigold.lua
+++ b/marigold.lua
@@ -87,6 +87,7 @@ end
marigold.html = function(tbl, indent_level)
indent_level = indent_level or 0
+ local indent = string.rep('\t', indent_level)
-- generate attribute strings
local attributes = {}
@@ -105,7 +106,19 @@ marigold.html = function(tbl, indent_level)
local open = string.format('<%s%s>', tbl.tag, a)
local close = string.format('</%s>', tbl.tag)
- return string.format('%s%s%s', open, tbl.content, close)
+ if #tbl.children == 0 then
+ return string.format('%s%s%s%s', indent, open, tbl.content, close)
+ end
+
+ local children = ''
+ for _, child in ipairs(tbl.children) do
+ children = children .. marigold.html(child, indent_level+1) .. '\n'
+ end
+
+ return string.format('%s%s%s\n%s%s%s',
+ indent, open, tbl.content,
+ children,
+ indent, close)
end
diff --git a/test.lua b/test.lua
index 5968e81..a464324 100755
--- a/test.lua
+++ b/test.lua
@@ -145,3 +145,20 @@ test("marigold.html correctly renders tag attributes", function()
local html = marigold.html(tbl)
assert(html == '<p class="blinking bold" id="p1">some paragraph</p>')
end)
+
+
+test("marigold.html correctly renders children", function()
+ local tbl = {
+ tag = 'div', content='', attributes = {id="root"},
+ children = {
+ { tag='p', content='p1', attributes = {class="bold"}, children={} },
+ { tag='p', content='p2', attributes={}, children={} },
+ }
+ }
+
+ local html = marigold.html(tbl)
+ assert(html == [[<div id="root">
+ <p class="bold">p1</p>
+ <p>p2</p>
+</div>]])
+end)