Skip to content

Pattern Lab Node Core 2.5.0 #460

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 9 commits into from
Sep 1, 2016
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If this looks **REALLY DIFFERENT** from what you expected, check out the [Change

## Upgrading

If you find yourself here and are looking to ugpgrade, check out how to upgrade from version to version of Pattern Lab Node here: [https://github.com/pattern-lab/patternlab-node/wiki/Upgrading](https://github.com/pattern-lab/patternlab-node/wiki/Upgrading)
If you find yourself here and are looking to upgrade, check out how to upgrade from version to version of Pattern Lab Node here: [https://github.com/pattern-lab/patternlab-node/wiki/Upgrading](https://github.com/pattern-lab/patternlab-node/wiki/Upgrading)

## Command Line Interface

Expand Down
98 changes: 65 additions & 33 deletions core/lib/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ var pattern_assembler = function () {
}

// do global registration


if (pattern.isPattern) {
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate || pattern.template;

Expand Down Expand Up @@ -202,8 +200,63 @@ var pattern_assembler = function () {
}
}

/**
* A helper that unravels a pattern looking for partials or listitems to unravel.
* The goal is really to convert pattern.template into pattern.extendedTemplate
* @param pattern - the pattern to decompose
* @param patternlab - global data store
* @param ignoreLineage - whether or not to hunt for lineage for this pattern
*/
function decomposePattern(pattern, patternlab, ignoreLineage) {

var lineage_hunter = new lh(),
list_item_hunter = new lih();

pattern.extendedTemplate = pattern.template;

//find how many partials there may be for the given pattern
var foundPatternPartials = pattern.findPartials();

//find any listItem blocks that within the pattern, even if there are no partials
list_item_hunter.process_list_item_partials(pattern, patternlab);

// expand any partials present in this pattern; that is, drill down into
// the template and replace their calls in this template with rendered
// results

if (pattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {
// eslint-disable-next-line
expandPartials(foundPatternPartials, list_item_hunter, patternlab, pattern);

// update the extendedTemplate in the partials object in case this
// pattern is consumed later
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate;
}

//find pattern lineage
if (!ignoreLineage) {
lineage_hunter.find_lineage(pattern, patternlab);
}

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

function processPatternIterative(relPath, patternlab) {

var relativeDepth = relPath.match(/\w(?=\\)|\w(?=\/)/g || []).length;
if (relativeDepth > 2) {
console.log('');
plutils.logOrange('Warning:');
plutils.logOrange('A pattern file: ' + relPath + ' was found greater than 2 levels deep from ' + patternlab.config.paths.source.patterns + '.');
plutils.logOrange('It\'s strongly suggested to not deviate from the following structure under _patterns/');
plutils.logOrange('[patternType]/[patternSubtype]/[patternName].[patternExtension]');
console.log('');
plutils.logOrange('While Pattern Lab may still function, assets may 404 and frontend links may break. Consider yourself warned. ');
plutils.logOrange('Read More: http://patternlab.io/docs/pattern-organization.html');
console.log('');
}

//check if the found file is a top-level markdown file
var fileObject = path.parse(relPath);
if (fileObject.ext === '.md') {
Expand Down Expand Up @@ -323,9 +376,6 @@ var pattern_assembler = function () {

function processPatternRecursive(file, patternlab) {

var lineage_hunter = new lh(),
list_item_hunter = new lih();

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

Expand All @@ -341,32 +391,8 @@ var pattern_assembler = function () {
//we are processing a markdown only pattern
if (currentPattern.engine === null) { return; }

currentPattern.extendedTemplate = currentPattern.template;

//find how many partials there may be for the given pattern
var foundPatternPartials = currentPattern.findPartials();

//find any listItem blocks that within the pattern, even if there are no partials
list_item_hunter.process_list_item_partials(currentPattern, patternlab);

// expand any partials present in this pattern; that is, drill down into
// the template and replace their calls in this template with rendered
// results

if (currentPattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {
// eslint-disable-next-line
expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern);

// update the extendedTemplate in the partials object in case this
// pattern is consumed later
patternlab.partials[currentPattern.patternPartial] = currentPattern.extendedTemplate;
}

//find pattern lineage
lineage_hunter.find_lineage(currentPattern, patternlab);

//add to patternlab object so we can look these up later.
addPattern(currentPattern, patternlab);
//call our helper method to actually unravel the pattern with any partials
decomposePattern(currentPattern, patternlab);
}

function expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern) {
Expand Down Expand Up @@ -400,14 +426,17 @@ var pattern_assembler = function () {
processPatternRecursive(partialPath, patternlab);

//complete assembly of extended template
//create a copy of the partial so as to not pollute it after the getPartial call.
var partialPattern = getPartial(partial, patternlab);
var cleanPartialPattern = JSON5.parse(JSON5.stringify(partialPattern));
cleanPartialPattern.extendedTemplate = cleanPartialPattern.template;

//if partial has style modifier data, replace the styleModifier value
if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) {
style_modifier_hunter.consume_style_modifier(partialPattern, foundPatternPartials[i], patternlab);
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPatternPartials[i], patternlab);
}

currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], partialPattern.extendedTemplate);
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], cleanPartialPattern.extendedTemplate);
}
}

Expand Down Expand Up @@ -501,6 +530,9 @@ var pattern_assembler = function () {
addSubtypePattern: function (subtypePattern, patternlab) {
addSubtypePattern(subtypePattern, patternlab);
},
decomposePattern: function (pattern, patternlab, ignoreLineage) {
decomposePattern(pattern, patternlab, ignoreLineage);
},
renderPattern: function (template, data, partials) {
return renderPattern(template, data, partials);
},
Expand Down
11 changes: 1 addition & 10 deletions core/lib/pattern_engines.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
/*
* patternlab-node - v0.10.1 - 2015
*
* Geoffrey Pursell, Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
*
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
*
*/

// special shoutout to Geoffrey Pursell for single-handedly making Pattern Lab Node Pattern Engines possible!
'use strict';

var path = require('path');
Expand Down
98 changes: 64 additions & 34 deletions core/lib/patternlab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v2.4.4 - 2016
* patternlab-node - v2.5.0 - 2016
*
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
* Licensed under the MIT license.
Expand All @@ -17,7 +17,6 @@ var diveSync = require('diveSync'),
plutils = require('./utilities');

function buildPatternData(dataFilesPath, fs) {
var dataFilesPath = dataFilesPath;
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
var mergeObject = {};
dataFiles.forEach(function (filePath) {
Expand Down Expand Up @@ -84,8 +83,13 @@ var patternlab_engine = function (config) {
lh = require('./lineage_hunter'),
ui = require('./ui_builder'),
sm = require('./starterkit_manager'),
Pattern = require('./object_factory').Pattern,
patternlab = {};

var pattern_assembler = new pa(),
pattern_exporter = new pe(),
lineage_hunter = new lh();

patternlab.package = fs.readJSONSync(path.resolve(__dirname, '../../package.json'));
patternlab.config = config || fs.readJSONSync(path.resolve(__dirname, '../../patternlab-config.json'));

Expand Down Expand Up @@ -191,6 +195,46 @@ var patternlab_engine = function (config) {
starterkit_manager.load_starterkit(starterkitName, clean);
}

/**
* Process the user-defined pattern head and prepare it for rendering
*/
function processHeadPattern() {
try {
var headPath = path.resolve(paths.source.meta, '_00-head.mustache');
var headPattern = new Pattern(headPath, null, patternlab);
headPattern.template = fs.readFileSync(headPath, 'utf8');
headPattern.isPattern = false;
headPattern.isMetaPattern = true;
pattern_assembler.decomposePattern(headPattern, patternlab, true);
patternlab.userHead = headPattern.extendedTemplate;
}
catch (ex) {
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);
}
}

/**
* Process the user-defined pattern footer and prepare it for rendering
*/
function processFootPattern() {
try {
var footPath = path.resolve(paths.source.meta, '_01-foot.mustache');
var footPattern = new Pattern(footPath, null, patternlab);
footPattern.template = fs.readFileSync(footPath, 'utf8');
footPattern.isPattern = false;
footPattern.isMetaPattern = true;
pattern_assembler.decomposePattern(footPattern, patternlab, true);
patternlab.userFoot = footPattern.extendedTemplate;
}
catch (ex) {
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);
}
}

function buildPatterns(deletePatternDir) {
try {
patternlab.data = buildPatternData(paths.source.data, fs);
Expand Down Expand Up @@ -222,37 +266,18 @@ var patternlab_engine = function (config) {

setCacheBust();

var pattern_assembler = new pa(),
pattern_exporter = new pe(),
lineage_hunter = new lh(),
patterns_dir = paths.source.patterns;

pattern_assembler.combine_listItems(patternlab);

// diveSync once to perform iterative populating of patternlab object
processAllPatternsIterative(pattern_assembler, patterns_dir, patternlab);
processAllPatternsIterative(pattern_assembler, paths.source.patterns, patternlab);

//diveSync again to recursively include partials, filling out the
//extendedTemplate property of the patternlab.patterns elements
processAllPatternsRecursive(pattern_assembler, patterns_dir, patternlab);
processAllPatternsRecursive(pattern_assembler, paths.source.patterns, patternlab);

//set user defined head and foot if they exist
try {
patternlab.userHead = fs.readFileSync(path.resolve(paths.source.meta, '_00-head.mustache'), 'utf8');
}
catch (ex) {
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) {
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);
}
//take the user defined head and foot and process any data and patterns that apply
processHeadPattern();
processFootPattern();

//now that all the main patterns are known, look for any links that might be within data and expand them
//we need to do this before expanding patterns & partials into extendedTemplates, otherwise we could lose the data -> partial reference
Expand Down Expand Up @@ -287,8 +312,6 @@ var patternlab_engine = function (config) {
return false;
}

pattern.header = head;

//todo move this into lineage_hunter
pattern.patternLineages = pattern.lineage;
pattern.patternLineageExists = pattern.lineage.length > 0;
Expand All @@ -307,14 +330,13 @@ var patternlab_engine = function (config) {
allData = plutils.mergeData(allData, pattern.jsonFileData);
allData.cacheBuster = patternlab.cacheBuster;

//re-rendering the headHTML each time allows pattern-specific data to influence the head of the pattern
pattern.header = head;
var headHTML = pattern_assembler.renderPattern(pattern.header, allData);

//render the extendedTemplate with all data
pattern.patternPartialCode = pattern_assembler.renderPattern(pattern, allData);

//todo see if this is still needed
//pattern.patternPartialCodeE = entity_encoder.encode(pattern.patternPartialCode);

// stringify this data for individual pattern rendering and use on the styleguide
// see if patternData really needs these other duped values
pattern.patternData = JSON.stringify({
Expand Down Expand Up @@ -350,9 +372,17 @@ var patternlab_engine = function (config) {
cacheBuster: patternlab.cacheBuster
});

var footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, {
patternLabFoot : footerPartial
});
var allFooterData;
try {
allFooterData = JSON5.parse(JSON5.stringify(patternlab.data));
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.relPath);
console.log(err);
}
allFooterData = plutils.mergeData(allFooterData, pattern.jsonFileData);
allFooterData.patternLabFoot = footerPartial;

var footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, allFooterData);

//write the compiled template to the public patterns directory
var patternPage = headHTML + pattern.patternPartialCode + footerHTML;
Expand Down
2 changes: 1 addition & 1 deletion core/lib/pseudopattern_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var pseudopattern_hunter = function () {

// use the same template engine as the non-variant
engine: currentPattern.engine
});
}, patternlab);

//process the companion markdown file if it exists
pattern_assembler.parse_pattern_markdown(patternVariant, patternlab);
Expand Down
Loading