forked from carlosmpv/lua_to_html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_to_html.lua
60 lines (48 loc) · 1.52 KB
/
lua_to_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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
local lua_to_html = {}
function lua_to_html:read_element (element, depth, ident)
decoded = ''
local tab = ''
if ident then
tab = '\n'
for i = 1, depth do
tab = tab.. '\t'
end
end
for k, v in ipairs(element) do
if type(v) == 'table' then
tagAttr = '<'.. v[1]
for i, a in pairs(v) do
if type(i) ~= 'number' and i ~= 1 then
tagAttr = tagAttr.. ' '.. i.. '="'.. a.. '"'
end
end
if #v > 1 and type(v[#v]) ~= 'boolean' then
tagAttr = tagAttr.. '>'
decoded = decoded.. (tab.. tagAttr)
decoded = decoded.. lua_to_html:read_element(v[#v], depth + 1, ident)
decoded = decoded.. (tab.. '</'.. v[1].. '>')
else
if type(v[#v]) ~= 'boolean' then
tagAttr = tagAttr.. '/>'
else
if not v[#v] then
tagAttr = tagAttr.. '>'
else
tagAttr = tagAttr.. '/>'
end
end
decoded = decoded.. (tab.. tagAttr)
end
else
decoded = decoded.. (tab.. v)
end
end
if type(element) ~= 'table' then
decoded = decoded.. (tab.. element)
end
return decoded
end
function lua_to_html:translate(table, ident)
return lua_to_html:read_element(table, 0, ident)
end
return lua_to_html