Skip to content

Commit

Permalink
Merge pull request #161 from pattern-lab/dev
Browse files Browse the repository at this point in the history
Dev push for v0.13.0
  • Loading branch information
Brian Muenzenmeyer committed Oct 6, 2015
2 parents 12a40e1 + e6fc96a commit 8a4d4c6
Show file tree
Hide file tree
Showing 26 changed files with 478 additions and 85 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public/fonts/*
public/js/*
public/images/*
public/patterns/*
config.ini
latest-change.txt
patternlab.json
.sass-cache/*
Expand Down
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
language: node_js

node_js:
- 4.1
- 4.0
- 0.12
- 0.11
- 0.10

before_install:
- phantomjs --version
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
THIS CHANGELOG IS AN ATTEMPT TO DOCUMENT CHANGES TO THIS PROJECT.

PL-node-v0.13.0
- FIX: Cleanup an old file and an incorrect entry in the .gitignore file
- CHG: Change order of pattern addition and ~variant pattern addition so they build naturally in the menu.
- THX: Thanks @e2tha-e for the flurry of pull requests!
- CHG: Update data merge function to prioritize handle pattern~variant.json files
- THX: Thanks @e2tha-e for finding, fixing, and unit testing the data merge issue.
- ADD: Support for recursive partial inclusion
- THX: Thanks @e2tha-e for making pattern inclusion a lot more robust. Great work!!!
- FIX: Improvements to style guide menu generation and capitalization.

PL-node-v0.12.0
- ADD: Gulp support arrives with an optional configuration
- ADD: Instructions how to install and run with Gulp
Expand Down
4 changes: 4 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ module.exports = function(grunt) {
src: './builder/patternlab_grunt.js',
dest: './builder/patternlab_grunt.js'
},
patternlab_gulp: {
src: './builder/patternlab_gulp.js',
dest: './builder/patternlab_gulp.js'
},
parameter_hunter: {
src: './builder/parameter_hunter.js',
dest: './builder/parameter_hunter.js'
Expand Down
2 changes: 1 addition & 1 deletion builder/lineage_hunter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v0.12.0 - 2015
* patternlab-node - v0.13.0 - 2015
*
* Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
Expand Down
2 changes: 1 addition & 1 deletion builder/list_item_hunter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v0.12.0 - 2015
* patternlab-node - v0.13.0 - 2015
*
* Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
Expand Down
2 changes: 1 addition & 1 deletion builder/media_hunter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v0.12.0 - 2015
* patternlab-node - v0.13.0 - 2015
*
* Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
Expand Down
22 changes: 16 additions & 6 deletions builder/object_factory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v0.12.0 - 2015
* patternlab-node - v0.13.0 - 2015
*
* Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
Expand All @@ -11,12 +11,16 @@
(function () {
"use strict";

var oPattern = function(subdir, filename, data){
var oPattern = function(abspath, subdir, filename, data){
this.fileName = filename.substring(0, filename.indexOf('.'));
this.abspath = abspath;
this.subdir = subdir;
this.name = subdir.replace(/[\/\\]/g, '-') + '-' + this.fileName; //this is the unique name with the subDir
this.jsonFileData = data || {};
this.patternName = this.fileName.substring(this.fileName.indexOf('-') + 1); //this is the display name for the ui
this.patternName = this.fileName.replace(/^\d*\-/, '');
this.patternDisplayName = this.patternName.split('-').reduce(function(val, working){
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
}, '').trim(); //this is the display name for the ui. strip numeric + hyphen prefixes
this.patternLink = this.name + '/' + this.name + '.html';
this.patternGroup = this.name.substring(this.name.indexOf('-') + 1, this.name.indexOf('-', 4) + 1 - this.name.indexOf('-') + 1);
this.patternSubGroup = subdir.substring(subdir.indexOf('/') + 4);
Expand All @@ -32,7 +36,9 @@

var oBucket = function(name){
this.bucketNameLC = name;
this.bucketNameUC = name.charAt(0).toUpperCase() + name.slice(1);
this.bucketNameUC = name.split('-').reduce(function(val, working){
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
}, '').trim();
this.navItems = [];
this.navItemsIndex = [];
this.patternItems = [];
Expand All @@ -41,15 +47,19 @@

var oNavItem = function(name){
this.sectionNameLC = name;
this.sectionNameUC = name.charAt(0).toUpperCase() + name.slice(1);
this.sectionNameUC = name.split('-').reduce(function(val, working){
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
}, '').trim();
this.navSubItems = [];
this.navSubItemsIndex = [];
};

var oNavSubItem = function(name){
this.patternPath = '';
this.patternPartial = '';
this.patternName = name.charAt(0).toUpperCase() + name.slice(1);
this.patternName = name.split(' ').reduce(function(val, working){
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
}, '').trim();
};

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion builder/parameter_hunter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v0.12.0 - 2015
* patternlab-node - v0.13.0 - 2015
*
* Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
Expand Down
147 changes: 113 additions & 34 deletions builder/pattern_assembler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v0.12.0 - 2015
* patternlab-node - v0.13.0 - 2015
*
* Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
Expand Down Expand Up @@ -44,7 +44,22 @@

function addPattern(pattern, patternlab){
patternlab.data.link[pattern.patternGroup + '-' + pattern.patternName] = '/patterns/' + pattern.patternLink;
patternlab.patterns.push(pattern);

//only push to array if the array doesn't contain this pattern
var isNew = true;
for(var i = 0; i < patternlab.patterns.length; i++){
//so we need the identifier to be unique, which patterns[i].abspath is
if(pattern.abspath === patternlab.patterns[i].abspath){
//if abspath already exists, overwrite that element
patternlab.patterns[i] = pattern;
isNew = false;
break;
}
}
//if the pattern is new, just push to the array
if(isNew){
patternlab.patterns.push(pattern);
}
}

function renderPattern(template, data, partials) {
Expand All @@ -58,23 +73,38 @@
}
}

function processPatternFile(file, patternlab){
function processPatternIterative(file, patternlab){
var fs = require('fs-extra'),
of = require('./object_factory'),
path = require('path');

//extract some information
var abspath = file.substring(2);
var subdir = path.dirname(path.relative(patternlab.config.patterns.source, file)).replace('\\', '/');
var filename = path.basename(file);
var ext = path.extname(filename);

//ignore _underscored patterns, json (for now), and dotfiles
if(filename.charAt(0) === '_' || path.extname(filename) === '.json' || filename.charAt(0) === '.'){
//ignore dotfiles and non-variant .json files
if(filename.charAt(0) === '.' || (ext === '.json' && filename.indexOf('~') === -1)){
return;
}

//make a new Pattern Object
var currentPattern = new of.oPattern(subdir, filename);
var currentPattern = new of.oPattern(file, subdir, filename);

//if file is named in the syntax for variants
if(ext === '.json' && filename.indexOf('~') > -1){
//add current pattern to patternlab object with minimal data
//processPatternRecursive() will run find_pseudopatterns() to fill out
//the object in the next diveSync
addPattern(currentPattern, patternlab);
//no need to process further
return;
}

//can ignore all non-mustache files at this point
if(ext !== '.mustache'){
return;
}

//see if this file has a state
setState(currentPattern, patternlab);
Expand All @@ -83,7 +113,9 @@
try {
var jsonFilename = patternlab.config.patterns.source + currentPattern.subdir + '/' + currentPattern.fileName + ".json";
currentPattern.jsonFileData = fs.readJSONSync(jsonFilename.substring(2));
console.log('found pattern-specific data.json for ' + currentPattern.key);
if(patternlab.config.debug){
console.log('found pattern-specific data.json for ' + currentPattern.key);
}
}
catch(e) {
}
Expand All @@ -92,19 +124,21 @@
try {
var listJsonFileName = patternlab.config.patterns.source + currentPattern.subdir + '/' + currentPattern.fileName + ".listitems.json";
currentPattern.patternSpecificListJson = fs.readJSONSync(listJsonFileName.substring(2));
console.log('found pattern-specific listitems.json for ' + currentPattern.key);
if(patternlab.config.debug){
console.log('found pattern-specific listitems.json for ' + currentPattern.key);
}
}
catch(e) {
}
}

//add the raw template to memory
currentPattern.template = fs.readFileSync(abspath, 'utf8');
currentPattern.template = fs.readFileSync(file, 'utf8');

//our helper function that does a lot of heavy lifting
processPattern(currentPattern, patternlab);
//add currentPattern to patternlab.patterns array
addPattern(currentPattern, patternlab);
}

function processPattern(currentPattern, patternlab, additionalData){
function processPatternRecursive(file, patternlab, additionalData){

var fs = require('fs-extra'),
mustache = require('mustache'),
Expand All @@ -119,6 +153,21 @@
list_item_hunter = new lih(),
pseudopattern_hunter = new pph();

//find current pattern in patternlab object using var file as a key
var currentPattern,
i;

for(i = 0; i < patternlab.patterns.length; i++){
if(patternlab.patterns[i].abspath === file){
currentPattern = patternlab.patterns[i];
}
}

//return if processing an ignored file
if(typeof currentPattern === 'undefined'){
return;
}

currentPattern.extendedTemplate = currentPattern.template;

//find how many partials there may be for the given pattern
Expand All @@ -137,8 +186,23 @@
parameter_hunter.find_parameters(currentPattern, patternlab);

//do something with the regular old partials
for(var i = 0; i < foundPatternPartials.length; i++){
for(i = 0; i < foundPatternPartials.length; i++){
var partialKey = foundPatternPartials[i].replace(/{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ])?}}/g, '$2');
var partialPath;

//identify which pattern this partial corresponds to
for(var j = 0; j < patternlab.patterns.length; j++){
if(patternlab.patterns[j].key === partialKey ||
patternlab.patterns[j].abspath.indexOf(partialKey) > -1)
{
partialPath = patternlab.patterns[j].abspath;
}
}

//recurse through nested partials to fill out this extended template.
processPatternRecursive(partialPath, patternlab);

//complete assembly of extended template
var partialPattern = getpatternbykey(partialKey, patternlab);
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], partialPattern.extendedTemplate);
}
Expand All @@ -148,11 +212,11 @@
//find pattern lineage
lineage_hunter.find_lineage(currentPattern, patternlab);

//look for a pseudo pattern by checking if there is a file containing same name, with ~ in it, ending in .json
pseudopattern_hunter.find_pseudopatterns(currentPattern, patternlab);

//add to patternlab object so we can look these up later.
addPattern(currentPattern, patternlab);

//look for a pseudo pattern by checking if there is a file containing same name, with ~ in it, ending in .json
pseudopattern_hunter.find_pseudopatterns(currentPattern, patternlab);
}

function getpatternbykey(key, patternlab){
Expand All @@ -167,24 +231,39 @@
throw 'Could not find pattern with key ' + key;
}


var self = this;
function mergeData(obj1, obj2) {
for (var p in obj2) {
/**
* Recursively merge properties of two objects.
*
* @param {Object} obj1 If obj1 has properties obj2 doesn't, add to obj2.
* @param {Object} obj2 This object's properties have priority over obj1.
* @returns {Object} obj2
*/
function mergeData(obj1, obj2){
if(typeof obj2 === 'undefined'){
obj2 = {};
}
for(var p in obj1){
try {
// Property in destination object set; update its value.
if ( obj2[p].constructor == Object ) {
obj1[p] = self.merge_data(obj1[p], obj2[p]);

} else {
obj1[p] = obj2[p];
// Only recurse if obj1[p] is an object.
if(obj1[p].constructor === Object){
// Requires 2 objects as params; create obj2[p] if undefined.
if(typeof obj2[p] === 'undefined'){
obj2[p] = {};
}
obj2[p] = mergeData(obj1[p], obj2[p]);
// Pop when recursion meets a non-object. If obj1[p] is a non-object,
// only copy to undefined obj2[p]. This way, obj2 maintains priority.
} else if(typeof obj2[p] === 'undefined'){
obj2[p] = obj1[p];
}
} catch(e) {
// Property in destination object not set; create it and set its value.
obj1[p] = obj2[p];
if(typeof obj2[p] === 'undefined'){
obj2[p] = obj1[p];
}
}
}
return obj1;
return obj2;
}

function buildListItems(patternlab){
Expand Down Expand Up @@ -233,11 +312,11 @@
renderPattern: function(template, data, partials){
return renderPattern(template, data, partials);
},
process_pattern_file: function(file, patternlab){
processPatternFile(file, patternlab);
process_pattern_iterative: function(file, patternlab){
processPatternIterative(file, patternlab);
},
process_pattern: function(pattern, patternlab, additionalData){
processPattern(pattern, patternlab, additionalData);
process_pattern_recursive: function(file, patternlab, additionalData){
processPatternRecursive(file, patternlab, additionalData);
},
get_pattern_by_key: function(key, patternlab){
return getpatternbykey(key, patternlab);
Expand Down
2 changes: 1 addition & 1 deletion builder/pattern_exporter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v0.12.0 - 2015
* patternlab-node - v0.13.0 - 2015
*
* Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
Expand Down
Loading

0 comments on commit 8a4d4c6

Please sign in to comment.