Skip to content

Commit faf734a

Browse files
committed
update example again
1 parent 8136357 commit faf734a

File tree

1 file changed

+57
-96
lines changed

1 file changed

+57
-96
lines changed

test.lua

Lines changed: 57 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,88 @@
11
local xml_gen = require("xml-generator")
2+
local xml = xml_gen.xml
23

34
---Table -> Tree using &ltdetails&gt and &ltsummary&gt
45
---@param tbl table
56
---@return XML.Node
67
local function tree(tbl)
7-
return xml_gen.generate_node(function(xml)
8-
return xml.div {
9-
function ()
10-
local function getval(v)
11-
local tname = xml_gen.typename(v)
12-
if tname == "XML.Node" then return v, false end
13-
14-
if tname ~= "table" or (getmetatable(v) or {}).__tostring then
15-
return xml.p(tostring(v)), false
16-
end
17-
18-
return tree(v), true
8+
return xml.div {
9+
function()
10+
local function getval(v)
11+
local tname = xml_gen.typename(v)
12+
if tname == "XML.Node" then return v, false end
13+
14+
if tname ~= "table" or (getmetatable(v) or {}).__tostring then
15+
return xml.p(tostring(v)), false
1916
end
2017

21-
for i, v in ipairs(tbl) do
22-
local val, is_tree = getval(v)
23-
if is_tree then
24-
coroutine.yield (
25-
xml.details {
26-
xml.summary(tostring(i)),
27-
val
28-
}
29-
)
30-
else
31-
coroutine.yield (
32-
xml.table {
33-
xml.tr {
34-
xml.td(tostring(i));
35-
xml.td(val);
36-
}
37-
}
38-
)
39-
end
18+
return tree(v), true
19+
end
4020

41-
tbl[i] = nil
21+
for i, v in ipairs(tbl) do
22+
local val, is_tree = getval(v)
23+
if is_tree then
24+
coroutine.yield(
25+
xml.details {
26+
xml.summary(tostring(i)),
27+
val
28+
}
29+
)
30+
else
31+
coroutine.yield(
32+
xml.table {
33+
xml.tr {
34+
xml.td(tostring(i)),
35+
xml.td(val),
36+
}
37+
}
38+
)
4239
end
4340

44-
for k, v in pairs(tbl) do
45-
local val, is_tree = getval(v)
46-
if is_tree then
47-
coroutine.yield (
48-
xml.details {
49-
xml.summary(tostring(k)),
50-
val
51-
}
52-
)
53-
else
54-
coroutine.yield (
55-
xml.table {
56-
xml.tr {
57-
xml.td(tostring(k));
58-
xml.td(val);
59-
}
41+
tbl[i] = nil
42+
end
43+
44+
for k, v in pairs(tbl) do
45+
local val, is_tree = getval(v)
46+
if is_tree then
47+
coroutine.yield(
48+
xml.details {
49+
xml.summary(tostring(k)),
50+
val
51+
}
52+
)
53+
else
54+
coroutine.yield(
55+
xml.table {
56+
xml.tr {
57+
xml.td(tostring(k)),
58+
xml.td(val),
6059
}
61-
)
62-
end
60+
}
61+
)
6362
end
6463
end
65-
}
66-
end)
64+
end
65+
}
6766
end
6867

69-
--[[
70-
details {
71-
font-family: arial, sans-serif;
72-
margin-left: 10px; /* Reduced for less indentation */
73-
border-left: 1px solid #ccc; /* Reduced for less spacing */
74-
padding-left: 5px; /* Reduced for less spacing */
75-
}
76-
77-
summary {
78-
font-weight: bold;
79-
cursor: pointer;
80-
display: list-item; /* To keep the arrow */
81-
list-style: disclosure-closed; /* Default arrow when closed */
82-
}
83-
84-
summary::-webkit-details-marker { /* Custom arrow for Webkit browsers */
85-
display: none;
86-
}
87-
88-
details[open] summary {
89-
list-style: disclosure-open; /* Arrow when opened */
90-
}
91-
92-
div {
93-
padding: 2px; /* Reduced for less spacing */
94-
margin-top: 2px; /* Reduced for less spacing */
95-
margin-bottom: 2px; /* Reduced for less spacing */
96-
border-radius: 3px; /* Reduced for less spacing */
97-
background-color: #f2f2f2;
98-
}
99-
100-
details > div {
101-
margin-left: 10px; /* Reduced for less indentation */
102-
}
103-
104-
]]
105-
106-
local xml = xml_gen.xml
10768

108-
local doc = xml.html {charset="utf8"} {
69+
local doc = xml.html { charset = "utf8" } {
10970
xml.head {
110-
xml.title "Hello, World!";
71+
xml.title "Hello, World!",
11172
xml_gen.style {
11273
["details > div"] = {
11374
["margin-left"] = "10px"
11475
}
11576
}
116-
};
77+
},
11778

11879
xml.body {
119-
xml.div {id="numbers"} {
80+
xml.div { id = "numbers" } {
12081
tree {
121-
key = "value";
82+
key = "value",
12283
sub = {
12384
key = "value"
124-
};
85+
},
12586

12687
array = { 1, 2, 3, 4, 5 }
12788
}

0 commit comments

Comments
 (0)