-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmpl.lua
65 lines (61 loc) · 1.55 KB
/
tmpl.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
return function(str)
local t = {[[
local ctx = ...
local function _____()
local _ENV = setmetatable(ctx, { __index = _ENV })
local function echo(s) if s ~= '' then coroutine.yield(s) end end
local function include(template, ctx)
if ctx then
ctx.escape = ctx.escape or _ENV.escape
else
ctx = _ENV
end
for s in template(ctx) do coroutine.yield(s) end
end
_ENV.escape = escape or function(i)
return tostring(i or ''):gsub('[&<>"\'/]', {
['&'] = '&',
['<'] = '<',
['>'] = '>',
['"'] = '"',
["'"] = ''',
['/'] = '/'
})
end
]] }
local function f(pos)
local start, stop, c = pos - 1
repeat
start, stop = str:find('%b{}', start + 1)
if not start then
table.insert(t, ' echo([=====[\n')
table.insert(t, str:sub(pos, #str))
table.insert(t, ']=====]) ')
return
end
c = str:sub(start + 1, start + 1)
until c:match('[{%*%[%%!]')
table.insert(t, ' echo([=====[\n')
table.insert(t, str:sub(pos, start - 1))
table.insert(t, ']=====]) ')
if c == '{' then
table.insert(t, ' echo(escape(')
table.insert(t, str:sub(start + 2, stop - 2))
table.insert(t, ')) ')
elseif c == '*' then
table.insert(t, ' echo(')
table.insert(t, str:sub(start + 2, stop - 2))
table.insert(t, ') ')
elseif c == '[' then
table.insert(t, ' include(')
table.insert(t, str:sub(start + 2, stop - 2))
table.insert(t, ') ')
elseif c == '%' then
table.insert(t, str:sub(start + 2, stop - 2))
end
f(stop + 1)
end
f(1)
table.insert(t, [[ end return coroutine.wrap(_____) ]])
return load(table.concat(t))
end