-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.moonxml.lua
186 lines (173 loc) · 3.9 KB
/
template.moonxml.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
local tbl, inspect = ...
local ROOT
ROOT = function() end
local stack = {
push = table.insert,
pop = table.remove,
find = function(self, target)
for index, item in ipairs(self) do
if item == target then
return index
end
end
end
}
local dump
dump = function(k, v)
if k == nil then
k = ROOT
end
return div({
class = "pair"
}, function()
return div({
class = "kv value"
}, function()
local _exp_0 = type(v)
if "table" == _exp_0 then
return details(function()
summary(function()
if k ~= ROOT then
div({
class = "kv key"
}, function()
span("[")
span({
class = "code-" .. tostring(type(k))
}, inspect(k))
return span("] = ")
end)
end
local len = 0
for kk, vv in pairs(v) do
len = len + 1
end
span({
class = "summary"
}, "Table (" .. tostring(len) .. ")")
return span({
class = "object"
}, tostring(v))
end)
if stack:find(v) then
return b("(Recursion)")
else
stack:push(v)
local tpairs = { }
for kk, vv in pairs(v) do
table.insert(tpairs, {
kk,
vv
})
end
table.sort(tpairs, function(a, b)
a, b = a[1], b[1]
if (type(a)) < (type(b)) then
return true
elseif (type(a)) > (type(b)) then
return false
else
if (type(a)) == "number" then
return a < b
else
return (tostring(a)) < (tostring(b))
end
end
end)
for idx, pair in ipairs(tpairs) do
dump(pair[1], pair[2])
end
return stack:pop()
end
end)
else
if k ~= ROOT then
div({
class = "kv key"
}, function()
span("[")
span({
class = "code-" .. tostring(type(k))
}, inspect(k))
return span("] = ")
end)
end
return div({
class = "kv code-" .. tostring(type(v))
}, inspect(v))
end
end)
end)
end
print("<!doctype html>")
return html(function()
head(function()
return style(function()
return print([[ details > *:not(summary) {
padding-left: 2em;
}
#main {
font-family: Fira Code, 'Courier New', Courier, monospace;
}
span.object {
color: #aaa;
font-size: 0.7em;
}
div.pair {
margin: 0.5em;
display: flex;
}
div.kv {
display: inline;
width: max-content;
}
details,
details[open],
details:focus {
outline: 0;
outline-color: #fff;
}
.code-string {
color: green;
}
.code-number {
color: purple;
}
.code-boolean {
color: gold;
}
.code-function {
color: blue;
}
.code-table {
color: brown;
}
details > summary {
list-style: none;
}
details > summary::-webkit-details-marker {
display: none;
}
details > summary::after {
content: "\25BC";
display: inline-block;
transform: rotate(-90deg);
margin-left: .6em;
}
details[open] > summary::after {
transform: none;
}
:focus {
outline: none;
}
]])
end)
end)
return body(function()
return div({
id = "main"
}, function()
return dump(ROOT, tbl)
end)
end)
end)