|
1 | 1 | var path = require('path'); |
2 | 2 | var glob = require('glob'); |
3 | | -var fs = require('fs'); |
| 3 | +var fs = require('fs-extra'); |
| 4 | +var through = require('through2'); |
4 | 5 | var extend = require('util')._extend; |
5 | 6 |
|
6 | 7 | var config = require('../config'); |
7 | 8 |
|
8 | 9 | module.exports.task = function(gulp, plugins, paths) { |
9 | | - |
| 10 | + |
| 11 | + // Handlebars engine |
| 12 | + var handlebars = new require('handlebars'); |
| 13 | + var handlebarsRegistrar = require('handlebars-registrar'); |
| 14 | + |
| 15 | + // Register handlebars engine helpers and partials |
| 16 | + handlebarsRegistrar(handlebars, { |
| 17 | + helpers: paths.app.helpers, |
| 18 | + partials: paths.app.templates, |
| 19 | + parsePartialName: function (file) { |
| 20 | + return file.shortPath; |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
10 | 24 | gulp.src(paths.app.pages) |
11 | 25 | // Frontmatter |
12 | 26 | .pipe(plugins.frontMatter()) |
13 | | - // handlebars compilation |
14 | | - .pipe(plugins.hb({ |
15 | | - // Register all templates as partials |
16 | | - partials: paths.app.templates, |
17 | | - // Partials naming e.g. 'app/app-layout' |
18 | | - parsePartialName: function (file) { |
19 | | - return file.shortPath; |
20 | | - }, |
21 | | - // Registering template helpers |
22 | | - helpers: paths.app.helpers, |
23 | | - // Context data for each page file |
24 | | - dataEach: function (context, file) { |
25 | | - |
26 | | - var contextExtended = extend(context, getPageContext(file)); |
27 | | - contextExtended = extend(contextExtended, file.frontMatter); |
28 | | - |
29 | | - return contextExtended; |
30 | | - }, |
31 | | - // Remove cache every time for 'watch' |
32 | | - bustCache: true |
| 27 | + // Render pages |
| 28 | + .pipe(through.obj(function (file, enc, cb) { |
| 29 | + // Page render result |
| 30 | + var pageRes = ""; |
| 31 | + |
| 32 | + // Get context from _context.js files and frontmatter |
| 33 | + var context = getPageContext(file); |
| 34 | + context = extend(context, file.frontMatter); |
| 35 | + |
| 36 | + // Compile template |
| 37 | + var template = handlebars.compile(String(file.contents)); |
| 38 | + var templateRes = template(context); |
| 39 | + |
| 40 | + // Layout processing |
| 41 | + var layout = file.frontMatter.layout || null; |
| 42 | + |
| 43 | + // If the layout exists, render it with template inside |
| 44 | + if (layout && handlebars.partials[layout]) { |
| 45 | + var layoutData = extend(context, { |
| 46 | + body: templateRes |
| 47 | + }); |
| 48 | + |
| 49 | + // Render layout with given context and content |
| 50 | + var layoutRes = handlebars.partials[layout](layoutData); |
| 51 | + |
| 52 | + pageRes = layoutRes; |
| 53 | + } |
| 54 | + // Return rendered template |
| 55 | + else { |
| 56 | + pageRes = templateRes; |
| 57 | + } |
| 58 | + |
| 59 | + file.contents = new Buffer(pageRes); |
| 60 | + |
| 61 | + this.push(file); |
| 62 | + cb(); |
33 | 63 | })) |
34 | | - |
35 | 64 | // Handle errors |
36 | 65 | .on('error', plugins.util.log) |
37 | 66 |
|
|
0 commit comments