9999--- @field [ string] XML.Node
100100
101101--- @type XML.GeneratorTable
102- export .generator_metatable = setmetatable ({}, {
102+ export .xml = setmetatable ({}, {
103103 --- @param _ XML.GeneratorTable
104104 --- @param tag_name string
105105 __index = function (_ , tag_name )
@@ -140,7 +140,9 @@ export.generator_metatable = setmetatable({}, {
140140 return self
141141 end ;
142142
143- __tostring = export .node_to_string
143+ __tostring = export .node_to_string ;
144+
145+ __name = " XML.Node" ;
144146 })
145147 end
146148})
@@ -168,59 +170,51 @@ end)
168170
169171```
170172]=]
171- --- @param ctx fun ( html : XML.GeneratorTable ): XML.Node
172- --- @return XML.Node
173- function export .generate_node (ctx ) return ctx (export .generator_metatable ) end
174173
175174--- @generic T
176175--- @param func fun ( ... : T ): XML.Node
177176--- @return fun ( ... : T ): XML.Node
178- function export .declare_generator (func ) return setfenv (func , export .generator_metatable ) end
179-
180- --- @param ctx fun ( html : XML.GeneratorTable ): table
181- --- @return string
182- function export .generate (ctx ) return tostring (export .generate_node (ctx )) end
177+ function export .declare_generator (func ) return setfenv (func , export .xml ) end
183178
184179--- Turns a lua table into an html table, recursively, with multiple levels of nesting
185180--- @param tbl table
186181--- @return XML.Node
187182function export .html_table (tbl )
188- return export .generate_node (function (xml )
189- return xml .table {
190- function ()
191- local function getval (v )
192- local tname = typename (v )
193- if tname == " XML.Node" then return v end
194-
195- if typename (v ) ~= " table" or (getmetatable (v ) or {}).__tostring then
196- return tostring (v )
197- end
198-
199- return export .html_table (v )
183+ local xml = export .xml
184+ return xml .table {
185+ function ()
186+ local function getval (v )
187+ local tname = typename (v )
188+ if tname == " XML.Node" then return v end
189+
190+ if typename (v ) ~= " table" or (getmetatable (v ) or {}).__tostring then
191+ return tostring (v )
200192 end
201193
202- for i , v in ipairs (tbl ) do
203- coroutine.yield (
204- xml .tr {
205- xml .td (tostring (i )),
206- xml .td (getval (v )),
207- }
208- )
194+ return export .html_table (v )
195+ end
196+
197+ for i , v in ipairs (tbl ) do
198+ coroutine.yield (
199+ xml .tr {
200+ xml .td (tostring (i )),
201+ xml .td (getval (v )),
202+ }
203+ )
209204
210- tbl [i ] = nil
211- end
205+ tbl [i ] = nil
206+ end
212207
213- for k , v in pairs (tbl ) do
214- coroutine.yield (
215- xml .tr {
216- xml .td (tostring (k )),
217- xml .td (getval (v )),
218- }
219- )
220- end
208+ for k , v in pairs (tbl ) do
209+ coroutine.yield (
210+ xml .tr {
211+ xml .td (tostring (k )),
212+ xml .td (getval (v )),
213+ }
214+ )
221215 end
222- }
223- end )
216+ end
217+ }
224218end
225219
226220--- @alias OptionalStringCollection string | string[]
@@ -240,7 +234,7 @@ function export.style(css)
240234 css_str = css_str .. " }\n "
241235 end
242236
243- return export .generate_node ( function ( xml ) return xml .style (css_str ) end )
237+ return export .xml .style (css_str )
244238end
245239
246240
0 commit comments