Skip to content

Commit 5e6fb3c

Browse files
committed
fix generation of components
1 parent 3a31b45 commit 5e6fb3c

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

component-test.lua

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
local xml_gen = require("xml-generator")
22
local xml = xml_gen.xml
3-
3+
local tw = xml_gen.namespace "tw"
44
math.randomseed(os.time())
55

66
local header = xml_gen.component(function (args, kids)
77
return xml.head {
8-
xml.title {args.title};
8+
xml.title(args.title);
99
xml.meta {
1010
name="viewport",
1111
content="width=device-width, initial-scale=1"
@@ -15,24 +15,35 @@ local header = xml_gen.component(function (args, kids)
1515
}
1616
end)
1717

18-
local tw = xml_gen.namespace "tw"
18+
local random_number = xml_gen.component(function (args)
19+
return xml.p(math.random(args.min, args.max))
20+
end)
1921

20-
local doc = xml.html {charset="utf8"} {
21-
header {title="Hello, World!", css_framework=xml.link {rel="stylesheet", href="..."}} {
22-
xml.script {src="index.lua"};
23-
xml.br;
24-
};
25-
26-
xml.body {
27-
xml.h1 {class="text-center"} "Fritsite";
28-
xml.main {class="container"} {
29-
xml.p "Hello, World!";
30-
xml.button {onclick="say_hi()"} "Say Hi!";
31-
};
3222

33-
tw.div {id="test div"} "hello"
34-
};
35-
}
23+
local yield = coroutine.yield
24+
local doc = xml_gen.declare_generator(function ()
25+
---@diagnostic disable: undefined-global
26+
return html {charset="utf8"} {
27+
header {title="Hello, World!", css_framework=link {rel="stylesheet", href="..."}};
28+
29+
body {
30+
h1 {class="text-center"} "Fritsite";
31+
main {class="container"} {
32+
p "Hello, World!";
33+
button {onclick="say_hi()"} "Say Hi!";
34+
};
35+
36+
function ()
37+
for i = 1, 10 do
38+
yield(random_number {id="rn-"..i} {min=1, max=100})
39+
end
40+
end;
41+
42+
tw.div {id="test div"} "hello"
43+
};
44+
}
45+
---@diagnostic enable: undefined-global
46+
end)
3647

3748

38-
print(doc)
49+
print(doc())

xml-generator.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ local typename = export.typename
113113
local insert = table.insert
114114
local concat = table.concat
115115
local tostring = tostring
116-
---@param node XML.Node
116+
---@param node XML.Node | XML.Component
117117
---@return string
118118
function export.node_to_string(node)
119+
if typename(node) == "XML.Component" then return tostring(node) end
120+
119121
local sanitize = not export.no_sanitize[node.tag:lower()]
120122
local sanitize_text = sanitize and export.sanitize_text or function (...) return ... end
121123

0 commit comments

Comments
 (0)