-
Notifications
You must be signed in to change notification settings - Fork 2
/
html.lua
33 lines (29 loc) · 885 Bytes
/
html.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
local M = {}
local function parse_args(args)
if not args then return "" end
local out = {""}
for key, val in pairs(args) do
table.insert(out, ("%s=%q"):format(key,val))
end
local out_str = table.concat(out, " ") .. " "
return out_str
end
---@return string
function M.doctype()
return "<!DOCTYPE html>"
end
M.__index = function(self, key)
return function(args, cont, ...)
if type(cont) ~= "table" then cont = table.pack(cont, ...) end
if not cont or (#cont < 1) then
local out = "<%s%%s />"
local out_f = out:format(key, key)
return (out_f):format(parse_args(args))
else
local out = "<%s%%s>%%s</%s>"
local out_f = out:format(key, key)
return (out_f):format(parse_args(args), table.concat(cont))
end
end
end
return setmetatable(M, M)