1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 | var path = require('path');
|
5 | var readdirp = require('readdirp');
|
6 | var handlebars = require('handlebars');
|
7 | var resolver = require('./resolver');
|
8 | var _ = require('lodash');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var layoutPattern = /{{!<\s+([A-Za-z0-9\._\-\/]+)\s*}}/;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var ExpressHbs = function() {
|
19 | this.handlebars = handlebars.create();
|
20 | this.SafeString = this.handlebars.SafeString;
|
21 | this.Utils = this.handlebars.Utils;
|
22 | this.beautify = null;
|
23 | this.beautifyrc = null;
|
24 | this.cwd = process.cwd();
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | ExpressHbs.prototype.content = function(name, options, context) {
|
37 | var block = options.data.root.blockCache[name] || (options.data.root.blockCache[name] = []);
|
38 | block.push(options.fn(context));
|
39 | };
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | ExpressHbs.prototype.layoutPath = function(filename, layout) {
|
50 | var dirs,
|
51 | layoutPath;
|
52 | if (layout[0] === '.') {
|
53 | dirs = path.dirname(filename);
|
54 | } else if (this.layoutsDir) {
|
55 | dirs = this.layoutsDir;
|
56 | } else {
|
57 | dirs = this.viewsDir;
|
58 | }
|
59 | [].concat(dirs).forEach(function (dir) {
|
60 | if (!layoutPath) {
|
61 | layoutPath = path.resolve(dir, layout);
|
62 | }
|
63 | });
|
64 | return layoutPath;
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | ExpressHbs.prototype.declaredLayoutFile = function(str, filename) {
|
75 | var matches = str.match(layoutPattern);
|
76 | if (matches) {
|
77 | var layout = matches[1];
|
78 |
|
79 |
|
80 | if (this.layoutsDir && layout[0] !== '.') {
|
81 | layout = path.resolve(this.layoutsDir, layout);
|
82 | }
|
83 | return path.resolve(path.dirname(filename), layout);
|
84 | }
|
85 | };
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | ExpressHbs.prototype.cacheLayout = function(layoutFile, useCache, cb) {
|
100 | var self = this;
|
101 |
|
102 |
|
103 | if (path.extname(layoutFile) === '') layoutFile += this._options.extname;
|
104 |
|
105 |
|
106 | var layoutTemplates = this.cache[layoutFile];
|
107 | if (layoutTemplates) return cb(null, layoutTemplates);
|
108 |
|
109 | fs.readFile(layoutFile, 'utf8', function(err, str) {
|
110 | if (err) return cb(err);
|
111 |
|
112 |
|
113 | var parentLayoutFile = self.declaredLayoutFile(str, layoutFile);
|
114 |
|
115 |
|
116 | var _returnLayouts = function(layouts) {
|
117 | var currentLayout;
|
118 | layouts = layouts.slice(0);
|
119 | currentLayout = self.compile(str, layoutFile);
|
120 | layouts.push(currentLayout);
|
121 | if (useCache) {
|
122 | self.cache[layoutFile] = layouts.slice(0);
|
123 | }
|
124 | cb(null, layouts);
|
125 | };
|
126 |
|
127 | if (parentLayoutFile) {
|
128 |
|
129 | self.cacheLayout(parentLayoutFile, useCache, function(err, parentLayouts) {
|
130 | if (err) return cb(err);
|
131 | _returnLayouts(parentLayouts);
|
132 | });
|
133 | } else {
|
134 |
|
135 | _returnLayouts([]);
|
136 | }
|
137 | });
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | ExpressHbs.prototype.cachePartials = function(cb) {
|
144 | var self = this;
|
145 |
|
146 | if (!(this.partialsDir instanceof Array)) {
|
147 | this.partialsDir = [this.partialsDir];
|
148 | }
|
149 |
|
150 |
|
151 | var count = 0;
|
152 |
|
153 | function readNext() {
|
154 | readdirp(self.partialsDir[count], {fileFilter: '*' + self._options.extname})
|
155 | .on('warn', function(err) {
|
156 | console.warn('Non-fatal error in express-hbs cachePartials.', err);
|
157 | })
|
158 | .on('error', function(err) {
|
159 | console.error('Fatal error in express-hbs cachePartials', err);
|
160 | return cb(err);
|
161 | })
|
162 | .on('data', function(entry) {
|
163 | if (!entry) return;
|
164 | var source = fs.readFileSync(entry.fullPath, 'utf8');
|
165 | var dirname = path.dirname(entry.path);
|
166 | dirname = dirname === '.' ? '' : dirname + '/';
|
167 |
|
168 | var name = dirname + path.basename(entry.basename, self._options.extname);
|
169 |
|
170 | name = name.split('\\').join('/');
|
171 | self.registerPartial(name, source, entry.fullPath);
|
172 | })
|
173 | .on('end', function() {
|
174 | count += 1;
|
175 |
|
176 |
|
177 | if (count < self.partialsDir.length) {
|
178 | readNext();
|
179 | } else {
|
180 | self.isPartialCachingComplete = true;
|
181 | if (cb) cb(null, true);
|
182 | }
|
183 | });
|
184 | }
|
185 |
|
186 | readNext();
|
187 | };
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | ExpressHbs.prototype.express3 = function(options) {
|
207 | var self = this;
|
208 |
|
209 |
|
210 | if (!options) options = {};
|
211 | if (!options.extname) options.extname = '.hbs';
|
212 | if (!options.contentHelperName) options.contentHelperName = 'contentFor';
|
213 | if (!options.blockHelperName) options.blockHelperName = 'block';
|
214 | if (!options.templateOptions) options.templateOptions = {};
|
215 | if (options.handlebars) this.handlebars = options.handlebars;
|
216 | if (options.onCompile) this.onCompile = options.onCompile;
|
217 |
|
218 | this._options = options;
|
219 | if (this._options.handlebars) this.handlebars = this._options.handlebars;
|
220 |
|
221 | if (options.i18n) {
|
222 | var i18n = options.i18n;
|
223 | this.handlebars.registerHelper('__', function() {
|
224 | var args = Array.prototype.slice.call(arguments);
|
225 | var options = args.pop();
|
226 | return i18n.__.apply(options.data.root, args);
|
227 | });
|
228 | this.handlebars.registerHelper('__n', function() {
|
229 | var args = Array.prototype.slice.call(arguments);
|
230 | var options = args.pop();
|
231 | return i18n.__n.apply(options.data.root, args);
|
232 | });
|
233 | }
|
234 |
|
235 | this.handlebars.registerHelper(this._options.blockHelperName, function(name, options) {
|
236 | var val = options.data.root.blockCache[name];
|
237 | if (val === undefined && typeof options.fn === 'function') {
|
238 | val = options.fn(this);
|
239 | }
|
240 | if (Array.isArray(val)) {
|
241 | val = val.join('\n');
|
242 | }
|
243 | return val;
|
244 | });
|
245 |
|
246 |
|
247 | this.handlebars.registerHelper(this._options.contentHelperName, function(name, options) {
|
248 | return self.content(name, options, this);
|
249 | });
|
250 |
|
251 |
|
252 | this.partialsDir = this._options.partialsDir;
|
253 |
|
254 |
|
255 | this.layoutsDir = this._options.layoutsDir;
|
256 |
|
257 |
|
258 | this.viewsDir = null;
|
259 | this.viewsDirOpt = this._options.viewsDir;
|
260 |
|
261 |
|
262 | this.cache = {};
|
263 |
|
264 |
|
265 | this.defaultLayoutTemplates = null;
|
266 |
|
267 |
|
268 | this.isPartialCachingComplete = false;
|
269 |
|
270 | return this.___express.bind(this);
|
271 | };
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | ExpressHbs.prototype.express4 = ExpressHbs.prototype.express3;
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | ExpressHbs.prototype.loadDefaultLayout = function(useCache, cb) {
|
295 | var self = this;
|
296 | if (!this._options.defaultLayout) return cb();
|
297 | if (useCache && this.defaultLayoutTemplates) return cb(null, this.defaultLayoutTemplates);
|
298 |
|
299 | this.cacheLayout(this._options.defaultLayout, useCache, function(err, templates) {
|
300 | if (err) return cb(err);
|
301 | self.defaultLayoutTemplates = templates.slice(0);
|
302 | return cb(null, templates);
|
303 | });
|
304 | };
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | ExpressHbs.prototype.registerHelper = function(name, fn) {
|
310 | this.handlebars.registerHelper(name, fn);
|
311 | };
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | ExpressHbs.prototype.registerPartial = function(name, source, filename) {
|
320 | this.handlebars.registerPartial(name, this.compile(source, filename));
|
321 | };
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | ExpressHbs.prototype.compile = function(source, filename) {
|
330 |
|
331 |
|
332 | if (typeof source !== 'string') {
|
333 | throw new Error('registerPartial must be a string for empty comment workaround');
|
334 | }
|
335 | if (source.indexOf('}}') === source.length - 2) {
|
336 | source += ' ';
|
337 | }
|
338 |
|
339 | var compiled;
|
340 | if (this.onCompile) {
|
341 | compiled = this.onCompile(this, source, filename);
|
342 | } else {
|
343 | compiled = this.handlebars.compile(source);
|
344 | }
|
345 |
|
346 | if (filename) {
|
347 | if (Array.isArray(this.viewsDir) && this.viewsDir.length > 0) {
|
348 | compiled.__filename = path.relative(this.cwd, filename).replace(path.sep, '/');
|
349 | } else {
|
350 | compiled.__filename = path.relative(this.viewsDir || '', filename).replace(path.sep, '/');
|
351 | }
|
352 | }
|
353 | return compiled;
|
354 | };
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 | ExpressHbs.prototype.registerAsyncHelper = function(name, fn) {
|
363 | this.handlebars.registerHelper(name, function(context, options) {
|
364 | var resolverCache = this.resolverCache ||
|
365 | _.get(context, 'data.root.resolverCache') ||
|
366 | _.get(options, 'data.root.resolverCache');
|
367 | if (!resolverCache) {
|
368 | throw new Error('Could not find resolver cache in async helper ' + name + '.');
|
369 | }
|
370 | if (options && fn.length > 2) {
|
371 | var resolveFunc = function(arr, cb) {
|
372 | return fn.call(this, arr[0], arr[1], cb);
|
373 | };
|
374 |
|
375 | return resolver.resolve(
|
376 | resolverCache,
|
377 | resolveFunc.bind(this),
|
378 | [context, options]
|
379 | );
|
380 | }
|
381 | return resolver.resolve(
|
382 | resolverCache,
|
383 | fn.bind(this),
|
384 | context
|
385 | );
|
386 | });
|
387 | };
|
388 |
|
389 | ExpressHbs.prototype.getTemplateOptions = function() {
|
390 | return this._options.templateOptions;
|
391 | };
|
392 |
|
393 | ExpressHbs.prototype.updateTemplateOptions = function(templateOptions) {
|
394 | this._options.templateOptions = templateOptions;
|
395 | };
|
396 |
|
397 | ExpressHbs.prototype.getLocalTemplateOptions = function(locals) {
|
398 | return locals._templateOptions || {};
|
399 | };
|
400 |
|
401 | ExpressHbs.prototype.updateLocalTemplateOptions = function(locals, localTemplateOptions) {
|
402 | return locals._templateOptions = localTemplateOptions;
|
403 | };
|
404 |
|
405 |
|
406 |
|
407 |
|
408 | ExpressHbs.prototype.create = function() {
|
409 | return new ExpressHbs();
|
410 | };
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 | ExpressHbs.prototype.___express = function ___express(filename, source, options, cb) {
|
443 |
|
444 | if (arguments.length === 3) {
|
445 | cb = options;
|
446 | options = source;
|
447 | source = null;
|
448 | }
|
449 |
|
450 | options.blockCache = {};
|
451 | options.resolverCache = {};
|
452 |
|
453 | this.viewsDir = options.settings.views || this.viewsDirOpt;
|
454 | var self = this;
|
455 |
|
456 | |
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 | function parseLayout(str, filename, cb) {
|
467 | var layoutFile = self.declaredLayoutFile(str, filename);
|
468 | if (layoutFile) {
|
469 | self.cacheLayout(layoutFile, options.cache, cb);
|
470 | } else {
|
471 | cb(null, null);
|
472 | }
|
473 | }
|
474 |
|
475 | |
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 | function renderTemplate(template, locals, cb) {
|
484 | var res;
|
485 |
|
486 | try {
|
487 | var localTemplateOptions = self.getLocalTemplateOptions(locals);
|
488 | var localsClone = _.extend({}, locals);
|
489 | self.updateLocalTemplateOptions(localsClone, undefined);
|
490 | res = template(localsClone, _.merge({}, self._options.templateOptions, localTemplateOptions));
|
491 | } catch (err) {
|
492 | if (err.message) {
|
493 | err.message = '[' + template.__filename + '] ' + err.message;
|
494 | } else if (typeof err === 'string') {
|
495 | return cb('[' + template.__filename + '] ' + err, null);
|
496 | }
|
497 | return cb(err, null);
|
498 | }
|
499 | cb(null, res);
|
500 | }
|
501 |
|
502 |
|
503 | |
504 |
|
505 |
|
506 |
|
507 | function render(template, locals, layoutTemplates, cb) {
|
508 | if (!layoutTemplates) layoutTemplates = [];
|
509 |
|
510 |
|
511 |
|
512 | var i = layoutTemplates.length - 1;
|
513 |
|
514 | var _stackRenderer = function(err, htmlStr) {
|
515 | if (err) return cb(err);
|
516 |
|
517 | if (i >= 0) {
|
518 | locals.body = htmlStr;
|
519 | renderTemplate(layoutTemplates[i--], locals, _stackRenderer);
|
520 | } else {
|
521 | cb(null, htmlStr);
|
522 | }
|
523 | };
|
524 |
|
525 |
|
526 | renderTemplate(template, locals, _stackRenderer);
|
527 | }
|
528 |
|
529 |
|
530 | |
531 |
|
532 |
|
533 | function loadBeautify() {
|
534 | if (!self.beautify) {
|
535 | self.beautify = require('js-beautify').html;
|
536 | var rc = path.join(process.cwd(), '.jsbeautifyrc');
|
537 | if (fs.existsSync(rc)) {
|
538 | self.beautifyrc = JSON.parse(fs.readFileSync(rc, 'utf8'));
|
539 | }
|
540 | }
|
541 | }
|
542 |
|
543 | |
544 |
|
545 |
|
546 |
|
547 | function getSourceTemplate(cb) {
|
548 | if (options.cache) {
|
549 | var info = self.cache[filename];
|
550 | if (info) {
|
551 | return cb(null, info.source, info.template);
|
552 | }
|
553 | }
|
554 |
|
555 | fs.readFile(filename, 'utf8', function(err, source) {
|
556 | if (err) return cb(err);
|
557 |
|
558 | var template = self.compile(source, filename);
|
559 | if (options.cache) {
|
560 | self.cache[filename] = {
|
561 | source: source,
|
562 | template: template
|
563 | };
|
564 | }
|
565 | return cb(null, source, template);
|
566 | });
|
567 | }
|
568 |
|
569 | |
570 |
|
571 |
|
572 | function compileFile(locals, cb) {
|
573 | getSourceTemplate(function(err, source, template) {
|
574 | if (err) return cb(err);
|
575 |
|
576 |
|
577 | parseLayout(source, filename, function(err, layoutTemplates) {
|
578 | if (err) return cb(err);
|
579 |
|
580 | function renderIt(layoutTemplates) {
|
581 | if (self._options.beautify) {
|
582 | return render(template, locals, layoutTemplates, function(err, html) {
|
583 | if (err) return cb(err);
|
584 | loadBeautify();
|
585 | return cb(null, self.beautify(html, self.beautifyrc));
|
586 | });
|
587 | }
|
588 | return render(template, locals, layoutTemplates, cb);
|
589 | }
|
590 |
|
591 |
|
592 |
|
593 | if (typeof options.layout !== 'undefined' && !options.layout) {
|
594 |
|
595 | renderIt(null);
|
596 | } else if (layoutTemplates) {
|
597 |
|
598 | renderIt(layoutTemplates);
|
599 | } else if (typeof options.layout !== 'undefined' && options.layout) {
|
600 |
|
601 | var layoutFile = self.layoutPath(filename, options.layout);
|
602 | self.cacheLayout(layoutFile, options.cache, function(err, layoutTemplates) {
|
603 | if (err) return cb(err);
|
604 | renderIt(layoutTemplates);
|
605 | });
|
606 | } else if (self.defaultLayoutTemplates) {
|
607 |
|
608 | renderIt(self.defaultLayoutTemplates);
|
609 | } else {
|
610 |
|
611 | renderIt(null);
|
612 | }
|
613 | });
|
614 | });
|
615 | }
|
616 |
|
617 | function replaceValue(values, text) {
|
618 | if (typeof text === 'string') {
|
619 | Object.keys(values).forEach(function(id) {
|
620 | text = text.replace(id, function() {
|
621 | return values[id];
|
622 | });
|
623 | text = text.replace(self.Utils.escapeExpression(id), function() {
|
624 | return self.Utils.escapeExpression(values[id]);
|
625 | });
|
626 | });
|
627 | }
|
628 | return text;
|
629 | }
|
630 |
|
631 |
|
632 | function handleAsync(err, res) {
|
633 | if (err) return cb(err);
|
634 | resolver.done(options.resolverCache, function(err, values) {
|
635 | if (err) return cb(err);
|
636 | Object.keys(values).forEach(function(key) {
|
637 | values[key] = replaceValue(values, values[key]);
|
638 | });
|
639 | res = replaceValue(values, res);
|
640 | if (resolver.hasResolvers(res)) {
|
641 | return handleAsync(null, res);
|
642 | }
|
643 | cb(null, res);
|
644 | });
|
645 | }
|
646 |
|
647 |
|
648 | this.loadDefaultLayout(options.cache, function(err) {
|
649 | if (err) return cb(err);
|
650 |
|
651 |
|
652 |
|
653 | if (self.partialsDir && (!options.cache || !self.isPartialCachingComplete)) {
|
654 | return self.cachePartials(function(err) {
|
655 | if (err) return cb(err);
|
656 | return compileFile(options, handleAsync);
|
657 | });
|
658 | }
|
659 |
|
660 | return compileFile(options, handleAsync);
|
661 | });
|
662 | };
|
663 |
|
664 | module.exports = new ExpressHbs();
|