Skip to content

Commit

Permalink
Use export.generate_node instead of passing param
Browse files Browse the repository at this point in the history
  • Loading branch information
Frityet committed Aug 26, 2023
1 parent d9204c9 commit 135ce0b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
20 changes: 20 additions & 0 deletions luaxmlgenerator-0.2.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package = "LuaXMLGenerator"
version = "0.2.0-1"
source = {
url = "git+https://github.com/Frityet/LuaXMLGenerator",
tag = "0.2.0"
}
description = {
summary = "DSL to generate XML/HTML",
homepage = "https://github.com/Frityet/LuaXMLGenerator",
license = "MIT"
}
dependencies = {
"lua >= 5.1, < 5.5"
}
build = {
type = "builtin",
modules = {
["xml-generator"] = "xml-generator.lua"
}
}
20 changes: 20 additions & 0 deletions luaxmlgenerator-0.2.1-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package = "LuaXMLGenerator"
version = "0.2.1-1"
source = {
url = "git+https://github.com/Frityet/LuaXMLGenerator",
tag = "0.2.1"
}
description = {
summary = "DSL to generate XML/HTML",
homepage = "https://github.com/Frityet/LuaXMLGenerator",
license = "MIT"
}
dependencies = {
"lua >= 5.1, < 5.5"
}
build = {
type = "builtin",
modules = {
["xml-generator"] = "xml-generator.lua"
}
}
59 changes: 29 additions & 30 deletions xml-generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export.generator_metatable = setmetatable({}, {
--[[
tag "string"
]]
--
--
--then the content is the `string`
local tname = typename(attributes)
if tname ~= "table" and tname ~= "HTML.Node" then
Expand Down Expand Up @@ -198,42 +198,41 @@ function export.generate_node(ctx) return ctx(export.generator_metatable) end
function export.generate(ctx) return tostring(export.generate_node(ctx)) end

---Turns a lua table into an html table, recursively, with multiple levels of nesting
---@param xml XML.GeneratorTable
---@param tbl table
---@return XML.Node
function export.html_table(xml, tbl)
---@diagnostic disable: undefined-global
return xml.table {
function()
local function getval(v)
if type(v) ~= "table" or (getmetatable(v) or {}).__tostring then
return tostring(v)
function export.html_table(tbl)
return export.generate_node(function(xml)
return xml.table {
function()
local function getval(v)
if type(v) ~= "table" or (getmetatable(v) or {}).__tostring then
return tostring(v)
end
return export.html_table(v)
end
return export.html_table(xml, v)
end

for i, v in ipairs(tbl) do
coroutine.yield (
xml.tr {
xml.td(tostring(i)),
xml.td(getval(v)),
}
)
for i, v in ipairs(tbl) do
coroutine.yield(
xml.tr {
xml.td(tostring(i)),
xml.td(getval(v)),
}
)

tbl[i] = nil
end
tbl[i] = nil
end

for k, v in pairs(tbl) do
coroutine.yield (
xml.tr {
xml.td(tostring(k)),
xml.td(getval(v)),
}
)
for k, v in pairs(tbl) do
coroutine.yield(
xml.tr {
xml.td(tostring(k)),
xml.td(getval(v)),
}
)
end
end
end
}
---@diagnostic enable: undefined-global
}
end)
end

---@alias OptionalStringCollection string | string[]
Expand Down

0 comments on commit 135ce0b

Please sign in to comment.