Repository navigation
Expand file tree
/
Copy pathluatml.lua
More file actions
229 lines (194 loc) · 5.02 KB
/
Copy pathluatml.lua
File metadata and controls
229 lines (194 loc) · 5.02 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
-- Fix Lua 5.4 "require".
-- Before, "require" would only return a single value.
-- Now it returns two on the first load of a module, the path to it.
-- This fucks up loading components:
-- ```lua
-- div {
-- require "my/component", -- returns: true, "./my/component.lua".
-- require "my/component", -- returns true, nil. Just like Lua 5.3 and before
-- }
-- ```
--
-- TODO: This sucks in many ways and can easily break Lua 5.4 programs.
local major, minor = _VERSION:match("Lua (%d+).(%d+)")
if tonumber(major) >= 5 and tonumber(minor) >= 4 then
print("Lua 5.4+ detected, substituting 'require' function.")
local new_require = _G.require
local function old_require(modname)
local status, _ = new_require(modname)
return status
end
_G.require = old_require
end
function html_tag(name)
return function(data)
return { tag = name, data = data}
end
end
-- helpers
-- luatml_if(is_debug_enabled) { button "Show secrets" }
function html_if(exp)
return function(html)
if exp then return html end
return ''
end
end
-- luatml_ifelse(is_debug_enabled) { p "Using dev db" } { p "Using prod db" }
function html_ifelse(exp)
return function(html)
return function(htmlelse)
if exp then return html end
return htmlelse
end
end
end
-- luatml_foreach{"Foo", "Bar", "Baz"} { p "Found:", function(item, i, list) return p { item .. " at " .. i } end }
function html_foreach(list)
return function(html)
local result = {}
for i, listitem in ipairs(list) do
-- TODO: `html` could be anything, currently just a table is handled.
for j, tag in ipairs(html) do
if type(tag) == "function" then
-- TODO: deep search, or write "context"
-- to html tag. and during _tostring()
-- search for "nearest" context for functions.
table.insert(result, function ()
return tag(listitem, i, list)
end)
else
table.insert(result, tag)
end
end
end
return result
end
end
-- router
function html_route_parse(uri)
if type(uri) ~= 'string' then
return nil
end
local parsedUri = {}
local path, query = uri:match("([^?]*)%??(.*)")
-- Parse the path
parsedUri.path = {}
if path ~= nil then
for segment in path:gmatch("[^/]+") do
table.insert(parsedUri.path, segment)
end
end
-- Parse the query parameters
parsedUri.query = {}
if query ~= nil then
for key, value in query:gmatch("([^=&]+)=([^=&]*)") do
parsedUri.query[key] = tonumber(value) or value
end
end
return parsedUri
end
function html_route_match(path, path_index, routes)
local dir = path[path_index]
if dir == nil then
dir = "_"
end
local route = routes[dir]
if type(route) == "table" then
return html_route_match(path, path_index + 1, route)
end
return route
end
function html_router(request, routes)
if routes == nil then
return nil
elseif type(routes) ~= 'table' then
return nil
end
return html_route_match(request.uri.path, 1, routes)
end
-- api
function html_registertags(tags)
if tags == nil then
return html_registerdefaulttags()
end
for i, name in ipairs(tags) do
_G[name] = html_tag(name)
end
end
function html_registerdefaulttags()
html_registertags({
"html",
"head", "title", "meta", "style", "script", "link",
"body",
"div", "img",
"p", "span", "b", "i", "h1", "h2", "h3", "h4", "h5", "code",
"ul", "ol", "li", "a",
"form", "input", "button",
})
end
-- converts a `html_tag`-tree into plaintext HTML.
-- example:
--
-- local variable = 42
-- local page = html {
-- h1 "Hello World",
-- div {
-- p "Here is some data: ", variable, "!",
-- p {
-- "Danger",
-- class = "text-warning",
-- }
-- },
-- html_tag"script" [[
-- let name = "Chris";
-- alert(`Hi, i'm ${name}!`);
-- ]]
-- }
--
function html_tostring(html)
if type(html) == 'string' then
return html
elseif type(html) == 'number' then
return string.format("%g", html)
elseif type(html) == 'function' then
return html_tostring(html())
elseif type(html) == 'table' and html.tag == nil then
-- this is a normal array
result = ''
for index, v in ipairs(html) do
result = result .. html_tostring(v)
end
return result
elseif type(html) == 'table' and type(html.tag) == 'string' then
-- this is a html tag
-- attributes
local attrs = ''
if type(html.data) == 'table' then
for name, value in pairs(html.data) do
if type(name) ~= 'number' then
attrs = attrs .. ' '
local val = ''
if type(value) == 'string' then
val = value
elseif type(value) == 'number' then
val = tostring(value)
end
attrs = attrs .. string.format('%s="%s"', name, val)
end
end
end
-- tag itself
local result = string.format('<%s%s>', html.tag, attrs)
if type(html.data) == 'table' then
for tag, value in ipairs(html.data) do
result = result .. html_tostring(value)
end
elseif type(html.data) == 'string' then
result = result .. html.data
elseif type(html.data) == 'number' then
result = result .. tostring(html.data)
end
return result .. string.format('</%s>', html.tag)
end
return nil
end