Skip to content

Commit 0744262

Browse files
committed
Update documentation and add styles for html_table
1 parent 1c203c6 commit 0744262

File tree

3 files changed

+210
-15
lines changed

3 files changed

+210
-15
lines changed

README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# LuaXMLGenerator
2+
3+
Library to easily generate XML with a clean Lua DSL.
4+
5+
## Installation
6+
7+
```bash
8+
luarocks insatll luaxmlgenerator
9+
```
10+
11+
## Usage
12+
13+
```lua
14+
local xml_gen = require("xml-generator")
15+
local xml = xml_gen.xml
16+
17+
local doc = xml.html {charset="utf-8", lang="en"} {
18+
xml.head {
19+
xml.title "Hello World"
20+
},
21+
xml.body {
22+
xml.h1 "Hello World",
23+
24+
xml.div {id="numbers"} {
25+
function() --run as a coroutine
26+
for i = 1, 10 do
27+
coroutine.yield(xml.p(i))
28+
end
29+
end
30+
}
31+
}
32+
}
33+
34+
print(doc)
35+
```
36+
37+
## Options
38+
39+
### `xml_gen.no_sanitize`
40+
Table of tags that should not be sanitized. By default, it contains `script` and `style`.
41+
42+
```lua
43+
local xml_gen = require("xml-generator")
44+
xml_gen.no_sanitize["mytag"] = true
45+
46+
local doc = xml_gen.xml {
47+
xml.mytag [[
48+
Thsi will not be sanitized! <><><><><><%%%<>%<>%<>% you can use all of this!
49+
]]
50+
}
51+
52+
```
53+
54+
### `xml_gen.lua_is_global`
55+
56+
By default, within `xml_gen.xml` there is a special key called `lua` which just allows for `_G` to be accessed. This is useful if you use `xml_gen.declare_generator`, which overloads the `_ENV` (`setfenv` on 5.1), where the `_G` would not be ordinarily accessible.
57+
58+
```lua
59+
local xml_gen = require("xml-generator")
60+
61+
local gen = xml_gen.declare_generator(function()
62+
return html {
63+
head {
64+
title "Hello World";
65+
};
66+
67+
body {
68+
p { "The time of generation is ", lua.os.date() }
69+
};
70+
}
71+
end)
72+
73+
print(gen())
74+
```
75+
76+
## Utilities
77+
78+
### `xml_gen,declare_generator`
79+
```lua
80+
---@generic T
81+
---@param func fun(...: T): XML.Node
82+
---@return fun(...: T): XML.Node
83+
function export.declare_generator(func)
84+
```
85+
86+
Allows you to create a function in which the `_ENV` is overloaded with the `xml` table. This allows you to write XML more concisely (see example above).
87+
88+
### `xml_gen.html_table`
89+
```lua
90+
---@generic TKey, TValue
91+
---@param tbl { [TKey] : TValue },
92+
---@param order TKey[]?
93+
---@param classes { table: string?, tr: string?, td: string? }?
94+
---@return XML.Node
95+
function export.html_table(tbl, order, classes)
96+
```
97+
98+
Creates an HTML table based off a lua table. This is unstyled, so you will need to add your own CSS.
99+
100+
```lua
101+
102+
local my_table = {
103+
key = "value",
104+
sub = {
105+
key = "value",
106+
}
107+
}
108+
109+
local tbl = xml_gen.html_table(my_table, { "key", "sub" }, {
110+
table = "my-table",
111+
tr = "my-table-row",
112+
td = "my-table-cell",
113+
})
114+
115+
print(tbl)
116+
117+
```
118+
119+
### `xml_gen.style`
120+
```lua
121+
---@param css { [string | string[]] : { [string | string[]] : (number | string | string[]) } }
122+
---@return XML.Node
123+
function export.style(css)
124+
```
125+
126+
Creates an HTML `style` tag with the given table
127+
128+
```lua
129+
local xml_gen = require("xml-generator")
130+
131+
local style = xml_gen.style {
132+
[{ "body", "html" }] = {
133+
margin = 0,
134+
padding = 0,
135+
},
136+
137+
body = {
138+
background = "#000",
139+
color = "#fff",
140+
}
141+
142+
--etc
143+
}
144+
145+
print(style)
146+
```

