Skip to content

Commit 27e3c68

Browse files
Merge pull request #460 from pattern-lab/dev
Pattern Lab Node Core 2.5.0
2 parents 9529336 + 5e90b80 commit 27e3c68

9 files changed

+217
-95
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ If this looks **REALLY DIFFERENT** from what you expected, check out the [Change
1515

1616
## Upgrading
1717

18-
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)
18+
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)
1919

2020
## Command Line Interface
2121

core/lib/pattern_assembler.js

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ var pattern_assembler = function () {
117117
}
118118

119119
// do global registration
120-
121-
122120
if (pattern.isPattern) {
123121
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate || pattern.template;
124122

@@ -202,8 +200,63 @@ var pattern_assembler = function () {
202200
}
203201
}
204202

203+
/**
204+
* A helper that unravels a pattern looking for partials or listitems to unravel.
205+
* The goal is really to convert pattern.template into pattern.extendedTemplate
206+
* @param pattern - the pattern to decompose
207+
* @param patternlab - global data store
208+
* @param ignoreLineage - whether or not to hunt for lineage for this pattern
209+
*/
210+
function decomposePattern(pattern, patternlab, ignoreLineage) {
211+
212+
var lineage_hunter = new lh(),
213+
list_item_hunter = new lih();
214+
215+
pattern.extendedTemplate = pattern.template;
216+
217+
//find how many partials there may be for the given pattern
218+
var foundPatternPartials = pattern.findPartials();
219+
220+
//find any listItem blocks that within the pattern, even if there are no partials
221+
list_item_hunter.process_list_item_partials(pattern, patternlab);
222+
223+
// expand any partials present in this pattern; that is, drill down into
224+
// the template and replace their calls in this template with rendered
225+
// results
226+
227+
if (pattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {
228+
// eslint-disable-next-line
229+
expandPartials(foundPatternPartials, list_item_hunter, patternlab, pattern);
230+
231+
// update the extendedTemplate in the partials object in case this
232+
// pattern is consumed later
233+
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate;
234+
}
235+
236+
//find pattern lineage
237+
if (!ignoreLineage) {
238+
lineage_hunter.find_lineage(pattern, patternlab);
239+
}
240+
241+
//add to patternlab object so we can look these up later.
242+
addPattern(pattern, patternlab);
243+
}
244+
205245
function processPatternIterative(relPath, patternlab) {
206246

247+
var relativeDepth = relPath.match(/\w(?=\\)|\w(?=\/)/g || []).length;
248+
if (relativeDepth > 2) {
249+
console.log('');
250+
plutils.logOrange('Warning:');
251+
plutils.logOrange('A pattern file: ' + relPath + ' was found greater than 2 levels deep from ' + patternlab.config.paths.source.patterns + '.');
252+
plutils.logOrange('It\'s strongly suggested to not deviate from the following structure under _patterns/');
253+
plutils.logOrange('[patternType]/[patternSubtype]/[patternName].[patternExtension]');
254+
console.log('');
255+
plutils.logOrange('While Pattern Lab may still function, assets may 404 and frontend links may break. Consider yourself warned. ');
256+
plutils.logOrange('Read More: http://patternlab.io/docs/pattern-organization.html');
257+
console.log('');
258+
}
259+
207260
//check if the found file is a top-level markdown file
208261
var fileObject = path.parse(relPath);
209262
if (fileObject.ext === '.md') {
@@ -323,9 +376,6 @@ var pattern_assembler = function () {
323376

324377
function processPatternRecursive(file, patternlab) {
325378

326-
var lineage_hunter = new lh(),
327-
list_item_hunter = new lih();
328-
329379
//find current pattern in patternlab object using var file as a partial
330380
var currentPattern, i;
331381

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

344-
currentPattern.extendedTemplate = currentPattern.template;
345-
346-
//find how many partials there may be for the given pattern
347-
var foundPatternPartials = currentPattern.findPartials();
348-
349-
//find any listItem blocks that within the pattern, even if there are no partials
350-
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
351-
352-
// expand any partials present in this pattern; that is, drill down into
353-
// the template and replace their calls in this template with rendered
354-
// results
355-
356-
if (currentPattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {
357-
// eslint-disable-next-line
358-
expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern);
359-
360-
// update the extendedTemplate in the partials object in case this
361-
// pattern is consumed later
362-
patternlab.partials[currentPattern.patternPartial] = currentPattern.extendedTemplate;
363-
}
364-
365-
//find pattern lineage
366-
lineage_hunter.find_lineage(currentPattern, patternlab);
367-
368-
//add to patternlab object so we can look these up later.
369-
addPattern(currentPattern, patternlab);
394+
//call our helper method to actually unravel the pattern with any partials
395+
decomposePattern(currentPattern, patternlab);
370396
}
371397

372398
function expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern) {
@@ -400,14 +426,17 @@ var pattern_assembler = function () {
400426
processPatternRecursive(partialPath, patternlab);
401427

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

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

410-
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], partialPattern.extendedTemplate);
439+
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], cleanPartialPattern.extendedTemplate);
411440
}
412441
}
413442

