@@ -2,61 +2,44 @@ var path = require('path');
22var glob = require ( 'glob' ) ;
33var fs = require ( 'fs-extra' ) ;
44var through = require ( 'through2' ) ;
5+ var File = require ( 'vinyl' ) ;
6+ var StringDecoder = require ( 'string_decoder' ) . StringDecoder ;
57var extend = require ( 'util' ) . _extend ;
68
9+ var frontMatter = require ( 'front-matter' ) ;
10+ var handlebars = require ( 'handlebars' ) ;
11+ var handlebarsRegistrar = require ( 'handlebars-registrar' ) ;
12+
713var config = require ( '../config' ) ;
14+ var partials = { } ;
815
916module . exports . task = function ( gulp , plugins , paths ) {
1017
11- // Handlebars engine
12- var handlebars = new require ( 'handlebars' ) ;
13- var handlebarsRegistrar = require ( 'handlebars-registrar' ) ;
1418
1519 // Register handlebars engine helpers and partials
1620 handlebarsRegistrar ( handlebars , {
1721 helpers : paths . app . helpers ,
1822 partials : paths . app . templates ,
19- parsePartialName : function ( file ) {
20- return file . shortPath ;
23+ parsePartialName : function ( partial ) {
24+
25+ // Save in partials vinyl registry
26+ partials [ partial . shortPath ] = new File ( {
27+ cwd : partial . cwd ,
28+ path : partial . path ,
29+ base : path . basename ( partial . path ) ,
30+ contents : fs . readFileSync ( partial . path )
31+ } ) ;
32+
33+ return partial . shortPath ;
2134 } ,
35+ bustCache : true ,
2236 } ) ;
2337
38+
2439 gulp . src ( paths . app . pages )
25- // Frontmatter
26- . pipe ( plugins . frontMatter ( ) )
2740 // Render pages
2841 . 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 = context . 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 ) ;
42+ file . contents = new Buffer ( renderTemplate ( file ) ) ;
6043
6144 this . push ( file ) ;
6245 cb ( ) ;
@@ -75,21 +58,99 @@ module.exports.task = function(gulp, plugins, paths) {
7558
7659 // Output
7760 . pipe ( gulp . dest ( config . destDir ) ) ;
61+
7862} ;
7963
8064
8165/********************************************
8266* Utils
8367*********************************************/
8468
69+ function renderTemplate ( file , options ) {
70+
71+ options = options || { } ;
72+
73+ // Set file frontMatter
74+ file = setFrontMatter ( file ) ;
75+
76+ // Get context from _context.js files and frontmatter
77+ var contextExternal = getPageContextExternal ( file ) ;
78+
79+ // Frontmatter context
80+ var contextTemplate = file . frontMatter || { } ;
81+
82+ // Inherited context from child
83+ var contextInherited = options . contextInherited || { } ;
84+
85+ // Result context
86+ var context = extend ( { } , contextExternal ) ;
87+ context = extend ( context , contextTemplate ) ;
88+ context = extend ( context , contextInherited ) ;
89+
90+ // Page render result
91+ var pageRes = "" ;
92+
93+ // Compile template
94+ var template = handlebars . compile ( String ( file . contents ) ) ;
95+ var templateRes = template ( context ) ;
96+
97+ // Layout processing
98+ var layout = context . layout || null ;
99+
100+ // If the layout exists, render it with template inside
101+ if ( layout && partials [ layout ] && handlebars . partials [ layout ] ) {
102+
103+ // New instance of context
104+ var layoutData = extend ( { } , context ) ;
105+
106+ // Add body to context
107+ layoutData = extend ( layoutData , {
108+ body : templateRes
109+ } ) ;
110+
111+ // Remove layout parameter from inhereted context
112+ delete layoutData . layout ;
113+
114+ // New vinyl file based on partail vinyl
115+ var layoutFile = new File ( partials [ layout ] ) ;
116+
117+ // Call recursively render template again
118+ pageRes = renderTemplate ( layoutFile , {
119+ contextInherited : layoutData
120+ } ) ;
121+ }
122+ // Return rendered template
123+ else {
124+ pageRes = templateRes ;
125+ }
126+
127+ return pageRes ;
128+ }
129+
130+
131+ /*
132+ Frontmatter file
133+ */
134+ function setFrontMatter ( file ) {
135+ // Read content from front matter
136+ var content = frontMatter ( file . contents . toString ( 'utf8' ) ) ;
137+
138+ // var res = new Buffer(content.body);
139+ file . contents = new Buffer ( content . body ) ;
140+ file . frontMatter = content . attributes ;
141+
142+ return file ;
143+ }
144+
145+
85146/*
86147 This function returns context of current page
87148 which is root context extended by all contexts untill
88149 current level context
89150*/
90151
91152
92- function getPageContext ( file ) {
153+ function getPageContextExternal ( file ) {
93154
94155 var context = { } ;
95156
0 commit comments