-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinit.lua
More file actions
213 lines (183 loc) · 5.39 KB
/
init.lua
File metadata and controls
213 lines (183 loc) · 5.39 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
local async = require 'async'
-- Base library
local html = {}
local tags = {
'a', 'abbr', 'acronym', 'address', 'area', 'b', 'base', 'bdo', 'big',
'blockquote', 'body', 'br', 'button', 'caption', 'cite', 'code', 'col',
'colgroup', 'dd', 'del', 'dfn', 'div', 'dl', 'DOCTYPE', 'dt', 'em',
'fieldset', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head',
'html', 'hr', 'i', 'img', 'input', 'ins', 'kbd', 'label', 'legend',
'li', 'link', 'map', 'meta', 'noscript', 'object', 'ol', 'optgroup',
'option', 'p', 'param', 'pre', 'q', 'samp', 'script', 'select',
'small', 'span', 'strong', 'style', 'sub', 'sup', 'table', 'tbody',
'td', 'textarea', 'tfoot', 'th', 'thead', 'title', 'tr', 'tt', 'ul', 'var'
}
local only1tags = { img = true, meta = true, link = true, input = true }
local entities = {
'nbsp', 'lt', 'gt', 'amp', 'cent', 'pound', 'yen', 'euro',
'copy', 'reg'
}
local function nopt(t)
local n = 0
for k, v in pairs(t) do n = n + 1 end
return n - #t
end
function maketag(tag, options, endtag, comment)
if endtag == nil then
endtag = string.format('</%s', tag)
end
comment = comment or false
local cdelim = '>'
if comment then cdelim = '' end
if type(options) == 'table' then
local nops = nopt(options)
local delim = ' '
if nops == 0 then delim = '' end
local rv = string.format('<%s%s', tag, delim)
delim = ' '
local n = 1
local inner = ''
for name, value in pairs(options) do
if type(name) == 'string' then
if n >= nops then delim = '' end
rv = rv .. string.format('%s="%s"%s', name, value, delim)
n = n + 1
elseif type(name) == 'number' then
inner = inner .. tostring(value)
n = 1
else
error('Invalid argument %s to %s', name, tag)
end
end
if only1tags[tag] then
return rv .. '>'
else
return string.format('%s%s%s%s>', rv, cdelim, inner, endtag)
end
elseif type(options) == 'string' then
return string.format('<%s%s%s%s>', tag, cdelim, options, endtag)
elseif type(options) == 'number' or type(options) == 'boolean' then
return maketag(tag, tostring(options))
else
local msg = '%s requires input of type table, string, number, or boolean'
error(string.format(msg, tag))
end
end
for _, tag in pairs(tags) do
html[tag] = function(inner)
return maketag(tag, inner)
end
end
for _, entity in pairs(entities) do
html[entity] = '&' .. entity .. ';'
end
html['br'] = '<br>'
function comment(inner)
return maketag('!--', inner, '--', true)
end
function doctype(inner)
return string.format('<!DOCTYPE %s>', inner)
end
-- Loops, conditionals
function DO(f, ...)
assert(type(f) == 'function', 'render requires input of type function')
return f(...)
end
function each(t, f)
if type(t) == 'string' then
t = torch.deserialize(t)
end
local rv = {}
for _, val in pairs(t) do
table.insert(rv, f(val))
end
return rv
end
function loop(t)
if type(t) == 'string' then
t = torch.deserialize(t)
end
return function(f)
local rv = {}
for key, value in pairs(t) do
table.insert(rv, f(key, value))
end
return rv
end
end
function IF(options)
local condition = options[1]
local truth = condition
if type(condition) == 'function' then
truth = condition()
end
if truth then
return options[2]
else
return options[3]
end
end
local function _baseconditional(inner)
if type(inner) == 'function' then
return inner()
else
return inner
end
end
THEN = _baseconditional
ELSE = _baseconditional
-- Blocks, for building template inheritance
local blocks = {}
function defblock(name)
assert(type(name) == 'string', 'defblock requires input of type string')
blocks[name] = true
return string.format('${block-%s}', name)
end
function block(base, name)
assert(type(base) == 'string' and type(name) == 'string',
'block requires input of type string and string')
if blocks[name] then
return function(inner)
if type(inner) == 'table' then
inner = table.concat(inner, '')
end
assert(type(inner) == 'string',
'block closure requires input of type string or table')
return base % {
[string.format('block-%s', name)] = inner
}
end
else
error(string.format(
'No Block defined with name "%s". Call defblock first.', name))
end
end
function extends(path)
assert(type(path) == 'string', 'extends requires input of type string')
local fi = io.open(path)
local content = fi:read('*all')
local rv = loadstring(content % _G.renderargs)()
fi:close()
return rv
end
-- Override string.__mod for easy variable rendering
getmetatable('').__mod = function(str, tab)
-- http://lua-users.org/wiki/StringInterpolation
-- ex: print('${name} is ${value}' % {name='foo', value='bar'})
return (str:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end))
end
-- Async Rendering
function render(path, args, callback)
for key, val in pairs(args) do
if type(val) == 'table' then
args[key] = torch.serialize(val)
end
end
_G.renderargs = args
async.fs.readFile(path, function(content)
callback(loadstring(content % args)())
_G.renderargs = nil
end)
async.go()
end
return html