-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuaTML.lua
428 lines (376 loc) · 13.5 KB
/
LuaTML.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
-- Prevent Prompt changing on REPL
_PROMPT = _ENV["_PROMT"] or "> "
local selfClosableTags = {
area = true,
base = true,
br = true,
col = true,
command = true,
embed = true,
hr = true,
img = true,
input = true,
keygen = true,
link = true,
meta = true,
param = true,
source = true,
track = true,
wbr = true,
}
-- To simplify logic, childdrens of extended elements will share this metatable
-- this will allow direct detect and allows new plain div syntax
local sharedMetatable = {}
-- In HTML we need encode certains characters
local function toHTMLEncode(v)
return tostring(v):gsub("&", "&"):gsub("<", "<"):gsub(">", ">"):gsub('"', """):gsub("'", "'")
end
-- Generic function to serialize tag childrens
local function serializeChildrens(self)
local result = ""
for i, element in ipairs(self) do
result = result..tostring(element)
end
return result
end
local function serializeProperties(properties)
local HTML = ""
-- Let's ensure that property order is same across builds
-- this avoids commit pollution
local propertiesList = {}
for property in pairs(properties) do
propertiesList[#propertiesList+1] = property
end
table.sort(propertiesList)
for i, property in ipairs(propertiesList) do
local value = properties[property]
HTML = HTML..property..'="'..toHTMLEncode(tostring(value))..'" '
end
return (HTML == "" and "" or " ")..HTML
end
local function processChildren(element,properties)
if getmetatable(element) == sharedMetatable then
-- We get a children of extended elements
local bindings = element.bindings
local elementWrapped = element.element
local require
for childrenProperty, parentProperty in pairs(bindings) do
if type(childrenProperty) ~= "number" then
if properties[parentProperty] ~= nil then
rawget(elementWrapped,"__lua4webapps_properties")[childrenProperty] = tostring(properties[parentProperty])
end
else
local childrens = rawget(elementWrapped,"__lua4webapps_childrens")
for i = 1, childrenProperty, 1 do
childrens[i] =childrens[i] or ""
end
if properties[parentProperty] ~= nil then
childrens[childrenProperty] = tostring(properties[parentProperty])
end
end
end
return elementWrapped
end
if type(element) == "table" and getmetatable(element) == nil then
-- we got a plain div , let's create a custom element
local newDiv = _ENV[{"div"}]
for key, value in pairs(element) do
newDiv[key] = value
end
return newDiv
end
return element
end
local function destructure(self)
local childrens = {}
local properties = {}
-- To allow extendable tags we need clone properties and childrens
for _, scope in ipairs {rawget(self,"__lua4webapps_hardcodeProperties"),rawget(self,"__lua4webapps_properties")} do
for property, value in pairs(scope) do
properties[property] = properties[property] == nil and value or properties[property].." "..value
end
end
for _, scope in ipairs {
(rawget(self,"__lua4webapps_hardcodeChildrens") or {}).first or {}, -- Retrocompatibility
(rawget(self,"__lua4webapps_hardcodeChildrens") or {}),
(rawget(self,"__lua4webapps_childrens") or {}).first or {},
(rawget(self,"__lua4webapps_childrens") or {}),
(rawget(self,"__lua4webapps_childrens") or {}).last or {},
(rawget(self,"__lua4webapps_hardcodeChildrens") or {}).last or {}
} do
for _, children in ipairs(scope) do
childrens[#childrens+1] = processChildren(children,properties)
end
end
return childrens,properties
end
-- lets avoid redeclare functions
local elementCommonMetatableEvents = {
__newindex =
function (self,k,v)
if type(k == "number") then
local childrens = rawget(self,"__lua4webapps_childrens")
childrens[#childrens+1] = v
return
end
local properties = rawget(self,"__lua4webapps_properties")
properties[k] = v
end
,
__mul =
function (self,n)
local block = {}
for i = 1, n do
block[i] = self
end
return setmetatable(block, {__tostring = serializeChildrens})
end
;
__pow =
function (self,items)
local block = {}
items = type(items) == "table" and items or {items}
for i, item in ipairs(items) do
local element = {tag = self.tag,properties = {}}
for property, value in pairs(self.properties or {}) do
element.properties[property] = value
end
element = setmetatable(element,getmetatable(self))
element.properties[1] = tostring(item)
block[#block+1] = element
end
return setmetatable(block, {__tostring = setmetatable(block, {__tostring = serializeChildrens})})
end
;
__tostring =
function (self)
local tagName = rawget(self,"__lua4webapps_tagName")
local childrens,properties = destructure(self)
local HTML = "<"..tagName..serializeProperties(properties)..((#childrens == 0 and selfClosableTags[tagName]) and "" or ">\n")
HTML = HTML..serializeChildrens(childrens or {})
if #childrens == 0 and selfClosableTags[tagName] then
return HTML:gsub(" $","").."/>\n"
end
if tagName == "body" then
return HTML..("</"..tagName..">")
end
return HTML..("\n</"..tagName..">\n")
end
;
__call =
function (self,t)
t = type(t) == "table" and t or {tostring(t)}
for key, value in pairs(t) do
if type(key) ~= "number" then
rawset(rawget(self,"__lua4webapps_properties"),key,value)
else
rawset(rawget(self,"__lua4webapps_childrens"),key,value)
end
end
return self
end
;
}
-- Some elements must have a special __tostring, __newindex and __call for convenience
local elementSpecificMetatableEvents = {
html = {
__tostring =
function (self)
return "<!DOCTYPE html>\n"..elementCommonMetatableEvents.__tostring(self)
end
;
__call =
function (self,t)
__HTML__ = tostring(elementCommonMetatableEvents.__call(self,t))
return __HTML__
end
;
},
table = {
__newindex =
function (self,k,v)
if type(v) == "table" and getmetatable(v) == nil then
v = tr(v)
end
elementCommonMetatableEvents.__newindex(self,k,v)
end
;
__call =
function (self,t)
for i, element in ipairs(t) do
if type(element) == "table" and getmetatable(element) == nil then
t[i] = tr(element)
end
end
return elementCommonMetatableEvents.__call(self,t)
end
,
},
tr = {
__newindex =
function (self,k,v)
if type(v) == "table" and getmetatable(v) == nil then
v = td(v)
end
elementCommonMetatableEvents.__newindex(self,k,v)
end
;
__call =
function (self,t)
for i, element in ipairs(t) do
if type(element) ~= "table" then
t[i] = td(element)
end
end
return (elementCommonMetatableEvents.__call(self,t))
end
,
},
select = {
__newindex =
function (self,k,v)
if type(k) =="number" then
if type(v) == "table" and getmetatable(v) == nil then
v = option {value = tostring(v[2]),tostring(v[1])}
elseif (type(v) == "table" or type(v) == "object") and getmetatable(v) then
v = v
else
v = option {value = tostring(v),tostring(v)}
end
end
elementCommonMetatableEvents.__newindex(self,k,v)
end
,
__call =
function (self,t)
for i, element in ipairs(t) do
if type(element) == "table" and getmetatable(element) == nil then
t[i] = option {value = tostring(element[2]),tostring(element[1])}
elseif (type(element) == "table" or type(element) == "object") and getmetatable(element) then
t[i] = element
else
t[i] = option {value = tostring(element),tostring(element)}
end
end
return (elementCommonMetatableEvents.__call(self,t))
end
,
},
}
local function processExtendedTagChildren(children)
if #children == 0 and (type(children) == "table" and children.element or false) and getmetatable(children) == nil then
setmetatable(children,sharedMetatable)
end
return children
end
local function extends(self,descriptor)
local element = _ENV[{rawget(self,"__lua4webapps_tagName")}] -- create a dummy element
local childrens = descriptor.childrens or {}
local hardcodeProperties = (rawget(self,"__lua4webapps_hardcodeProperties") or {}).first or {}
local hardcodeChildrensFirst = (rawget(self,"__lua4webapps_hardcodeChildrens") or {}).first or {}
local hardcodeChildrens = (rawget(self,"__lua4webapps_hardcodeChildrens") or {})
local hardcodeChildrensLast = (rawget(self,"__lua4webapps_hardcodeChildrens") or {}).last or {}
local newHardcodeProperties = {}
local newHardcodeChildrensFirst = {}
local newHardcodeChildrens = {}
local newHardcodeChildrensLast = {}
descriptor.childrens = nil
-- Merge hardcoded properties
for _, scope in ipairs {hardcodeProperties,descriptor} do
for key, value in pairs(scope) do
newHardcodeProperties[key] = newHardcodeProperties[key] == nil and value or newHardcodeProperties[key].." "..value
end
end
-- Merge hardcoded head childrens
for _, scope in ipairs {hardcodeChildrensFirst or {},childrens.first or {}} do
for _,children in ipairs(scope) do
newHardcodeChildrensFirst[#newHardcodeChildrensFirst+1] = processExtendedTagChildren(children)
end
end
--Merge hardcoded body childrens (syntatic sugar for first)
for _, scope in ipairs {hardcodeChildrens or {},childrens or {}} do
for _,children in ipairs(scope) do
newHardcodeChildrens[#newHardcodeChildrens+1] = processExtendedTagChildren(children)
end
end
-- Merge hardcoded footer childrens
for _, scope in ipairs {childrens.last or {},hardcodeChildrensLast or {}} do
for _,children in ipairs(scope) do
newHardcodeChildrensLast[#newHardcodeChildrensLast+1] = processExtendedTagChildren(children)
end
end
newHardcodeChildrens.first = newHardcodeChildrensFirst or {}
newHardcodeChildrens.last = newHardcodeChildrensLast or {}
rawset(element,"__lua4webapps_hardcodeProperties",newHardcodeProperties)
rawset(element,"__lua4webapps_hardcodeChildrens",newHardcodeChildrens)
return element
end
-- We need to backup select and remove them
local luaSelect,luaTable,luaString,luaMath,luadebug = select,table,string,math,debug
select,table,string,math,debug = nil,nil,nil,nil,nil
local function createElement(tag)
tag = tostring(tag):lower()
local element = setmetatable({
__lua4webapps_tagName = tag,
__lua4webapps_properties = {},
__lua4webapps_hardcodeProperties = {},
__lua4webapps_hardcodeChildrens = {},
__lua4webapps_childrens = {},
extends = extends,
},{
__newindex = (elementSpecificMetatableEvents[tag] or {}).__newindex or elementCommonMetatableEvents.__newindex;
__name = "table", -- Iasy support
__mul = elementCommonMetatableEvents.__mul,
__pow = elementCommonMetatableEvents.__pow,
__tostring = (elementSpecificMetatableEvents[tag] or {}).__tostring or elementCommonMetatableEvents.__tostring,
__call = (elementSpecificMetatableEvents[tag] or {}).__call or elementCommonMetatableEvents.__call;
})
if tag == "select" then
-- Select in lua is already declared a function,
-- since we want to keep Lua untouched, is need
-- to encapsulate then
local elementMetatable = getmetatable(element)
function elementMetatable.__call(self,...)
if type(({...})[1]) == "number" then
return (luaSelect)(...)
end
return elementSpecificMetatableEvents.select.__call(self,...)
end
return element
end
if tag == "table" then
getmetatable(element).__index = luaTable
return element
end
if tag == "string" then
getmetatable(element).__index = luaString
return element
end
if tag == "math" then
getmetatable(element).__index = luaMath
return element
end
if tag == "debug" then
getmetatable(element).__index = luadebug
return element
end
return element
end
local envMetatable = {
__index =
function (self,k)
-- if something doesn't exists, return an HTML element
return rawget(self,k) or createElement(type(k)=="table" and table.concat(k) or tostring(k))
end
;
}
-- Let's do the trick
setmetatable(_ENV,envMetatable)
-- For convenience let's reduce head tag boilerplate
head = head:extends {
childrens = {
meta {charset="UTF-8"},
meta {content="width=device-width,initial-scale=1.0", name="viewport"},
style "body {font-family: sans-serif;}",
}
}