Skip to content

Commit a38e78c

Browse files
author
Brian Muenzenmeyer
committed
Merge pull request #49 from ivanmayes/ivan
Abstracted template rendering; fixed issue with JSON files order in filesystem
2 parents 8b4f0c6 + c0485ec commit a38e78c

File tree

1 file changed

+62
-77
lines changed

1 file changed

+62
-77
lines changed

builder/patternlab.js

Lines changed: 62 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.1.2 - 2014-07-12
2+
* patternlab-node - v0.1.2 - 2014-07-15
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
@@ -61,87 +61,64 @@ var patternlab_engine = function(grunt){
6161
var currentPattern;
6262
var flatPatternPath;
6363

64-
//ignore _underscored patterns
65-
if(filename.charAt(0) === '_'){
64+
//ignore _underscored patterns and json
65+
if(filename.charAt(0) === '_' || grunt.util._.str.include(filename, 'json')){
6666
return;
6767
}
68+
69+
70+
//make a new Pattern Object
71+
var flatPatternName = subdir.replace(/\//g, '-') + '-' + patternName;
72+
flatPatternName = flatPatternName.replace(/\//g, '-');
73+
currentPattern = new of.oPattern(flatPatternName, subdir, filename, {});
74+
currentPattern.patternName = patternName.substring(patternName.indexOf('-') + 1);
75+
currentPattern.data = null;
76+
77+
//look for a json file for this template
78+
try {
79+
var jsonFilename = abspath.substr(0, abspath.lastIndexOf(".")) + ".json";
80+
currentPattern.data = grunt.file.readJSON(jsonFilename);
81+
}
82+
catch(e) {
6883

69-
//two reasons could return no pattern, 1) just a bare mustache, or 2) a json found before the mustache
70-
//returns -1 if patterns does not exist, otherwise returns the index
71-
//add the pattern array if first time, otherwise pull it up
72-
if(patternIndex === -1){
73-
grunt.verbose.ok('pattern not found, adding to array');
74-
var flatPatternName = subdir.replace(/\//g, '-') + '-' + patternName;
75-
flatPatternName = flatPatternName.replace(/\//g, '-');
76-
currentPattern = new of.oPattern(flatPatternName, subdir, filename, {});
77-
currentPattern.patternName = patternName.substring(patternName.indexOf('-') + 1);
78-
79-
if(grunt.util._.str.include(filename, 'json')){
80-
grunt.verbose.ok('json file found first, so add it to the pattern and continuing');
81-
currentPattern.data = grunt.file.readJSON(abspath);
82-
//done
83-
} else{
84-
grunt.verbose.ok('mustache file found, assume no data, so compile it right away');
85-
currentPattern.template = grunt.file.read(abspath);
86-
87-
//render the pattern. pass partials object just in case.
88-
currentPattern.patternPartial = mustache.render(currentPattern.template, patternlab.data, patternlab.partials);
89-
90-
//write the compiled template to the public patterns directory
91-
flatPatternPath = currentPattern.name + '/' + currentPattern.name + '.html';
92-
93-
//add footer info before writing
94-
var currentPatternFooter = mustache.render(patternlab.footer, currentPattern);
95-
96-
grunt.file.write('./public/patterns/' + flatPatternPath, patternlab.header + currentPattern.patternPartial + currentPatternFooter);
97-
currentPattern.patternLink = flatPatternPath;
98-
99-
//add as a partial in case this is referenced later. convert to syntax needed by existing patterns
100-
var sub = subdir.substring(subdir.indexOf('-') + 1);
101-
var folderIndex = sub.indexOf('/'); //THIS IS MOST LIKELY WINDOWS ONLY. path.sep not working yet
102-
var cleanSub = sub.substring(0, folderIndex);
103-
104-
//add any templates found to an object of partials, so downstream templates may use them too
105-
//exclude the template patterns - we don't need them as partials because pages will just swap data
106-
if(cleanSub !== ''){
107-
var partialname = cleanSub + '-' + patternName.substring(patternName.indexOf('-') + 1);
108-
109-
patternlab.partials[partialname] = currentPattern.template;
84+
}
11085

111-
//done
112-
}
113-
}
114-
//add to patternlab arrays so we can look these up later. this could probably just be an object.
115-
patternlab.patternIndex.push(currentPattern.name);
116-
patternlab.patterns.push(currentPattern);
117-
} else{
118-
//if we get here, we can almost ensure we found the json first, so render the template and pass in the unique json
119-
currentPattern = patternlab.patterns[patternIndex];
120-
grunt.verbose.ok('pattern found, loaded');
121-
//determine if this file is data or pattern
122-
if(grunt.util._.str.include(filename, 'mustache')){
86+
currentPattern.template = grunt.file.read(abspath);
12387

124-
currentPattern.template = grunt.file.read(abspath);
88+
//render the pattern. pass partials object just in case.
89+
if(currentPattern.data) { // Pass JSON as data
90+
currentPattern.patternPartial = renderPattern(currentPattern.template, currentPattern.data, patternlab.partials);
91+
}else{ // Pass global patternlab data
92+
currentPattern.patternPartial = renderPattern(currentPattern.template, patternlab.data, patternlab.partials);
93+
}
94+
95+
//write the compiled template to the public patterns directory
96+
flatPatternPath = currentPattern.name + '/' + currentPattern.name + '.html';
12597

126-
//render the pattern. pass partials object just in case.
127-
currentPattern.patternPartial = mustache.render(currentPattern.template, currentPattern.data, patternlab.partials);
128-
grunt.verbose.ok('template compiled with data!');
98+
//add footer info before writing
99+
var currentPatternFooter = renderPattern(patternlab.footer, currentPattern);
129100

130-
//write the compiled template to the public patterns directory
131-
flatPatternPath = currentPattern.name + '/' + currentPattern.name + '.html';
101+
grunt.file.write('./public/patterns/' + flatPatternPath, patternlab.header + currentPattern.patternPartial + currentPatternFooter);
102+
currentPattern.patternLink = flatPatternPath;
132103

133-
//add footer info before writing
134-
var currentPatternFooter = mustache.render(patternlab.footer, currentPattern);
104+
//add as a partial in case this is referenced later. convert to syntax needed by existing patterns
105+
var sub = subdir.substring(subdir.indexOf('-') + 1);
106+
var folderIndex = sub.indexOf('/'); //THIS IS MOST LIKELY WINDOWS ONLY. path.sep not working yet
107+
var cleanSub = sub.substring(0, folderIndex);
135108

136-
grunt.file.write('./public/patterns/' + flatPatternPath, patternlab.header + currentPattern.patternPartial + currentPatternFooter);
109+
//add any templates found to an object of partials, so downstream templates may use them too
110+
//exclude the template patterns - we don't need them as partials because pages will just swap data
111+
if(cleanSub !== ''){
112+
var partialname = cleanSub + '-' + patternName.substring(patternName.indexOf('-') + 1);
137113

138-
currentPattern.patternLink = flatPatternPath;
114+
patternlab.partials[partialname] = currentPattern.template;
139115

140-
//done
141-
} else{
142-
grunt.log.error('json encountered!? there should only be one');
143-
}
116+
//done
144117
}
118+
119+
//add to patternlab arrays so we can look these up later. this could probably just be an object.
120+
patternlab.patternIndex.push(currentPattern.name);
121+
patternlab.patterns.push(currentPattern);
145122

146123
});
147124

@@ -155,7 +132,7 @@ var patternlab_engine = function(grunt){
155132

156133
//build the styleguide
157134
var styleguideTemplate = grunt.file.read('./source/_patternlab-files/styleguide.mustache');
158-
var styleguideHtml = mustache.render(styleguideTemplate, {partials: patternlab.patterns});
135+
var styleguideHtml = renderPattern(styleguideTemplate, {partials: patternlab.patterns});
159136
grunt.file.write('./public/styleguide/html/styleguide.html', styleguideHtml);
160137

161138
//build the patternlab website
@@ -285,29 +262,29 @@ var patternlab_engine = function(grunt){
285262
//the patternlab site requires a lot of partials to be rendered.
286263
//patternNav
287264
var patternNavTemplate = grunt.file.read('./source/_patternlab-files/partials/patternNav.mustache');
288-
var patternNavPartialHtml = mustache.render(patternNavTemplate, patternlab);
265+
var patternNavPartialHtml = renderPattern(patternNavTemplate, patternlab);
289266

290267
//ishControls
291268
var ishControlsTemplate = grunt.file.read('./source/_patternlab-files/partials/ishControls.mustache');
292-
var ishControlsPartialHtml = mustache.render(ishControlsTemplate, patternlab.config);
269+
var ishControlsPartialHtml = renderPattern(ishControlsTemplate, patternlab.config);
293270

294271
//patternPaths
295272
var patternPathsTemplate = grunt.file.read('./source/_patternlab-files/partials/patternPaths.mustache');
296-
var patternPathsPartialHtml = mustache.render(patternPathsTemplate, {'patternPaths': JSON.stringify(patternlab.patternPaths)});
273+
var patternPathsPartialHtml = renderPattern(patternPathsTemplate, {'patternPaths': JSON.stringify(patternlab.patternPaths)});
297274

298275
//viewAllPaths
299276
var viewAllPathsTemplate = grunt.file.read('./source/_patternlab-files/partials/viewAllPaths.mustache');
300-
var viewAllPathersPartialHtml = mustache.render(viewAllPathsTemplate, {'viewallpaths': JSON.stringify(patternlab.viewAllPaths)});
277+
var viewAllPathersPartialHtml = renderPattern(viewAllPathsTemplate, {'viewallpaths': JSON.stringify(patternlab.viewAllPaths)});
301278

302279
//websockets
303280
var websocketsTemplate = grunt.file.read('./source/_patternlab-files/partials/websockets.mustache');
304281
patternlab.contentsyncport = patternlab.config.contentSyncPort;
305282
patternlab.navsyncport = patternlab.config.navSyncPort;
306283

307-
var websocketsPartialHtml = mustache.render(websocketsTemplate, patternlab);
284+
var websocketsPartialHtml = renderPattern(websocketsTemplate, patternlab);
308285

309286
//render the patternlab template, with all partials
310-
var patternlabSiteHtml = mustache.render(patternlabSiteTemplate, {}, {
287+
var patternlabSiteHtml = renderPattern(patternlabSiteTemplate, {}, {
311288
'ishControls': ishControlsPartialHtml,
312289
'patternNav': patternNavPartialHtml,
313290
'patternPaths': patternPathsPartialHtml,
@@ -318,6 +295,14 @@ var patternlab_engine = function(grunt){
318295

319296
}
320297

298+
function renderPattern(name, data, partials) {
299+
if(partials) {
300+
return mustache.render(name, data, partials);
301+
}else{
302+
return mustache.render(name, data);
303+
}
304+
}
305+
321306
return {
322307
version: function(){
323308
return getVersion();

0 commit comments

Comments
 (0)