Skip to content

Commit

Permalink
fix generation of components
Browse files Browse the repository at this point in the history
  • Loading branch information
Frityet committed Jun 18, 2024
1 parent 3a31b45 commit 5e6fb3c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
49 changes: 30 additions & 19 deletions component-test.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local xml_gen = require("xml-generator")
local xml = xml_gen.xml

local tw = xml_gen.namespace "tw"
math.randomseed(os.time())

local header = xml_gen.component(function (args, kids)
return xml.head {
xml.title {args.title};
xml.title(args.title);
xml.meta {
name="viewport",
content="width=device-width, initial-scale=1"
Expand All @@ -15,24 +15,35 @@ local header = xml_gen.component(function (args, kids)
}
end)

local tw = xml_gen.namespace "tw"
local random_number = xml_gen.component(function (args)
return xml.p(math.random(args.min, args.max))
end)

local doc = xml.html {charset="utf8"} {
header {title="Hello, World!", css_framework=xml.link {rel="stylesheet", href="..."}} {
xml.script {src="index.lua"};
xml.br;
};

xml.body {
xml.h1 {class="text-center"} "Fritsite";
xml.main {class="container"} {
xml.p "Hello, World!";
xml.button {onclick="say_hi()"} "Say Hi!";
};

tw.div {id="test div"} "hello"
};
}
local yield = coroutine.yield
local doc = xml_gen.declare_generator(function ()
---@diagnostic disable: undefined-global
return html {charset="utf8"} {
header {title="Hello, World!", css_framework=link {rel="stylesheet", href="..."}};

body {
h1 {class="text-center"} "Fritsite";
main {class="container"} {
p "Hello, World!";
button {onclick="say_hi()"} "Say Hi!";
};

function ()
for i = 1, 10 do
yield(random_number {id="rn-"..i} {min=1, max=100})
end
end;

tw.div {id="test div"} "hello"
};
}
---@diagnostic enable: undefined-global
end)


print(doc)
print(doc())
4 changes: 3 additions & 1 deletion xml-generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ local typename = export.typename
local insert = table.insert
local concat = table.concat
local tostring = tostring
---@param node XML.Node
---@param node XML.Node | XML.Component
---@return string
function export.node_to_string(node)
if typename(node) == "XML.Component" then return tostring(node) end

local sanitize = not export.no_sanitize[node.tag:lower()]
local sanitize_text = sanitize and export.sanitize_text or function (...) return ... end

Expand Down

0 comments on commit 5e6fb3c

Please sign in to comment.