Skip to content

Abstracted template rendering; fixed issue with JSON files order in filesystem #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 62 additions & 77 deletions builder/patternlab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v0.1.2 - 2014-07-12
* patternlab-node - v0.1.2 - 2014-07-15
*
* Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
Expand Down Expand Up @@ -61,87 +61,64 @@ var patternlab_engine = function(grunt){
var currentPattern;
var flatPatternPath;

//ignore _underscored patterns
if(filename.charAt(0) === '_'){
//ignore _underscored patterns and json
if(filename.charAt(0) === '_' || grunt.util._.str.include(filename, 'json')){
return;
}


//make a new Pattern Object
var flatPatternName = subdir.replace(/\//g, '-') + '-' + patternName;
flatPatternName = flatPatternName.replace(/\//g, '-');
currentPattern = new of.oPattern(flatPatternName, subdir, filename, {});
currentPattern.patternName = patternName.substring(patternName.indexOf('-') + 1);
currentPattern.data = null;

//look for a json file for this template
try {
var jsonFilename = abspath.substr(0, abspath.lastIndexOf(".")) + ".json";
currentPattern.data = grunt.file.readJSON(jsonFilename);
}
catch(e) {

//two reasons could return no pattern, 1) just a bare mustache, or 2) a json found before the mustache
//returns -1 if patterns does not exist, otherwise returns the index
//add the pattern array if first time, otherwise pull it up
if(patternIndex === -1){
grunt.verbose.ok('pattern not found, adding to array');
var flatPatternName = subdir.replace(/\//g, '-') + '-' + patternName;
flatPatternName = flatPatternName.replace(/\//g, '-');
currentPattern = new of.oPattern(flatPatternName, subdir, filename, {});
currentPattern.patternName = patternName.substring(patternName.indexOf('-') + 1);

if(grunt.util._.str.include(filename, 'json')){
grunt.verbose.ok('json file found first, so add it to the pattern and continuing');
currentPattern.data = grunt.file.readJSON(abspath);
//done
} else{
grunt.verbose.ok('mustache file found, assume no data, so compile it right away');
currentPattern.template = grunt.file.read(abspath);

//render the pattern. pass partials object just in case.
currentPattern.patternPartial = mustache.render(currentPattern.template, patternlab.data, patternlab.partials);

//write the compiled template to the public patterns directory
flatPatternPath = currentPattern.name + '/' + currentPattern.name + '.html';

//add footer info before writing
var currentPatternFooter = mustache.render(patternlab.footer, currentPattern);

grunt.file.write('./public/patterns/' + flatPatternPath, patternlab.header + currentPattern.patternPartial + currentPatternFooter);
currentPattern.patternLink = flatPatternPath;

//add as a partial in case this is referenced later. convert to syntax needed by existing patterns
var sub = subdir.substring(subdir.indexOf('-') + 1);
var folderIndex = sub.indexOf('/'); //THIS IS MOST LIKELY WINDOWS ONLY. path.sep not working yet
var cleanSub = sub.substring(0, folderIndex);

//add any templates found to an object of partials, so downstream templates may use them too
//exclude the template patterns - we don't need them as partials because pages will just swap data
if(cleanSub !== ''){
var partialname = cleanSub + '-' + patternName.substring(patternName.indexOf('-') + 1);

patternlab.partials[partialname] = currentPattern.template;
}

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

currentPattern.template = grunt.file.read(abspath);
//render the pattern. pass partials object just in case.
if(currentPattern.data) { // Pass JSON as data
currentPattern.patternPartial = renderPattern(currentPattern.template, currentPattern.data, patternlab.partials);
}else{ // Pass global patternlab data
currentPattern.patternPartial = renderPattern(currentPattern.template, patternlab.data, patternlab.partials);
}

//write the compiled template to the public patterns directory
flatPatternPath = currentPattern.name + '/' + currentPattern.name + '.html';

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

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

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

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

currentPattern.patternLink = flatPatternPath;
patternlab.partials[partialname] = currentPattern.template;

//done
} else{
grunt.log.error('json encountered!? there should only be one');
}
//done
}

//add to patternlab arrays so we can look these up later. this could probably just be an object.
patternlab.patternIndex.push(currentPattern.name);
patternlab.patterns.push(currentPattern);

});

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

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

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

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

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

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

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

var websocketsPartialHtml = mustache.render(websocketsTemplate, patternlab);
var websocketsPartialHtml = renderPattern(websocketsTemplate, patternlab);

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

}

function renderPattern(name, data, partials) {
if(partials) {
return mustache.render(name, data, partials);
}else{
return mustache.render(name, data);
}
}

return {
version: function(){
return getVersion();
Expand Down