-
Notifications
You must be signed in to change notification settings - Fork 1
/
etlua.lua
401 lines (398 loc) · 10.4 KB
/
etlua.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
-- from https://github.com/leafo/etlua
-- MIT license Copyright (C) 2014 by Leaf Corcoran
-- Modified by Jesse Rusak to remove debug library use
-- See link above for full usage. Examples from there:
--[[
local etlua = require "etlua"
local template = etlua.compile("my template <%= foo %>")
template({foo = "something"})
]]
-- Other examples:
--[[
Hello <%= name %>,
Here are your items:
<% for i, item_raw_html in pairs(items) do %>
* <%- item_raw_html -%>
<% end %>
]]
local VERSION = "1.3.0-jder"
local insert, concat
do
local _obj_0 = table
insert, concat = _obj_0.insert, _obj_0.concat
end
local load, assert, type, error, tostring, tonumber, setmetatable
do
local _obj_0 = _G
load, assert, type, error, tostring, tonumber, setmetatable = _obj_0.load, _obj_0.assert, _obj_0.type, _obj_0.error, _obj_0.tostring, _obj_0.tonumber, _obj_0.setmetatable
end
local html_escape_entities = {
['&'] = '&',
['<'] = '<',
['>'] = '>',
['"'] = '"',
["'"] = '''
}
local html_escape
html_escape = function(str)
return (str:gsub([=[["><'&]]=], html_escape_entities))
end
local get_line
get_line = function(str, line_num)
for line in str:gmatch("([^\n]*)\n?") do
if line_num == 1 then
return line
end
line_num = line_num - 1
end
end
local pos_to_line
pos_to_line = function(str, pos)
local line = 1
for _ in str:sub(1, pos):gmatch("\n") do
line = line + 1
end
return line
end
local Compiler
do
local _class_0
local _base_0 = {
html_escape = true,
render = function(self)
return table.concat(self.buffer)
end,
push = function(self, str, ...)
local i = self.i + 1
self.buffer[i] = str
self.i = i
if ... then
return self:push(...)
end
end,
header = function(self)
return self:push("local _tostring, _escape, _b, _b_i = ...\n")
end,
footer = function(self)
return self:push("return _b")
end,
increment = function(self)
return self:push("_b_i = _b_i + 1\n")
end,
mark = function(self, pos)
return self:push("--[[", tostring(pos), "]] ")
end,
assign = function(self, ...)
self:push("_b[_b_i] = ", ...)
if ... then
return self:push("\n")
end
end,
compile_chunks = function(self, chunks)
self:header()
for _index_0 = 1, #chunks do
local chunk = chunks[_index_0]
local t = type(chunk)
if t == "table" then
t = chunk[1]
end
local _exp_0 = t
if "string" == _exp_0 then
self:increment()
self:assign(("%q"):format(chunk))
elseif "code" == _exp_0 then
self:mark(chunk[3])
self:push(chunk[2], "\n")
elseif "=" == _exp_0 or "-" == _exp_0 then
self:increment()
self:mark(chunk[3])
self:assign()
if t == "=" and self.html_escape then
self:push("_escape(_tostring(", chunk[2], "))\n")
else
self:push("_tostring(", chunk[2], ")\n")
end
else
error("unknown type " .. tostring(t))
end
end
self:footer()
return self:render()
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self)
self.buffer = { }
self.i = 0
end,
__base = _base_0,
__name = "Compiler"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Compiler = _class_0
end
local Parser
do
local _class_0
local _base_0 = {
open_tag = "<%",
close_tag = "%>",
modifiers = "^[=-]",
next_tag = function(self)
local start, stop = self.str:find(self.open_tag, self.pos, true)
if not (start) then
self:push_raw(self.pos, #self.str)
return false
end
if not (start == self.pos) then
self:push_raw(self.pos, start - 1)
end
self.pos = stop + 1
local modifier
if self.str:match(self.modifiers, self.pos) then
do
local _with_0 = self.str:sub(self.pos, self.pos)
self.pos = self.pos + 1
modifier = _with_0
end
end
local close_start, close_stop = self.str:find(self.close_tag, self.pos, true)
if not (close_start) then
return nil, self:error_for_pos(start, "failed to find closing tag")
end
while self:in_string(self.pos, close_start) do
close_start, close_stop = self.str:find(self.close_tag, close_stop, true)
if not (close_start) then
return nil, self:error_for_pos(start, "failed to find string close")
end
end
local trim_newline
if "-" == self.str:sub(close_start - 1, close_start - 1) then
close_start = close_start - 1
trim_newline = true
end
self:push_code(modifier or "code", self.pos, close_start - 1)
self.pos = close_stop + 1
if trim_newline then
do
local match = self.str:match("^\n", self.pos)
if match then
self.pos = self.pos + #match
end
end
end
return true
end,
in_string = function(self, start, stop)
local in_string = false
local end_delim = nil
local escape = false
local pos = 0
local skip_until = nil
local chunk = self.str:sub(start, stop)
for char in chunk:gmatch(".") do
local _continue_0 = false
repeat
pos = pos + 1
if skip_until then
if pos <= skip_until then
_continue_0 = true
break
end
skip_until = nil
end
if end_delim then
if end_delim == char and not escape then
in_string = false
end_delim = nil
end
else
if char == "'" or char == '"' then
end_delim = char
in_string = true
end
if char == "[" then
do
local lstring = chunk:match("^%[=*%[", pos)
if lstring then
local lstring_end = lstring:gsub("%[", "]")
local lstring_p1, lstring_p2 = chunk:find(lstring_end, pos, true)
if not (lstring_p1) then
return true
end
skip_until = lstring_p2
end
end
end
end
escape = char == "\\"
_continue_0 = true
until true
if not _continue_0 then
break
end
end
return in_string
end,
push_raw = function(self, start, stop)
return insert(self.chunks, self.str:sub(start, stop))
end,
push_code = function(self, kind, start, stop)
return insert(self.chunks, {
kind,
self.str:sub(start, stop),
start
})
end,
compile = function(self, str)
local success, err = self:parse(str)
if not (success) then
return nil, err
end
local fn, env_holder
env_holder = {}
fn, err = self:load(self:chunks_to_lua(), nil, env_holder)
if not (fn) then
return nil, err
end
return function(env, ...)
local buffer
setmetatable(env_holder, {__index = function(t, k) return env[k] or _G[k] end, __newindex = env})
buffer, err = self:run(fn, ...)
if buffer then
return concat(buffer)
else
return nil, err
end
end
end,
parse = function(self, str)
self.str = str
assert(type(self.str) == "string", "expecting string for parse")
self.pos = 1
self.chunks = { }
while true do
local found, err = self:next_tag()
if err then
return nil, err
end
if not (found) then
break
end
end
return true
end,
parse_error = function(self, err, code)
local line_no, err_msg = err:match("%[.-%]:(%d+): (.*)$")
line_no = tonumber(line_no)
if not (line_no) then
return
end
local line = get_line(code, line_no)
local source_pos = tonumber(line:match("^%-%-%[%[(%d+)%]%]"))
if not (source_pos) then
return
end
return self:error_for_pos(source_pos, err_msg)
end,
error_for_pos = function(self, source_pos, err_msg)
local source_line_no = pos_to_line(self.str, source_pos)
local source_line = get_line(self.str, source_line_no)
return tostring(err_msg) .. " [" .. tostring(source_line_no) .. "]: " .. tostring(source_line)
end,
load = function(self, code, name, env)
if name == nil then
name = "etlua"
end
local code_fn
do
local code_ref = code
code_fn = function()
do
local ret = code_ref
code_ref = nil
return ret
end
end
end
local fn, err = load(code_fn, name, "t", env)
if not (fn) then
do
local err_msg = self:parse_error(err, code)
if err_msg then
return nil, err_msg
end
end
return nil, err
end
return fn
end,
run = function(self, fn, env, buffer, i, ...)
if not (buffer) then
buffer = { }
i = 0
end
return fn(tostring, html_escape, buffer, i, ...)
end,
compile_to_lua = function(self, str, ...)
local success, err = self:parse(str)
if not (success) then
return nil, err
end
return self:chunks_to_lua(...)
end,
chunks_to_lua = function(self, compiler_cls)
if compiler_cls == nil then
compiler_cls = Compiler
end
return compiler_cls():compile_chunks(self.chunks)
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function() end,
__base = _base_0,
__name = "Parser"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Parser = _class_0
end
local compile
do
local _base_0 = Parser()
local _fn_0 = _base_0.compile
compile = function(...)
return _fn_0(_base_0, ...)
end
end
local render
render = function(str, ...)
local fn, err = compile(str)
if fn then
return fn(...)
else
return nil, err
end
end
return {
compile = compile,
render = render,
Parser = Parser,
Compiler = Compiler,
_version = VERSION
}