test.lua

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,54 @@
11
local xml_gen = require("xml-generator")
22
local xml = xml_gen.xml
33

4-
local test = xml.div {key="value"}
4+
local doc = xml.html {charset="utf-8", lang="en"} {
5+
xml.head {
6+
xml.title "Hello World"
7+
},
8+
xml.body {
9+
xml.h1 "Hello World",
510

6-
print(xml_gen.node_to_string(test))
11+
xml.div {id="numbers"} {
12+
function() --run as a coroutine
13+
for i = 1, 10 do
14+
coroutine.yield(xml.p(i))
15+
end
16+
end
17+
}
18+
}
19+
}
20+
21+
print(doc)
22+
23+
local my_table = {
24+
key = "value",
25+
sub = {
26+
key = "value",
27+
}
28+
}
29+
30+
local tbl = xml_gen.html_table(my_table, { "key", "sub" }, {
31+
table = "my-table",
32+
tr = "my-table-row",
33+
td = "my-table-cell",
34+
})
35+
36+
print(tbl)
37+
38+
local xml_gen = require("xml-generator")
39+
40+
local style = xml_gen.style {
41+
[{ "body", "html" }] = {
42+
margin = 0,
43+
padding = 0,
44+
},
45+
46+
body = {
47+
background = "#000",
48+
color = "#fff",
49+
}
50+
51+
--etc
52+
}
53+
54+
print(style)

xml-generator.lua

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ function export.declare_generator(func) return setfenv(func, export.xml) end
168168
---@generic TKey, TValue
169169
---@param tbl { [TKey] : TValue },
170170
---@param order TKey[]?
171+
---@param classes { table: string?, tr: string?, td: string? }?
171172
---@return XML.Node
172-
function export.html_table(tbl, order)
173+
function export.html_table(tbl, order, classes)
174+
local classes = classes or {}
173175
local xml = export.xml
174-
return xml.table {
176+
return xml.table {class=classes.table} {
175177
function ()
176178
local function getval(v)
177179
local tname = typename(v)
@@ -188,18 +190,18 @@ function export.html_table(tbl, order)
188190
for i, v in ipairs(order) do
189191
local val = tbl[v]
190192
coroutine.yield (
191-
xml.tr {
192-
xml.td(tostring(v)),
193-
xml.td(getval(val))
193+
xml.tr {class=classes.table} {
194+
xml.td {class=classes.td} (tostring(v)),
195+
xml.td {class=classes.td} (getval(val))
194196
}
195197
)
196198
end
197199
else
198200
for i, v in ipairs(tbl) do
199201
coroutine.yield (
200-
xml.tr {
201-
xml.td(tostring(i)),
202-
xml.td(getval(v)),
202+
xml.tr {class=classes.tr} {
203+
xml.td {class=classes.td} (tostring(i)),
204+
xml.td {class=classes.td} (getval(v)),
203205
}
204206
)
205207

@@ -208,9 +210,9 @@ function export.html_table(tbl, order)
208210

209211
for k, v in pairs(tbl) do
210212
coroutine.yield (
211-
xml.tr {
212-
xml.td(tostring(k)),
213-
xml.td(getval(v)),
213+
xml.tr {class=classes.tr} {
214+
xml.td {class=classes.td} (tostring(k)),
215+
xml.td {class=classes.td} (getval(v)),
214216
}
215217
)
216218
end
@@ -220,8 +222,7 @@ function export.html_table(tbl, order)
220222
end
221223

222224
---Creates a style tag with the given lua table
223-
---@alias OptionalStringCollection string | string[]
224-
---@param css { [OptionalStringCollection] : { [OptionalStringCollection] : (OptionalStringCollection) } }
225+
---@param css { [string | string[]] : { [string | string[]] : (number | string | string[]) } }
225226
---@return XML.Node
226227
function export.style(css)
227228
local css_str = ""

0 commit comments

Comments
 (0)