2 FFLuCI - Template Parser
5 A template parser supporting includes, translations, Lua code blocks
6 and more. It can be used either as a compiler or as an interpreter.
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
26 module("ffluci.template", package.seeall)
28 require("ffluci.config")
29 require("ffluci.util")
31 require("ffluci.i18n")
32 require("ffluci.model.uci")
34 viewdir = ffluci.fs.dirname(ffluci.util.__file__()) .. "view/"
38 -- none: Never compile, only render precompiled
39 -- memory: Always compile, do not save compiled files, ignore precompiled
40 -- always: Same as "memory" but also saves compiled files
41 -- smart: Compile on demand, save compiled files, update precompiled
42 compiler_mode = "memory"
45 -- This applies to compiler modes "always" and "smart"
47 -- Produce compiled lua code rather than lua sourcecode
48 -- WARNING: Increases template size heavily!!!
49 -- This produces the same bytecode as luac but does not have a strip option
50 compiler_enable_bytecode = false
53 -- Define the namespace for template modules
55 translate = ffluci.i18n.translate,
56 config = ffluci.model.uci.get,
57 controller = os.getenv("SCRIPT_NAME"),
58 media = ffluci.config.mediaurlbase,
59 include = function(name) return render(name, getfenv(2)) end,
64 -- Compiles and builds a given template
65 function build(template, compiled)
66 local template = compile(ffluci.fs.readfile(template))
69 ffluci.fs.writefile(compiled, template)
76 -- Compiles a given template into an executable Lua module
77 function compile(template)
78 -- Search all <% %> expressions (remember: Lua table indexes begin with #1)
79 local function expr_add(command)
80 table.insert(expr, command)
81 return "<%" .. tostring(#expr) .. "%>"
84 -- As "expr" should be local, we have to assign it to the "expr_add" scope
86 ffluci.util.extfenv(expr_add, "expr", expr)
88 -- Save all expressiosn to table "expr"
89 template = template:gsub("<%%(.-)%%>", expr_add)
91 local function sanitize(s)
92 s = ffluci.util.escape(s)
93 s = ffluci.util.escape(s, "'")
94 s = ffluci.util.escape(s, "\n")
98 -- Escape and sanitize all the template (all non-expressions)
99 template = sanitize(template)
101 -- Template module header/footer declaration
102 local header = "write('"
105 template = header .. template .. footer
108 local r_include = "')\ninclude('%s')\nwrite('"
109 local r_i18n = "'..translate('%1','%2')..'"
110 local r_uci = "'..config('%1','%2','%3')..'"
111 local r_pexec = "'..%s..'"
112 local r_exec = "')\n%s\nwrite('"
114 -- Parse the expressions
115 for k,v in pairs(expr) do
116 local p = v:sub(1, 1)
119 re = r_include:format(sanitize(string.sub(v, 2)))
121 re = sanitize(v):gsub(":(.-) (.+)", r_i18n)
123 re = sanitize(v):gsub("~(.-)%.(.-)%.(.+)", r_uci)
125 re = r_pexec:format(string.sub(v, 2))
127 re = r_exec:format(v)
129 template = template:gsub("<%%"..tostring(k).."%%>", re)
132 if compiler_enable_bytecode then
133 tf = loadstring(template)
134 template = string.dump(tf)
141 -- Returns and builds the template for "name" depending on the compiler mode
143 local templatefile = viewdir .. name .. ".htm"
144 local compiledfile = viewdir .. name .. ".lua"
147 if compiler_mode == "smart" then
148 local tplmt = ffluci.fs.mtime(templatefile)
149 local commt = ffluci.fs.mtime(compiledfile)
151 -- Build if there is no compiled file or if compiled file is outdated
152 if ((commt == nil) and not (tplmt == nil))
153 or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
154 template = loadstring(build(templatefile, compiledfile))
156 template = loadfile(compiledfile)
159 elseif compiler_mode == "none" then
160 template = loadfile(compiledfile)
162 elseif compiler_mode == "memory" then
163 template = loadstring(build(templatefile))
165 elseif compiler_mode == "always" then
166 template = loadstring(build(templatefile, compiledfile))
169 error("Invalid compiler mode: " .. compiler_mode)
173 return template or error("Unable to load template: " .. name)
176 -- Renders a template
177 function render(name, scope)
178 scope = scope or getfenv(2)
180 -- Our template module
181 local view = get(name)
183 -- Put our predefined objects in the scope of the template
184 ffluci.util.updfenv(view, scope)
185 ffluci.util.updfenv(view, viewns)
187 -- Now finally render the thing