1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var Parser = require('./parser')
|
14 | , Lexer = require('./lexer')
|
15 | , Compiler = require('./compiler')
|
16 | , runtime = require('./runtime')
|
17 | , addWith = require('with')
|
18 | , fs = require('fs');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | exports.selfClosing = require('./self-closing');
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | exports.doctypes = require('./doctypes');
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | exports.filters = require('./filters');
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | exports.utils = require('./utils');
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | exports.Compiler = Compiler;
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | exports.Parser = Parser;
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | exports.Lexer = Lexer;
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | exports.nodes = require('./nodes');
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | exports.runtime = runtime;
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | exports.cache = {};
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function parse(str, options){
|
90 |
|
91 | var parser = new (options.parser || Parser)(str, options.filename, options);
|
92 | var tokens;
|
93 | try {
|
94 |
|
95 | tokens = parser.parse();
|
96 | } catch (err) {
|
97 | parser = parser.context();
|
98 | runtime.rethrow(err, parser.filename, parser.lexer.lineno, parser.input);
|
99 | }
|
100 |
|
101 |
|
102 | var compiler = new (options.compiler || Compiler)(tokens, options);
|
103 | var js;
|
104 | try {
|
105 | js = compiler.compile();
|
106 | } catch (err) {
|
107 | if (err.line && (err.filename || !options.filename)) {
|
108 | runtime.rethrow(err, err.filename, err.line, parser.input);
|
109 | }
|
110 | }
|
111 |
|
112 |
|
113 | if (options.debug) {
|
114 | console.error('\nCompiled Function:\n\n\u001b[90m%s\u001b[0m', js.replace(/^/gm, ' '));
|
115 | }
|
116 |
|
117 | var globals = [];
|
118 |
|
119 | globals.push('jade');
|
120 | globals.push('jade_mixins');
|
121 | globals.push('jade_interp');
|
122 | globals.push('jade_debug');
|
123 | globals.push('buf');
|
124 |
|
125 | var body = ''
|
126 | + 'var buf = [];\n'
|
127 | + 'var jade_mixins = {};\n'
|
128 | + 'var jade_interp;\n'
|
129 | + (options.self
|
130 | ? 'var self = locals || {};\n' + js
|
131 | : addWith('locals || {}', '\n' + js, globals)) + ';'
|
132 | + 'return buf.join("");';
|
133 | return {body: body, dependencies: parser.dependencies};
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | exports.compile = function(str, options){
|
153 | var options = options || {}
|
154 | , filename = options.filename
|
155 | ? JSON.stringify(options.filename)
|
156 | : 'undefined'
|
157 | , fn;
|
158 |
|
159 | str = String(str);
|
160 |
|
161 | var parsed = parse(str, options);
|
162 | if (options.compileDebug !== false) {
|
163 | fn = [
|
164 | 'var jade_debug = [{ lineno: 1, filename: ' + filename + ' }];'
|
165 | , 'try {'
|
166 | , parsed.body
|
167 | , '} catch (err) {'
|
168 | , ' jade.rethrow(err, jade_debug[0].filename, jade_debug[0].lineno' + (options.compileDebug === true ? ',' + JSON.stringify(str) : '') + ');'
|
169 | , '}'
|
170 | ].join('\n');
|
171 | } else {
|
172 | fn = parsed.body;
|
173 | }
|
174 | fn = new Function('locals, jade', fn)
|
175 | var res = function(locals){ return fn(locals, Object.create(runtime)) };
|
176 | if (options.client) {
|
177 | res.toString = function () {
|
178 | var err = new Error('The `client` option is deprecated, use the `jade.compileClient` method instead');
|
179 | err.name = 'Warning';
|
180 | console.error(err.stack || err.message);
|
181 | return exports.compileClient(str, options);
|
182 | };
|
183 | }
|
184 | res.dependencies = parsed.dependencies;
|
185 | return res;
|
186 | };
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | exports.compileClient = function(str, options){
|
205 | var options = options || {};
|
206 | var name = options.name || 'template';
|
207 | var filename = options.filename ? JSON.stringify(options.filename) : 'undefined';
|
208 | var fn;
|
209 |
|
210 | str = String(str);
|
211 |
|
212 | if (options.compileDebug) {
|
213 | options.compileDebug = true;
|
214 | fn = [
|
215 | 'var jade_debug = [{ lineno: 1, filename: ' + filename + ' }];'
|
216 | , 'try {'
|
217 | , parse(str, options).body
|
218 | , '} catch (err) {'
|
219 | , ' jade.rethrow(err, jade_debug[0].filename, jade_debug[0].lineno, ' + JSON.stringify(str) + ');'
|
220 | , '}'
|
221 | ].join('\n');
|
222 | } else {
|
223 | options.compileDebug = false;
|
224 | fn = parse(str, options).body;
|
225 | }
|
226 |
|
227 | return 'function ' + name + '(locals) {\n' + fn + '\n}';
|
228 | };
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | exports.render = function(str, options, fn){
|
248 |
|
249 | if ('function' == typeof options) {
|
250 | fn = options, options = undefined;
|
251 | }
|
252 | if (typeof fn === 'function') {
|
253 | var res
|
254 | try {
|
255 | res = exports.render(str, options);
|
256 | } catch (ex) {
|
257 | return fn(ex);
|
258 | }
|
259 | return fn(null, res);
|
260 | }
|
261 |
|
262 | options = options || {};
|
263 |
|
264 |
|
265 | if (options.cache && !options.filename) {
|
266 | throw new Error('the "filename" option is required for caching');
|
267 | }
|
268 |
|
269 | var path = options.filename;
|
270 | var tmpl = options.cache
|
271 | ? exports.cache[path] || (exports.cache[path] = exports.compile(str, options))
|
272 | : exports.compile(str, options);
|
273 | return tmpl(options);
|
274 | };
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | exports.renderFile = function(path, options, fn){
|
287 |
|
288 | if ('function' == typeof options) {
|
289 | fn = options, options = undefined;
|
290 | }
|
291 | if (typeof fn === 'function') {
|
292 | var res
|
293 | try {
|
294 | res = exports.renderFile(path, options);
|
295 | } catch (ex) {
|
296 | return fn(ex);
|
297 | }
|
298 | return fn(null, res);
|
299 | }
|
300 |
|
301 | options = options || {};
|
302 |
|
303 | var key = path + ':string';
|
304 |
|
305 | options.filename = path;
|
306 | var str = options.cache
|
307 | ? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8'))
|
308 | : fs.readFileSync(path, 'utf8');
|
309 | return exports.render(str, options);
|
310 | };
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | exports.compileFileClient = function(path, options){
|
323 | options = options || {};
|
324 |
|
325 | var key = path + ':string';
|
326 |
|
327 | options.filename = path;
|
328 | var str = options.cache
|
329 | ? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8'))
|
330 | : fs.readFileSync(path, 'utf8');
|
331 |
|
332 | return exports.compileClient(str, options);
|
333 | };
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | exports.__express = exports.renderFile;
|