@@ -501,6 +530,9 @@ var pattern_assembler = function () {
501530
addSubtypePattern: function (subtypePattern, patternlab) {
502531
addSubtypePattern(subtypePattern, patternlab);
503532
},
533+
decomposePattern: function (pattern, patternlab, ignoreLineage) {
534+
decomposePattern(pattern, patternlab, ignoreLineage);
535+
},
504536
renderPattern: function (template, data, partials) {
505537
return renderPattern(template, data, partials);
506538
},

core/lib/pattern_engines.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
/*
2-
* patternlab-node - v0.10.1 - 2015
3-
*
4-
* Geoffrey Pursell, Brian Muenzenmeyer, and the web community.
5-
* Licensed under the MIT license.
6-
*
7-
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
8-
*
9-
*/
10-
1+
// special shoutout to Geoffrey Pursell for single-handedly making Pattern Lab Node Pattern Engines possible!
112
'use strict';
123

134
var path = require('path');

core/lib/patternlab.js

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v2.4.4 - 2016
2+
* patternlab-node - v2.5.0 - 2016
33
*
44
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
55
* Licensed under the MIT license.
@@ -17,7 +17,6 @@ var diveSync = require('diveSync'),
1717
plutils = require('./utilities');
1818

1919
function buildPatternData(dataFilesPath, fs) {
20-
var dataFilesPath = dataFilesPath;
2120
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
2221
var mergeObject = {};
2322
dataFiles.forEach(function (filePath) {
@@ -84,8 +83,13 @@ var patternlab_engine = function (config) {
8483
lh = require('./lineage_hunter'),
8584
ui = require('./ui_builder'),
8685
sm = require('./starterkit_manager'),
86+
Pattern = require('./object_factory').Pattern,
8787
patternlab = {};
8888

89+
var pattern_assembler = new pa(),
90+
pattern_exporter = new pe(),
91+
lineage_hunter = new lh();
92+
8993
patternlab.package = fs.readJSONSync(path.resolve(__dirname, '../../package.json'));
9094
patternlab.config = config || fs.readJSONSync(path.resolve(__dirname, '../../patternlab-config.json'));
9195

@@ -191,6 +195,46 @@ var patternlab_engine = function (config) {
191195
starterkit_manager.load_starterkit(starterkitName, clean);
192196
}
193197

198+
/**
199+
* Process the user-defined pattern head and prepare it for rendering
200+
*/
201+
function processHeadPattern() {
202+
try {
203+
var headPath = path.resolve(paths.source.meta, '_00-head.mustache');
204+
var headPattern = new Pattern(headPath, null, patternlab);
205+
headPattern.template = fs.readFileSync(headPath, 'utf8');
206+
headPattern.isPattern = false;
207+
headPattern.isMetaPattern = true;
208+
pattern_assembler.decomposePattern(headPattern, patternlab, true);
209+
patternlab.userHead = headPattern.extendedTemplate;
210+
}
211+
catch (ex) {
212+
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');
213+
if (patternlab.config.debug) { console.log(ex); }
214+
process.exit(1);
215+
}
216+
}
217+
218+
/**
219+
* Process the user-defined pattern footer and prepare it for rendering
220+
*/
221+
function processFootPattern() {
222+
try {
223+
var footPath = path.resolve(paths.source.meta, '_01-foot.mustache');
224+
var footPattern = new Pattern(footPath, null, patternlab);
225+
footPattern.template = fs.readFileSync(footPath, 'utf8');
226+
footPattern.isPattern = false;
227+
footPattern.isMetaPattern = true;
228+
pattern_assembler.decomposePattern(footPattern, patternlab, true);
229+
patternlab.userFoot = footPattern.extendedTemplate;
230+
}
231+
catch (ex) {
232+
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');
233+
if (patternlab.config.debug) { console.log(ex); }
234+
process.exit(1);
235+
}
236+
}
237+
194238
function buildPatterns(deletePatternDir) {
195239
try {
196240
patternlab.data = buildPatternData(paths.source.data, fs);
@@ -222,37 +266,18 @@ var patternlab_engine = function (config) {
222266

223267
setCacheBust();
224268

225-
var pattern_assembler = new pa(),
226-
pattern_exporter = new pe(),
227-
lineage_hunter = new lh(),
228-
patterns_dir = paths.source.patterns;
229-
230269
pattern_assembler.combine_listItems(patternlab);
231270

232271
// diveSync once to perform iterative populating of patternlab object
233-
processAllPatternsIterative(pattern_assembler, patterns_dir, patternlab);
272+
processAllPatternsIterative(pattern_assembler, paths.source.patterns, patternlab);
234273

235274
//diveSync again to recursively include partials, filling out the
236275
//extendedTemplate property of the patternlab.patterns elements
237-
processAllPatternsRecursive(pattern_assembler, patterns_dir, patternlab);
276+
processAllPatternsRecursive(pattern_assembler, paths.source.patterns, patternlab);
238277

239-
//set user defined head and foot if they exist
240-
try {
241-
patternlab.userHead = fs.readFileSync(path.resolve(paths.source.meta, '_00-head.mustache'), 'utf8');
242-
}
243-
catch (ex) {
244-
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');
245-
if (patternlab.config.debug) { console.log(ex); }
246-
process.exit(1);
247-
}
248-
try {
249-
patternlab.userFoot = fs.readFileSync(path.resolve(paths.source.meta, '_01-foot.mustache'), 'utf8');
250-
}
251-
catch (ex) {
252-
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');
253-
if (patternlab.config.debug) { console.log(ex); }
254-
process.exit(1);
255-
}
278+
//take the user defined head and foot and process any data and patterns that apply
279+
processHeadPattern();
280+
processFootPattern();
256281

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

290-
pattern.header = head;
291-
292315
//todo move this into lineage_hunter
293316
pattern.patternLineages = pattern.lineage;
294317
pattern.patternLineageExists = pattern.lineage.length > 0;
@@ -307,14 +330,13 @@ var patternlab_engine = function (config) {
307330
allData = plutils.mergeData(allData, pattern.jsonFileData);
308331
allData.cacheBuster = patternlab.cacheBuster;
309332

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

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

315-
//todo see if this is still needed
316-
//pattern.patternPartialCodeE = entity_encoder.encode(pattern.patternPartialCode);
317-
318340
// stringify this data for individual pattern rendering and use on the styleguide
319341
// see if patternData really needs these other duped values
320342
pattern.patternData = JSON.stringify({
@@ -350,9 +372,17 @@ var patternlab_engine = function (config) {
350372
cacheBuster: patternlab.cacheBuster
351373
});
352374

353-
var footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, {
354-
patternLabFoot : footerPartial
355-
});
375+
var allFooterData;
376+
try {
377+
allFooterData = JSON5.parse(JSON5.stringify(patternlab.data));
378+
} catch (err) {
379+
console.log('There was an error parsing JSON for ' + pattern.relPath);
380+
console.log(err);
381+
}
382+
allFooterData = plutils.mergeData(allFooterData, pattern.jsonFileData);
383+
allFooterData.patternLabFoot = footerPartial;
384+
385+
var footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, allFooterData);
356386

357387
//write the compiled template to the public patterns directory
358388
var patternPage = headHTML + pattern.patternPartialCode + footerHTML;

core/lib/pseudopattern_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var pseudopattern_hunter = function () {
5454

5555
// use the same template engine as the non-variant
5656
engine: currentPattern.engine
57-
});
57+
}, patternlab);
5858

5959
//process the companion markdown file if it exists
6060
pattern_assembler.parse_pattern_markdown(patternVariant, patternlab);

0 commit comments

Comments
 (0)