summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-05-22 13:18:09 -0500
committersanine <sanine.not@pm.me>2022-05-22 13:18:09 -0500
commit23e77b891e735b68ed0d83b1b1d4560fb5224785 (patch)
tree61f8eb2531d97b36c87c2ec67865ced1aa6b3a96
parent7188614f0e2103f2cfc87dd0cb268f82fafd8c22 (diff)
add basic html rendering
-rw-r--r--marigold.lua27
-rwxr-xr-xtest.lua16
2 files changed, 42 insertions, 1 deletions
diff --git a/marigold.lua b/marigold.lua
index 4f04052..37cbdfb 100644
--- a/marigold.lua
+++ b/marigold.lua
@@ -1,6 +1,6 @@
-- ANTI-CAPITALIST SOFTWARE LICENSE (v 1.4)
--
--- Copyright (c) 2022 Kate Swanson
+-- marigold-cgi Copyright (c) 2022 Kate Swanson
--
-- This is anti-capitalist software, released for free use by individuals and
-- organizations that do not operate by capitalist principles.
@@ -84,4 +84,29 @@ marigold.h = function(tag_type, content, tbl)
return tag
end
+
+marigold.html = function(tbl, indent_level)
+ indent_level = indent_level or 0
+
+ -- generate attribute strings
+ local attributes = {}
+ for k, v in pairs(tbl.attributes) do
+ table.insert(attributes, string.format(' %s="%s"', k, v))
+ end
+ if test then
+ -- sort alphabetically for well-defined testing
+ table.sort(attributes)
+ end
+ local a = ''
+ for _, attrib in ipairs(attributes) do
+ a = a .. attrib
+ end
+
+ 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)
+end
+
+
return marigold
diff --git a/test.lua b/test.lua
index cd999b2..5968e81 100755
--- a/test.lua
+++ b/test.lua
@@ -127,5 +127,21 @@ test("marigold.h correctly sets attributes and children", function()
assert(div.children[1].content == 'the first paragraph')
assert(div.children[2].tag == 'p')
assert(div.children[2].content == 'the second paragraph')
+end)
+
+
+test("marigold.html correctly renders basic html table", function()
+ local tbl = { tag='p', content='some paragraph', attributes={}, children={}}
+ local html = marigold.html(tbl)
+ assert(html == '<p>some paragraph</p>')
+end)
+
+test("marigold.html correctly renders tag attributes", function()
+ local tbl = {
+ tag='p', content='some paragraph',
+ attributes={ id='p1', class='blinking bold' },
+ children={}}
+ local html = marigold.html(tbl)
+ assert(html == '<p class="blinking bold" id="p1">some paragraph</p>')
end)