Skip to content

Commit a14feac

Browse files
committed
initial support for #410
1 parent b79f5c1 commit a14feac

File tree

5 files changed

+141
-88
lines changed

5 files changed

+141
-88
lines changed

core/lib/pattern_assembler.js

+47-31
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,6 +200,48 @@ 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

207247
//check if the found file is a top-level markdown file
@@ -323,9 +363,6 @@ var pattern_assembler = function () {
323363

324364
function processPatternRecursive(file, patternlab) {
325365

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

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

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);
381+
//call our helper method to actually unravel the pattern with any partials
382+
decomposePattern(currentPattern, patternlab);
370383
}
371384

372385
function expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern) {
@@ -501,6 +514,9 @@ var pattern_assembler = function () {
501514
addSubtypePattern: function (subtypePattern, patternlab) {
502515
addSubtypePattern(subtypePattern, patternlab);
503516
},
517+
decomposePattern: function (pattern, patternlab, ignoreLineage) {
518+
decomposePattern(pattern, patternlab, ignoreLineage);
519+
},
504520
renderPattern: function (template, data, partials) {
505521
return renderPattern(template, data, partials);
506522
},

core/lib/pattern_engines.js

+1-10
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

+64-34
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/ui_builder.js

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
var path = require('path');
4+
var JSON5 = require('json5');
45
var fs = require('fs-extra');
56
var ae = require('./annotation_exporter');
67
var of = require('./object_factory');
@@ -98,7 +99,16 @@ var ui_builder = function () {
9899
isOmitted = pattern.relPath.charAt(0) === '_' || pattern.relPath.indexOf('/_') > -1;
99100
if (isOmitted) {
100101
if (patternlab.config.debug) {
101-
console.log('Omitting ' + pattern.patternPartial + ' from styleguide patterns its contained within an underscored directory.');
102+
console.log('Omitting ' + pattern.patternPartial + ' from styleguide patterns because its contained within an underscored directory.');
103+
}
104+
return true;
105+
}
106+
107+
//this pattern is a head or foot pattern
108+
isOmitted = pattern.isMetaPattern;
109+
if (isOmitted) {
110+
if (patternlab.config.debug) {
111+
console.log('Omitting ' + pattern.patternPartial + ' from styleguide patterns because its a meta pattern.');
102112
}
103113
return true;
104114
}
@@ -396,10 +406,17 @@ var ui_builder = function () {
396406
cacheBuster: patternlab.cacheBuster
397407
});
398408

409+
var allFooterData;
410+
try {
411+
allFooterData = JSON5.parse(JSON5.stringify(patternlab.data));
412+
} catch (err) {
413+
console.log('There was an error parsing JSON for patternlab.data');
414+
console.log(err);
415+
}
416+
allFooterData.patternLabFoot = footerPartial;
417+
399418
//then add it to the user footer
400-
var footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, {
401-
patternLabFoot : footerPartial
402-
});
419+
var footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, allFooterData);
403420
return footerHTML;
404421
}
405422

@@ -505,7 +522,6 @@ var ui_builder = function () {
505522
return patterns;
506523
}
507524

508-
509525
/**
510526
* Write out our pattern information for use by the front end
511527
* @param patternlab - global data store
@@ -574,19 +590,19 @@ var ui_builder = function () {
574590
var headerPartial = pattern_assembler.renderPattern(patternlab.header, {
575591
cacheBuster: patternlab.cacheBuster
576592
});
577-
var headerHTML = pattern_assembler.renderPattern(patternlab.userHead, {
578-
patternLabHead : headerPartial,
579-
cacheBuster: patternlab.cacheBuster
580-
});
593+
594+
var headFootData = patternlab.data;
595+
headFootData.patternLabHead = headerPartial;
596+
headFootData.cacheBuster = patternlab.cacheBuster;
597+
var headerHTML = pattern_assembler.renderPattern(patternlab.userHead, headFootData);
581598

582599
//set the pattern-specific footer by compiling the general-footer with data, and then adding it to the meta footer
583600
var footerPartial = pattern_assembler.renderPattern(patternlab.footer, {
584601
patternData: '{}',
585602
cacheBuster: patternlab.cacheBuster
586603
});
587-
var footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, {
588-
patternLabFoot : footerPartial
589-
});
604+
headFootData.patternLabFoot = footerPartial;
605+
var footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, headFootData);
590606

591607
//build the viewall pages
592608
var allPatterns = buildViewAllPages(headerHTML, patternlab, styleguidePatterns);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "patternlab-node",
33
"description": "Pattern Lab is a collection of tools to help you create atomic design systems. This is the node command line interface (CLI).",
4-
"version": "2.4.4",
4+
"version": "2.5.0",
55
"main": "./core/lib/patternlab.js",
66
"dependencies": {
77
"diveSync": "^0.3.0",

0 commit comments

Comments
 (0)