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 use precompiled data from files
39 -- memory: Always compile, do not save compiled files, ignore precompiled
40 -- file: Compile on demand, save compiled files, update precompiled
41 compiler_mode = "memory"
44 -- This applies to compiler modes "always" and "smart"
46 -- Produce compiled lua code rather than lua sourcecode
47 -- WARNING: Increases template size heavily!!!
48 -- This produces the same bytecode as luac but does not have a strip option
49 compiler_enable_bytecode = false
52 -- Define the namespace for template modules
54 translate = ffluci.i18n.translate,
55 config = ffluci.model.uci.get,
56 controller = os.getenv("SCRIPT_NAME"),
57 media = ffluci.config.mediaurlbase,
59 include = function(name) Template(name):render(getfenv(2)) end,
62 -- Compiles a given template into an executable Lua module
63 function compile(template)
64 -- Search all <% %> expressions (remember: Lua table indexes begin with #1)
65 local function expr_add(command)
66 table.insert(expr, command)
67 return "<%" .. tostring(#expr) .. "%>"
70 -- As "expr" should be local, we have to assign it to the "expr_add" scope
72 ffluci.util.extfenv(expr_add, "expr", expr)
74 -- Save all expressiosn to table "expr"
75 template = template:gsub("<%%(.-)%%>", expr_add)
77 local function sanitize(s)
78 s = ffluci.util.escape(s)
79 s = ffluci.util.escape(s, "'")
80 s = ffluci.util.escape(s, "\n")
84 -- Escape and sanitize all the template (all non-expressions)
85 template = sanitize(template)
87 -- Template module header/footer declaration
88 local header = "write('"
91 template = header .. template .. footer
94 local r_include = "')\ninclude('%s')\nwrite('"
95 local r_i18n = "'..translate('%1','%2')..'"
96 local r_uci = "'..config('%1','%2','%3')..'"
97 local r_pexec = "'..%s..'"
98 local r_exec = "')\n%s\nwrite('"
100 -- Parse the expressions
101 for k,v in pairs(expr) do
102 local p = v:sub(1, 1)
105 re = r_include:format(sanitize(string.sub(v, 2)))
107 re = sanitize(v):gsub(":(.-) (.+)", r_i18n)
109 re = sanitize(v):gsub("~(.-)%.(.-)%.(.+)", r_uci)
111 re = r_pexec:format(string.sub(v, 2))
113 re = r_exec:format(v)
115 template = template:gsub("<%%"..tostring(k).."%%>", re)
118 if compiler_enable_bytecode then
119 tf = loadstring(template)
120 template = string.dump(tf)
126 -- Oldstyle render shortcut
127 function render(name, scope, ...)
128 scope = scope or getfenv(2)
129 local s, t = pcall(Template, name)
131 error("Unable to load template: " .. name)
139 Template = ffluci.util.class()
141 -- Shared template cache to store templates in to avoid unnecessary reloading
145 -- Constructor - Reads and compiles the template on-demand
146 function Template.__init__(self, name)
147 if self.cache[name] then
148 self.template = self.cache[name]
153 -- Create a new namespace for this template
156 -- Copy over from general namespace
157 for k, v in pairs(viewns) do
161 -- If we have a cached template, skip compiling and loading
162 if self.template then
167 local sourcefile = viewdir .. name .. ".htm"
168 local compiledfile = viewdir .. name .. ".lua"
170 if compiler_mode == "file" then
171 local tplmt = ffluci.fs.mtime(sourcefile)
172 local commt = ffluci.fs.mtime(compiledfile)
174 -- Build if there is no compiled file or if compiled file is outdated
175 if ((commt == nil) and not (tplmt == nil))
176 or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
177 local compiled = compile(ffluci.fs.readfile(sourcefile))
178 ffluci.fs.writefile(compiledfile, compiled)
179 self.template = loadstring(compiled)
181 self.template = loadfile(compiledfile)
184 elseif compiler_mode == "none" then
185 self.template = loadfile(self.compiledfile)
187 elseif compiler_mode == "memory" then
188 self.template = loadstring(compile(ffluci.fs.readfile(sourcefile)))
191 error("Invalid compiler mode: " .. compiler_mode)
195 -- If we have no valid template throw error, otherwise cache the template
196 if not self.template then
197 error("Unable to load template: " .. name)
199 self.cache[name] = self.template
204 -- Renders a template
205 function Template.render(self, scope)
206 scope = scope or getfenv(2)
208 -- Save old environment
209 local oldfenv = getfenv(self.template)
211 -- Put our predefined objects in the scope of the template
212 ffluci.util.resfenv(self.template)
213 ffluci.util.updfenv(self.template, scope)
214 ffluci.util.updfenv(self.template, self.viewns)
216 -- Now finally render the thing
220 setfenv(self.template, oldfenv)