|
| 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 | +``` |
0 commit comments