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 | , utils = require('./utils');
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | exports.selfClosing = Object.keys(require('void-elements'));
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | exports.doctypes = require('./doctypes');
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | exports.filters = require('./filters');
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | exports.utils = utils;
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | exports.Compiler = Compiler;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | exports.Parser = Parser;
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | exports.Lexer = Lexer;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | exports.nodes = require('./nodes');
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | exports.runtime = runtime;
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | exports.cache = {};
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | function parse(str, options){
|
93 |
|
94 | if (options.lexer) {
|
95 | console.warn('Using `lexer` as a local in render() is deprecated and '
|
96 | + 'will be interpreted as an option in Jade 2.0.0');
|
97 | }
|
98 |
|
99 |
|
100 | var parser = new (options.parser || Parser)(str, options.filename, options);
|
101 | var tokens;
|
102 | try {
|
103 |
|
104 | tokens = parser.parse();
|
105 | } catch (err) {
|
106 | parser = parser.context();
|
107 | runtime.rethrow(err, parser.filename, parser.lexer.lineno, parser.input);
|
108 | }
|
109 |
|
110 |
|
111 | var compiler = new (options.compiler || Compiler)(tokens, options);
|
112 | var js;
|
113 | try {
|
114 | js = compiler.compile();
|
115 | } catch (err) {
|
116 | if (err.line && (err.filename || !options.filename)) {
|
117 | runtime.rethrow(err, err.filename, err.line, parser.input);
|
118 | } else {
|
119 | if (err instanceof Error) {
|
120 | err.message += '\n\nPlease report this entire error and stack trace to https://github.com/jadejs/jade/issues';
|
121 | }
|
122 | throw err;
|
123 | }
|
124 | }
|
125 |
|
126 |
|
127 | if (options.debug) {
|
128 | console.error('\nCompiled Function:\n\n\u001b[90m%s\u001b[0m', js.replace(/^/gm, ' '));
|
129 | }
|
130 |
|
131 | var globals = [];
|
132 |
|
133 | if (options.globals) {
|
134 | globals = options.globals.slice();
|
135 | }
|
136 |
|
137 | globals.push('jade');
|
138 | globals.push('jade_mixins');
|
139 | globals.push('jade_interp');
|
140 | globals.push('jade_debug');
|
141 | globals.push('buf');
|
142 |
|
143 | var body = ''
|
144 | + 'var buf = [];\n'
|
145 | + 'var jade_mixins = {};\n'
|
146 | + 'var jade_interp;\n'
|
147 | + (options.self
|
148 | ? 'var self = locals || {};\n' + js
|
149 | : addWith('locals || {}', '\n' + js, globals)) + ';'
|
150 | + 'return buf.join("");';
|
151 | return {body: body, dependencies: parser.dependencies};
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function handleTemplateCache (options, str) {
|
169 | var key = options.filename;
|
170 | if (options.cache && exports.cache[key]) {
|
171 | return exports.cache[key];
|
172 | } else {
|
173 | if (str === undefined) str = fs.readFileSync(options.filename, 'utf8');
|
174 | var templ = exports.compile(str, options);
|
175 | if (options.cache) exports.cache[key] = templ;
|
176 | return templ;
|
177 | }
|
178 | }
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | exports.compile = function(str, options){
|
197 | var options = options || {}
|
198 | , filename = options.filename
|
199 | ? utils.stringify(options.filename)
|
200 | : 'undefined'
|
201 | , fn;
|
202 |
|
203 | str = String(str);
|
204 |
|
205 | var parsed = parse(str, options);
|
206 | if (options.compileDebug !== false) {
|
207 | fn = [
|
208 | 'var jade_debug = [ new jade.DebugItem( 1, ' + filename + ' ) ];'
|
209 | , 'try {'
|
210 | , parsed.body
|
211 | , '} catch (err) {'
|
212 | , ' jade.rethrow(err, jade_debug[0].filename, jade_debug[0].lineno' + (options.compileDebug === true ? ',' + utils.stringify(str) : '') + ');'
|
213 | , '}'
|
214 | ].join('\n');
|
215 | } else {
|
216 | fn = parsed.body;
|
217 | }
|
218 | fn = new Function('locals, jade', fn)
|
219 | var res = function(locals){ return fn(locals, Object.create(runtime)) };
|
220 | if (options.client) {
|
221 | res.toString = function () {
|
222 | var err = new Error('The `client` option is deprecated, use the `jade.compileClient` method instead');
|
223 | err.name = 'Warning';
|
224 | console.error(err.stack || err.message);
|
225 | return exports.compileClient(str, options);
|
226 | };
|
227 | }
|
228 | res.dependencies = parsed.dependencies;
|
229 | return res;
|
230 | };
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | exports.compileClientWithDependenciesTracked = function(str, options){
|
249 | var options = options || {};
|
250 | var name = options.name || 'template';
|
251 | var filename = options.filename ? utils.stringify(options.filename) : 'undefined';
|
252 | var fn;
|
253 |
|
254 | str = String(str);
|
255 | options.compileDebug = options.compileDebug ? true : false;
|
256 | var parsed = parse(str, options);
|
257 | if (options.compileDebug) {
|
258 | fn = [
|
259 | 'var jade_debug = [ new jade.DebugItem( 1, ' + filename + ' ) ];'
|
260 | , 'try {'
|
261 | , parsed.body
|
262 | , '} catch (err) {'
|
263 | , ' jade.rethrow(err, jade_debug[0].filename, jade_debug[0].lineno, ' + utils.stringify(str) + ');'
|
264 | , '}'
|
265 | ].join('\n');
|
266 | } else {
|
267 | fn = parsed.body;
|
268 | }
|
269 |
|
270 | return {body: 'function ' + name + '(locals) {\n' + fn + '\n}', dependencies: parsed.dependencies};
|
271 | };
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | exports.compileClient = function (str, options) {
|
289 | return exports.compileClientWithDependenciesTracked(str, options).body;
|
290 | };
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | exports.compileFile = function (path, options) {
|
307 | options = options || {};
|
308 | options.filename = path;
|
309 | return handleTemplateCache(options);
|
310 | };
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 | exports.render = function(str, options, fn){
|
328 |
|
329 | if ('function' == typeof options) {
|
330 | fn = options, options = undefined;
|
331 | }
|
332 | if (typeof fn === 'function') {
|
333 | var res
|
334 | try {
|
335 | res = exports.render(str, options);
|
336 | } catch (ex) {
|
337 | return fn(ex);
|
338 | }
|
339 | return fn(null, res);
|
340 | }
|
341 |
|
342 | options = options || {};
|
343 |
|
344 |
|
345 | if (options.cache && !options.filename) {
|
346 | throw new Error('the "filename" option is required for caching');
|
347 | }
|
348 |
|
349 | return handleTemplateCache(options, str)(options);
|
350 | };
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 | exports.renderFile = function(path, options, fn){
|
363 |
|
364 | if ('function' == typeof options) {
|
365 | fn = options, options = undefined;
|
366 | }
|
367 | if (typeof fn === 'function') {
|
368 | var res
|
369 | try {
|
370 | res = exports.renderFile(path, options);
|
371 | } catch (ex) {
|
372 | return fn(ex);
|
373 | }
|
374 | return fn(null, res);
|
375 | }
|
376 |
|
377 | options = options || {};
|
378 |
|
379 | options.filename = path;
|
380 | return handleTemplateCache(options)(options);
|
381 | };
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 | exports.compileFileClient = function(path, options){
|
394 | var key = path + ':client';
|
395 | options = options || {};
|
396 |
|
397 | options.filename = path;
|
398 |
|
399 | if (options.cache && exports.cache[key]) {
|
400 | return exports.cache[key];
|
401 | }
|
402 |
|
403 | var str = fs.readFileSync(options.filename, 'utf8');
|
404 | var out = exports.compileClient(str, options);
|
405 | if (options.cache) exports.cache[key] = out;
|
406 | return out;
|
407 | };
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 | exports.__express = function(path, options, fn) {
|
414 | if(options.compileDebug == undefined && process.env.NODE_ENV === 'production') {
|
415 | options.compileDebug = false;
|
416 | }
|
417 | exports.renderFile(path, options, fn);
|
418 | }
|