|
1 | 1 | local xml_gen = require("xml-generator")
|
| 2 | +local xml = xml_gen.xml |
2 | 3 |
|
3 | 4 | ---Table -> Tree using <details> and <summary>
|
4 | 5 | ---@param tbl table
|
5 | 6 | ---@return XML.Node
|
6 | 7 | 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 |
19 | 16 | end
|
20 | 17 |
|
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 |
40 | 20 |
|
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 | + ) |
42 | 39 | end
|
43 | 40 |
|
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), |
60 | 59 | }
|
61 |
| - ) |
62 |
| - end |
| 60 | + } |
| 61 | + ) |
63 | 62 | end
|
64 | 63 | end
|
65 |
| - } |
66 |
| - end) |
| 64 | + end |
| 65 | + } |
67 | 66 | end
|
68 | 67 |
|
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 |
107 | 68 |
|
108 |
| -local doc = xml.html {charset="utf8"} { |
| 69 | +local doc = xml.html { charset = "utf8" } { |
109 | 70 | xml.head {
|
110 |
| - xml.title "Hello, World!"; |
| 71 | + xml.title "Hello, World!", |
111 | 72 | xml_gen.style {
|
112 | 73 | ["details > div"] = {
|
113 | 74 | ["margin-left"] = "10px"
|
114 | 75 | }
|
115 | 76 | }
|
116 |
| - }; |
| 77 | + }, |
117 | 78 |
|
118 | 79 | xml.body {
|
119 |
| - xml.div {id="numbers"} { |
| 80 | + xml.div { id = "numbers" } { |
120 | 81 | tree {
|
121 |
| - key = "value"; |
| 82 | + key = "value", |
122 | 83 | sub = {
|
123 | 84 | key = "value"
|
124 |
| - }; |
| 85 | + }, |
125 | 86 |
|
126 | 87 | array = { 1, 2, 3, 4, 5 }
|
127 | 88 | }
|
|
0 commit comments