summaryrefslogtreecommitdiffhomepage
path: root/libs/web/luasrc/template.lua
blob: dc0ccf6201895b1329a3860cb582dfe42f234636 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
--[[
LuCI - Template Parser

Description:
A template parser supporting includes, translations, Lua code blocks
and more. It can be used either as a compiler or as an interpreter.

FileId: $Id$

License:
Copyright 2008 Steven Barth <steven@midlink.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

]]--
module("luci.template", package.seeall)

require("luci.config")
require("luci.util")
require("luci.fs")
require("luci.http")

luci.config.template = luci.config.template or {}

viewdir    = luci.config.template.viewdir or luci.sys.libpath() .. "/view"
compiledir = luci.config.template.compiledir or luci.sys.libpath() .. "/view"


-- Compile modes:
-- none:	Never compile, only use precompiled data from files
-- memory:	Always compile, do not save compiled files, ignore precompiled 
-- file:	Compile on demand, save compiled files, update precompiled
compiler_mode = luci.config.template.compiler_mode or "memory"


-- Define the namespace for template modules
context = luci.util.threadlocal()

viewns = {
	include    = function(name) Template(name):render(getfenv(2)) end,
}

-- Compiles a given template into an executable Lua module
function compile(template)	
	-- Search all <% %> expressions (remember: Lua table indexes begin with #1)
	local function expr_add(ws1, skip1, command, skip2, ws2)
		table.insert(expr, command)
		return ( #skip1 > 0 and "" or ws1 ) .. 
		       "<%" .. tostring(#expr) .. "%>" ..
		       ( #skip2 > 0 and "" or ws2 )
	end
	
	-- As "expr" should be local, we have to assign it to the "expr_add" scope 
	local expr = {}
	luci.util.extfenv(expr_add, "expr", expr)
	
	-- Save all expressiosn to table "expr"
	template = template:gsub("(%s*)<%%(%-?)(.-)(%-?)%%>(%s*)", expr_add)
	
	local function sanitize(s)
		s = luci.util.escape(s)
		s = luci.util.escape(s, "'")
		s = luci.util.escape(s, "\n")
		return s
	end
	
	-- Escape and sanitize all the template (all non-expressions)
	template = sanitize(template)

	-- Template module header/footer declaration
	local header = "write('"
	local footer = "')"
	
	template = header .. template .. footer
	
	-- Replacements
	local r_include = "')\ninclude('%s')\nwrite('"
	local r_i18n    = "'..translate('%1','%2')..'"
	local r_i18n2    = "'..translate('%1', '')..'"
	local r_pexec   = "'..(%s or '')..'"
	local r_exec    = "')\n%s\nwrite('"
	
	-- Parse the expressions
	for k,v in pairs(expr) do
		local p = v:sub(1, 1)
		v = v:gsub("%%", "%%%%")
		local re = nil
		if p == "+" then
			re = r_include:format(sanitize(string.sub(v, 2)))
		elseif p == ":" then
			if v:find(" ") then
				re = sanitize(v):gsub(":(.-) (.*)", r_i18n)
			else
				re = sanitize(v):gsub(":(.+)", r_i18n2)
			end
		elseif p == "=" then
			re = r_pexec:format(v:sub(2))
		elseif p == "#" then
			re = ""
		else
			re = r_exec:format(v)
		end
		template = template:gsub("<%%"..tostring(k).."%%>", re)
	end

	return loadstring(template)
end

-- Oldstyle render shortcut
function render(name, scope, ...)
	scope = scope or getfenv(2)
	local s, t = luci.util.copcall(Template, name)
	if not s then
		error(t)
	else
		t:render(scope, ...)
	end
end


-- Template class
Template = luci.util.class()

-- Shared template cache to store templates in to avoid unnecessary reloading
Template.cache = {}
setmetatable(Template.cache, {__mode = "v"})


-- Constructor - Reads and compiles the template on-demand
function Template.__init__(self, name)	
	self.template = self.cache[name]
	
	-- Create a new namespace for this template
	self.viewns = {}
	
	-- Copy over from general namespace
	luci.util.update(self.viewns, viewns)
	if context.viewns then
		luci.util.update(self.viewns, context.viewns)
	end
	
	-- If we have a cached template, skip compiling and loading
	if self.template then
		return
	end
	
	-- Enforce cache security
	local cdir = compiledir .. "/" .. luci.sys.process.info("uid")
	
	-- Compile and build
	local sourcefile   = viewdir    .. "/" .. name .. ".htm"
	local compiledfile = cdir .. "/" .. luci.http.urlencode(name) .. ".lua"
	local err	
	
	if compiler_mode == "file" then
		local tplmt = luci.fs.mtime(sourcefile)
		local commt = luci.fs.mtime(compiledfile)
		
		if not luci.fs.mtime(cdir) then
			luci.fs.mkdir(cdir, true)
			luci.fs.chmod(luci.fs.dirname(cdir), "a+rxw")
		end
				
		-- Build if there is no compiled file or if compiled file is outdated
		if ((commt == nil) and not (tplmt == nil))
		or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
			local source
			source, err = luci.fs.readfile(sourcefile)
			
			if source then
				local compiled, err = compile(source)
				
				luci.fs.writefile(compiledfile, luci.util.get_bytecode(compiled))
				self.template = compiled
			end
		else
			self.template, err = loadfile(compiledfile)
		end
		
	elseif compiler_mode == "none" then
		self.template, err = loadfile(self.compiledfile)
		
	elseif compiler_mode == "memory" then
		local source
		source, err = luci.fs.readfile(sourcefile)
		if source then
			self.template, err = compile(source)
		end
			
	end
	
	-- If we have no valid template throw error, otherwise cache the template
	if not self.template then
		error(err)
	else
		self.cache[name] = self.template
	end
end


-- Renders a template
function Template.render(self, scope)
	scope = scope or getfenv(2)
	
	-- Save old environment
	local oldfenv = getfenv(self.template)
	
	-- Put our predefined objects in the scope of the template
	luci.util.resfenv(self.template)
	luci.util.updfenv(self.template, scope)
	luci.util.updfenv(self.template, self.viewns)
	
	-- Now finally render the thing
	self.template()
	
	-- Reset environment
	setfenv(self.template, oldfenv)
end