ltp/template.lua

Sun, 06 Aug 2023 14:32:25 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 06 Aug 2023 14:32:25 +0300
changeset 42
260f867d46c0
parent 6
219d7a7304f8
permissions
-rwxr-xr-x

Preliminary breadcrumb support

3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 --
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 -- Copyright 2007-2008 Savarese Software Research Corporation.
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 --
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4 -- Licensed under the Apache License, Version 2.0 (the "License");
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 -- you may not use this file except in compliance with the License.
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 -- You may obtain a copy of the License at
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 --
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 -- http://www.savarese.com/software/ApacheLicense-2.0
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 --
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 -- Unless required by applicable law or agreed to in writing, software
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 -- distributed under the License is distributed on an "AS IS" BASIS,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 -- See the License for the specific language governing permissions and
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 -- limitations under the License.
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 --
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 local ltp = require('ltp.util')
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 local function compile_template_to_table(result, data, start_lua, end_lua)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 local LF, CR, EQ = 10, 13, 61
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 local i1, i2, i3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 i3 = 1
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 repeat
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 i2, i1 = data:find(start_lua, i3, true)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 if i2 then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 if i3 < i2 then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 table.insert(result, "table.insert(output,")
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 table.insert(result, string.format('%q', data:sub(i3, i2 - 1)))
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 table.insert(result, ");")
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 i1 = i1 + 2
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 i2, i3 = data:find(end_lua, i1, true)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 if i2 then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 if data:byte(i1-1) == EQ then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 table.insert(result, "table.insert(output,")
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 table.insert(result, data:sub(i1, i2 - 1))
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 table.insert(result, ");")
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 i3 = i3 + 1
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 table.insert(result, data:sub(i1, i2 - 1))
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 i3 = i3 + 1
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 if data:byte(i3) == LF then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 i3 = i3 + 1
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 elseif data:byte(i3) == CR and data:byte(i3+1) == LF then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 i3 = i3 + 2
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 elseif i3 <= #data then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 table.insert(result, "table.insert(output,")
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 table.insert(result, string.format('%q', data:sub(i3)))
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 table.insert(result, ");")
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 until not i2
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 return result
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 local function compile_template_as_function(data, start_lua, end_lua)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 local result = { "return function(output) " }
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66 table.insert(compile_template_to_table(result, data, start_lua, end_lua),
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 "end")
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 return table.concat(result)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70
6
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
71 local function compile_template_as_chunk(data, start_lua, end_lua)
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
72 local result = { "local output = ... " }
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 return
6
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
74 table.concat(compile_template_to_table(result, data, start_lua, end_lua))
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
75 end
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
76
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
77 local function compile_template(data, start_lua, end_lua)
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
78 return table.concat(compile_template_to_table({ }, data, start_lua, end_lua))
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
79 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 local function load_template(data, start_lua, end_lua)
6
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
82 return assert(load(compile_template_as_chunk(data, start_lua, end_lua),
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
83 "=(load)"))
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86 local function execute_template(template, environment, output)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 setfenv(template, environment)(output)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
90 local function basic_environment(merge_global, environment)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91 if not environment then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 environment = { }
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
94
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95 if merge_global then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
96 ltp.merge_index(environment, _G)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
98 environment.table = table
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
99 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 return environment
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
103
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 local function load_environment(env_files, merge_global)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 local environment = nil
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
106
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 if env_files and #env_files > 0 then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
108 for i = 1,#env_files,1 do
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109 local efun = assert(loadfile(env_files[i]))
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
110
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
111 if i > 1 then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
112 environment = ltp.merge_table(setfenv(efun, environment)(), environment)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
113 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
114 environment = basic_environment(merge_global, efun())
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
115 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
116 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
117 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
118 environment = basic_environment(merge_global)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
119 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
120
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 return environment
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
122 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
123
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
124 local function read_template(template)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
125 return ((template == "-" and io.stdin:read("*a")) or ltp.read_all(template))
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
127
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
128 local function render_template(template_data, start_lua, end_lua, environment)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
129 local rfun = load_template(template_data, start_lua, end_lua)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
130 local output = { }
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
131 execute_template(rfun, environment, output)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
132 return table.concat(output)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
133 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
134
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
135 local function execute_env_code(env_code, environment)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
136 for i = 1,#env_code do
6
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
137 local fun, emsg = load(env_code[i])
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
138
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
139 if fun then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
140 setfenv(fun, environment)()
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
141 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
142 error("error loading " .. env_code[i] .. "\n" .. emsg)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
143 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
144 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
146
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
147 local function render(outfile, num_passes, template, merge_global,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
148 env_files, start_lua, end_lua, env_code)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
149 local data = assert(read_template(template), "error reading " .. template)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
150 local environment = load_environment(env_files, merge_global)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
151
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152 execute_env_code(env_code, environment)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
154 if num_passes > 0 then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
155 for i = 1,num_passes do
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
156 data = render_template(data, start_lua, end_lua, environment)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
158 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
159 -- Prevent an infinite loop by capping expansion to 100 times.
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160 num_passes = 1
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161 repeat
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
162 data = render_template(data, start_lua, end_lua, environment)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163 num_passes = num_passes + 1
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164 until data:find(start_lua, 1, true) == nil or num_passes >= 100
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167 outfile:write(data);
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
168 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
169
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
170 local function compile_as_function(outfile, template, start_lua, end_lua)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
171 local data = read_template(template)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172 outfile:write(compile_template_as_function(data, start_lua, end_lua))
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
173 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
175 local function compile(outfile, template, start_lua, end_lua)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
176 local data = read_template(template)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
177 outfile:write(compile_template(data, start_lua, end_lua))
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
178 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
179
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
180 return ltp.merge_table(
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
181 {
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
182 compile_template_to_table = compile_template_to_table,
6
219d7a7304f8 5.3 updates
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
183 compile_template_as_chunk = compile_template_as_chunk,
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
184 compile_template_as_function = compile_template_as_function,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
185 compile_template = compile_template,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
186 load_template = load_template,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
187 execute_template = execute_template,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
188 basic_environment = basic_environment,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
189 load_environment = load_environment,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
190 render_template = render_template,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
191 execute_env_code = execute_env_code,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
192 render = render,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
193 compile_as_function = compile_as_function,
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
194 compile = compile
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
195 },
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
196 ltp
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197 )

mercurial