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