Skip to content

Commit

Permalink
Merge pull request #427 from pattern-lab/dev
Browse files Browse the repository at this point in the history
Pattern Lab Node Core 2.4.0
  • Loading branch information
bmuenzenmeyer authored Aug 15, 2016
2 parents 547c565 + 9baf45d commit f77f635
Show file tree
Hide file tree
Showing 30 changed files with 921 additions and 601 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ branches:
only:
- master
- dev
- dev-2.0-core

notifications:
webhooks:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/pattern-lab/patternlab-node.png?branch=master)](https://travis-ci.org/pattern-lab/patternlab-node) ![current release](https://img.shields.io/github/release/pattern-lab/patternlab-node.svg?maxAge=2592000) ![license](https://img.shields.io/github/license/pattern-lab/patternlab-node.svg?maxAge=2592000) [![Join the chat at Gitter](https://badges.gitter.im/pattern-lab/node.svg)](https://gitter.im/pattern-lab/node)
[![Build Status](https://travis-ci.org/pattern-lab/patternlab-node.png?branch=master)](https://travis-ci.org/pattern-lab/patternlab-node) ![current release](https://img.shields.io/github/release/pattern-lab/patternlab-node.svg) ![license](https://img.shields.io/github/license/pattern-lab/patternlab-node.svg) [![Join the chat at Gitter](https://badges.gitter.im/pattern-lab/node.svg)](https://gitter.im/pattern-lab/node)

# Pattern Lab Node Core

Expand Down
6 changes: 3 additions & 3 deletions core/lib/lineage_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var lineage_hunter = function () {
if (matches !== null) {
matches.forEach(function (match) {
//get the ancestorPattern
var ancestorPattern = pattern_assembler.findPartial(pattern.findPartial(match), patternlab);
var ancestorPattern = pattern_assembler.getPartial(pattern.findPartial(match), patternlab);

if (ancestorPattern && pattern.lineageIndex.indexOf(ancestorPattern.patternPartial) === -1) {
//add it since it didnt exist
Expand Down Expand Up @@ -83,7 +83,7 @@ var lineage_hunter = function () {

//find all lineage - patterns being consumed by this one
for (var h = 0; h < pattern.lineageIndex.length; h++) {
var lineagePattern = pattern_assembler.findPartial(pattern.lineageIndex[h], patternlab);
var lineagePattern = pattern_assembler.getPartial(pattern.lineageIndex[h], patternlab);
setPatternState('fromFuture', lineagePattern, pattern);
}
}
Expand All @@ -93,7 +93,7 @@ var lineage_hunter = function () {
//find all reverse lineage - that is, patterns consuming this one
for (var j = 0; j < pattern.lineageRIndex.length; j++) {

var lineageRPattern = pattern_assembler.findPartial(pattern.lineageRIndex[j], patternlab);
var lineageRPattern = pattern_assembler.getPartial(pattern.lineageRIndex[j], patternlab);

//only set patternState if pattern.patternState "is less than" the lineageRPattern.patternstate
//or if lineageRPattern.patternstate (the consuming pattern) does not have a state
Expand Down
2 changes: 1 addition & 1 deletion core/lib/list_item_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var list_item_hunter = function () {

//get the partial
var partialName = foundPartials[j].match(/([\w\-\.\/~]+)/g)[0];
var partialPattern = pattern_assembler.findPartial(partialName, patternlab);
var partialPattern = pattern_assembler.getPartial(partialName, patternlab);

//create a copy of the partial so as to not pollute it after the get_pattern_by_key call.
var cleanPartialPattern;
Expand Down
40 changes: 19 additions & 21 deletions core/lib/markdown_parser.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
"use strict";

var md = require('markdown-it')();
var yaml = require('js-yaml');

var markdown_parser = function () {

/**
* Converts a markdown block with frontmatter (each is optional, technically) to a well-formed object.
* @param block - the ".md" file, which can contain frontmatter or not, or only frontmatter.
* @returns an object with any frontmatter keys, plus a .markdown key
*/
function parseMarkdownBlock(block) {
var returnObject = {};

try {
//for each block process the yaml frontmatter and markdown
var frontmatterRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
var chunks = frontmatterRE.exec(block);
if (chunks && chunks[1]) {

//convert each yaml frontmatter key / value into an object key
var frontmatter = chunks[1];
var frontmatterLines = frontmatter.split(/\n/gm);
for (var j = 0; j < frontmatterLines.length; j++) {

var frontmatterLine = frontmatterLines[j];
if (frontmatterLine.length > 0) {

var frontmatterLineChunks = frontmatterLine.split(':'); //test this
var frontmatterKey = frontmatterLineChunks[0].toLowerCase().trim();
var frontmatterValueString = frontmatterLineChunks[1].trim();

returnObject[frontmatterKey] = frontmatterValueString;
}

if (chunks) {
//we got some frontmatter
if (chunks && chunks[1]) {
//parse the yaml if we got it
var frontmatter = chunks[1];
returnObject = yaml.safeLoad(frontmatter);
}
}

if (chunks && chunks[2]) {
//parse the actual markdown
returnObject.markdown = md.render(chunks[2]);
if (chunks[2]) {
//parse the actual markdown if it exists
returnObject.markdown = md.render(chunks[2]);
} else {
returnObject.markdown = '';
}
} else {
//assume the passed in block is raw markdown
//assume the block was only markdown
returnObject.markdown = md.render(block);
}
} catch (ex) {
Expand Down
48 changes: 10 additions & 38 deletions core/lib/object_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Pattern = function (relPath, data) {
this.fileExtension = pathObj.ext; // '.mustache'

// this is the unique name, subDir + fileName (sans extension)
this.name = this.subdir.replace(/[\/\\]/g, '-') + '-' + this.fileName.replace('~', '-'); // '00-atoms-00-global-00-colors'
this.name = this.subdir.replace(path.sep, '-') + '-' + this.fileName.replace('~', '-'); // '00-atoms-00-global-00-colors'

// the JSON used to render values in the pattern
this.jsonFileData = data || {};
Expand All @@ -36,17 +36,24 @@ var Pattern = function (relPath, data) {
// the top-level pattern group this pattern belongs to. 'atoms'
this.patternGroup = this.subdir.split(path.sep)[0].replace(/^\d*-/, '');

//00-atoms if needed
this.patternType = this.subdir.split(path.sep)[0];

// the sub-group this pattern belongs to.
this.patternSubGroup = path.basename(this.subdir).replace(/^\d*-/, ''); // 'global'

// Not sure what this is used for.
//00-colors if needed
this.patternSubType = path.basename(this.subdir);

// the joined pattern group and subgroup directory
this.flatPatternPath = this.subdir.replace(/[\/\\]/g, '-'); // '00-atoms-00-global'

// The canonical "key" by which this pattern is known. This is the callable
// name of the pattern. UPDATE: this.key is now known as this.patternPartial
this.patternPartial = this.patternGroup + '-' + this.patternBaseName;

this.isPattern = true;
this.isFlatPattern = this.patternGroup === this.patternSubGroup;
this.patternState = '';
this.template = '';
this.patternPartialCode = '';
Expand Down Expand Up @@ -117,41 +124,6 @@ Pattern.create = function (relPath, data, customProps) {
return extend(newPattern, customProps);
};


var oPatternType = function (name) {
this.patternTypeLC = name;
this.patternTypeUC = name.split('-').reduce(function (val, working) {
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
}, '').trim();
this.patternTypeItems = [];
this.patternTypeItemsIndex = [];
this.patternItems = [];
this.patternItemsIndex = [];
};


var oPatternSubType = function (name) {
this.patternSubtypeLC = name;
this.patternSubtypeUC = name.split('-').reduce(function (val, working) {
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
}, '').trim();
this.patternSubtypeItems = [];
this.patternSubtypeItemsIndex = [];
};


var oPatternSubTypeItem = function (name) {
this.patternPath = '';
this.patternPartialCode = '';
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 = {
Pattern: Pattern,
oPatternType: oPatternType,
oPatternSubType: oPatternSubType,
oPatternSubTypeItem: oPatternSubTypeItem
Pattern: Pattern
};
2 changes: 1 addition & 1 deletion core/lib/parameter_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ var parameter_hunter = function () {
pattern.parameteredPartials.forEach(function (pMatch) {
//find the partial's name and retrieve it
var partialName = pMatch.match(/([\w\-\.\/~]+)/g)[0];
var partialPattern = pattern_assembler.findPartial(partialName, patternlab);
var partialPattern = pattern_assembler.getPartial(partialName, patternlab);

//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #190
partialPattern.extendedTemplate = partialPattern.template;
Expand Down
14 changes: 8 additions & 6 deletions core/lib/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ var pattern_assembler = function () {
//else look by verbose syntax
for (var i = 0; i < patternlab.patterns.length; i++) {
switch (partialName) {
case patternlab.patterns[i].relPath:
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName:
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName + '.mustache':
return patternlab.patterns[i];
}
}
Expand All @@ -45,7 +45,8 @@ var pattern_assembler = function () {
return patternlab.patterns[i];
}
}
throw 'Could not find pattern with partial ' + partialName;
console.error('Could not find pattern with partial ' + partialName);
return undefined;
}

function buildListItems(container) {
Expand Down Expand Up @@ -91,7 +92,7 @@ var pattern_assembler = function () {
function addPattern(pattern, patternlab) {

//add the link to the global object
patternlab.data.link[pattern.patternGroup + '-' + pattern.patternBaseName] = '/patterns/' + pattern.patternLink;
patternlab.data.link[pattern.patternPartial] = '/patterns/' + pattern.patternLink;

//only push to array if the array doesn't contain this pattern
var isNew = true;
Expand Down Expand Up @@ -214,7 +215,7 @@ var pattern_assembler = function () {
subTypePattern.patternSectionSubtype = true;
subTypePattern.patternLink = subTypePattern.name + '/index.html';
subTypePattern.patternDesc = subTypeMarkdown.markdown;
subTypePattern.patternPartial = 'viewall-' + subTypePattern.patternPartial;
subTypePattern.flatPatternPath = subTypePattern.flatPatternPath + '-' + subTypePattern.fileName;
subTypePattern.isPattern = false;
subTypePattern.engine = null;

Expand Down Expand Up @@ -417,8 +418,9 @@ var pattern_assembler = function () {

if (linkMatches) {
for (var i = 0; i < linkMatches.length; i++) {
expandedLink = patternlab.data.link[linkMatches[i].split('.')[1]];
expandedLink = encodeURI(patternlab.data.link[linkMatches[i].split('.')[1]]);
if (expandedLink) {
expandedLink = expandedLink.replace('\\', '/');
if (patternlab.config.debug) {
console.log('expanded data link from ' + linkMatches[i] + ' to ' + expandedLink + ' inside ' + key);
}
Expand Down Expand Up @@ -482,7 +484,7 @@ var pattern_assembler = function () {
process_pattern_recursive: function (file, patternlab, additionalData) {
processPatternRecursive(file, patternlab, additionalData);
},
findPartial: function (partial, patternlab) {
getPartial: function (partial, patternlab) {
return getPartial(partial, patternlab);
},
combine_listItems: function (patternlab) {
Expand Down
53 changes: 15 additions & 38 deletions core/lib/patternlab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v2.3.0 - 2016
* patternlab-node - v2.4.0 - 2016
*
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
* Licensed under the MIT license.
Expand All @@ -21,27 +21,17 @@ var diveSync = require('diveSync'),
function buildPatternData(dataFilesPath, fs) {
var dataFilesPath = dataFilesPath;
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
var mergeObject = {}
var mergeObject = {};
dataFiles.forEach(function (filePath) {
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8')
mergeObject = _.merge(mergeObject, jsonData)
})
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8');
mergeObject = _.merge(mergeObject, jsonData);
});
return mergeObject;
}

function processAllPatternsIterative(pattern_assembler, patterns_dir, patternlab) {
diveSync(
patterns_dir,
{
filter: function (thisPath, dir) {
if (dir) {
var remainingPath = thisPath.replace(patterns_dir, '');
var isValidPath = remainingPath.indexOf('/_') === -1;
return isValidPath;
}
return true;
}
},
function (err, file) {
//log any errors
if (err) {
Expand All @@ -56,16 +46,6 @@ function processAllPatternsIterative(pattern_assembler, patterns_dir, patternlab
function processAllPatternsRecursive(pattern_assembler, patterns_dir, patternlab) {
diveSync(
patterns_dir,
{
filter: function (thisPath, dir) {
if (dir) {
var remainingPath = thisPath.replace(patterns_dir, '');
var isValidPath = remainingPath.indexOf('/_') === -1;
return isValidPath;
}
return true;
}
},
function (err, file) {
//log any errors
if (err) {
Expand All @@ -85,7 +65,7 @@ var patternlab_engine = function (config) {
pa = require('./pattern_assembler'),
pe = require('./pattern_exporter'),
lh = require('./lineage_hunter'),
buildFrontEnd = require('./ui_builder'),
ui = require('./ui_builder'),
plutils = require('./utilities'),
sm = require('./starterkit_manager'),
patternlab = {};
Expand All @@ -99,7 +79,6 @@ var patternlab_engine = function (config) {
console.log(patternlab.package.version);
}


function help() {

console.log('');
Expand Down Expand Up @@ -185,7 +164,7 @@ var patternlab_engine = function (config) {
}

function listStarterkits() {
var starterkit_manager = new sm(patternlab);
var starterkit_manager = new sm(patternlab.config);
return starterkit_manager.list_starterkits();
}

Expand Down Expand Up @@ -244,19 +223,17 @@ var patternlab_engine = function (config) {
patternlab.userHead = fs.readFileSync(path.resolve(paths.source.meta, '_00-head.mustache'), 'utf8');
}
catch (ex) {
if (patternlab.config.debug) {
console.log(ex);
console.log('Could not find optional user-defined header, usually found at ./source/_meta/_00-head.mustache. It was likely deleted.');
}
plutils.logRed('\nWARNING: Could not find the user-editable header template, currently configured to be at ' + path.join(config.paths.source.meta, '_00-head.mustache') + '. Your configured path may be incorrect (check paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.\n');
if (patternlab.config.debug) { console.log(ex); }
process.exit(1);
}
try {
patternlab.userFoot = fs.readFileSync(path.resolve(paths.source.meta, '_01-foot.mustache'), 'utf8');
}
catch (ex) {
if (patternlab.config.debug) {
console.log(ex);
console.log('Could not find optional user-defined footer, usually found at ./source/_meta/_01-foot.mustache. It was likely deleted.');
}
plutils.logRed('\nWARNING: Could not find the user-editable footer template, currently configured to be at ' + path.join(config.paths.source.meta, '_01-foot.mustache') + '. Your configured path may be incorrect (check paths.source.meta in your config file), the file may have been deleted, or it may have been left in the wrong place during a migration or update.\n');
if (patternlab.config.debug) { console.log(ex); }
process.exit(1);
}

//now that all the main patterns are known, look for any links that might be within data and expand them
Expand All @@ -275,7 +252,7 @@ var patternlab_engine = function (config) {
//set pattern-specific header if necessary
var head;
if (patternlab.userHead) {
head = patternlab.userHead.replace('{% pattern-lab-head %}', patternlab.header);
head = patternlab.userHead;
} else {
head = patternlab.header;
}
Expand Down Expand Up @@ -389,7 +366,7 @@ var patternlab_engine = function (config) {
},
build: function (callback, deletePatternDir) {
buildPatterns(deletePatternDir);
buildFrontEnd(patternlab);
new ui().buildFrontend(patternlab);
printDebug();
callback();
},
Expand Down
Loading

0 comments on commit f77f635

Please sign in to comment.