Skip to content

Commit d7cffee

Browse files
author
Brian Muenzenmeyer
authored
Merge pull request #403 from pattern-lab/dev
Pattern Lab Node v2.3.0
2 parents 65c108c + 5776439 commit d7cffee

12 files changed

+105
-61
lines changed

core/lib/object_factory.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ var extend = require('util')._extend;
99
var Pattern = function (relPath, data) {
1010
// We expect relPath to be the path of the pattern template, relative to the
1111
// root of the pattern tree. Parse out the path parts and save the useful ones.
12-
var pathObj = path.parse(relPath);
13-
this.relPath = relPath; // '00-atoms/00-global/00-colors.mustache'
12+
var pathObj = path.parse(path.normalize(relPath));
13+
this.relPath = path.normalize(relPath); // '00-atoms/00-global/00-colors.mustache'
1414
this.fileName = pathObj.name; // '00-colors'
1515
this.subdir = pathObj.dir; // '00-atoms/00-global'
1616
this.fileExtension = pathObj.ext; // '.mustache'
@@ -31,10 +31,10 @@ var Pattern = function (relPath, data) {
3131

3232
// calculated path from the root of the public directory to the generated html
3333
// file for this pattern
34-
this.patternLink = this.name + '/' + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'
34+
this.patternLink = this.name + path.sep + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'
3535

3636
// the top-level pattern group this pattern belongs to. 'atoms'
37-
this.patternGroup = this.name.substring(this.name.indexOf('-') + 1, this.name.indexOf('-', 4) + 1 - this.name.indexOf('-') + 1);
37+
this.patternGroup = this.subdir.split(path.sep)[0].replace(/^\d*-/, '');
3838

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

core/lib/parameter_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ var parameter_hunter = function () {
202202
}
203203

204204
//colon delimiter.
205-
paramStringWellFormed += ':'; + values[i];
205+
paramStringWellFormed += ':';
206206

207207
//values
208208
//replace single-quote wrappers with double-quotes

core/lib/patternlab.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1-
/*
2-
* patternlab-node - v2.2.1 - 2016
3-
*
1+
/*
2+
* patternlab-node - v2.2.1 - 2016
3+
*
44
* Brian Muenzenmeyer, Geoff Pursell, 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.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
88
*
99
*/
1010

1111
"use strict";
1212

1313
var diveSync = require('diveSync'),
14+
glob = require('glob'),
15+
_ = require('lodash'),
1416
path = require('path');
1517

1618
// GTP: these two diveSync pattern processors factored out so they can be reused
1719
// from unit tests to reduce code dupe!
1820

21+
function buildPatternData(dataFilesPath, fs) {
22+
var dataFilesPath = dataFilesPath;
23+
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
24+
var mergeObject = {}
25+
dataFiles.forEach(function (filePath) {
26+
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8')
27+
mergeObject = _.merge(mergeObject, jsonData)
28+
})
29+
return mergeObject;
30+
}
31+
1932
function processAllPatternsIterative(pattern_assembler, patterns_dir, patternlab) {
2033
diveSync(
2134
patterns_dir,
@@ -183,7 +196,7 @@ var patternlab_engine = function (config) {
183196

184197
function buildPatterns(deletePatternDir) {
185198
try {
186-
patternlab.data = fs.readJSONSync(path.resolve(paths.source.data, 'data.json'));
199+
patternlab.data = buildPatternData(paths.source.data, fs);
187200
} catch (ex) {
188201
plutils.logRed('missing or malformed' + paths.source.data + 'data.json Pattern Lab may not work without this file.');
189202
patternlab.data = {};
@@ -327,7 +340,7 @@ var patternlab_engine = function (config) {
327340
patternType: pattern.patternGroup,
328341
patternSubtype: pattern.patternSubGroup
329342
},
330-
patternExtension: pattern.fileExtension,
343+
patternExtension: pattern.fileExtension.substr(1), //remove the dot because styleguide asset default adds it for us
331344
patternName: pattern.patternName,
332345
patternPartial: pattern.patternPartial,
333346
patternState: pattern.patternState,
@@ -345,15 +358,23 @@ var patternlab_engine = function (config) {
345358
patternLabFoot : footerPartial
346359
});
347360

361+
//default the output suffixes if not present
362+
var outputFileSuffixes = {
363+
rendered: '',
364+
rawTemplate: '',
365+
markupOnly: '.markup-only'
366+
}
367+
outputFileSuffixes = _.extend(outputFileSuffixes, patternlab.config.outputFileSuffixes);
368+
348369
//write the compiled template to the public patterns directory
349370
var patternPage = headHTML + pattern.patternPartialCode + footerHTML;
350-
fs.outputFileSync(paths.public.patterns + pattern.patternLink, patternPage);
371+
fs.outputFileSync(paths.public.patterns + pattern.patternLink.replace('.html', outputFileSuffixes.rendered + '.html'), patternPage);
351372

352373
//write the mustache file too
353-
fs.outputFileSync(paths.public.patterns + pattern.patternLink.replace('.html', pattern.fileExtension), pattern.template);
374+
fs.outputFileSync(paths.public.patterns + pattern.patternLink.replace('.html', outputFileSuffixes.rawTemplate + pattern.fileExtension), pattern.template);
354375

355376
//write the encoded version too
356-
fs.outputFileSync(paths.public.patterns + pattern.patternLink.replace('.html', '.markup-only.html'), pattern.patternPartialCode);
377+
fs.outputFileSync(paths.public.patterns + pattern.patternLink.replace('.html', outputFileSuffixes.markupOnly + '.html'), pattern.patternPartialCode);
357378

358379
return true;
359380
});
@@ -392,6 +413,7 @@ var patternlab_engine = function (config) {
392413
// export these free functions so they're available without calling the exported
393414
// function, for use in reducing code dupe in unit tests. At least, until we
394415
// have a better way to do this
416+
patternlab_engine.build_pattern_data = buildPatternData;
395417
patternlab_engine.process_all_patterns_iterative = processAllPatternsIterative;
396418
patternlab_engine.process_all_patterns_recursive = processAllPatternsRecursive;
397419

core/lib/ui_builder.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ var eol = require('os').EOL;
1111

1212
// PRIVATE FUNCTIONS
1313

14-
function addToPatternPaths(patternlab, patternTypeName, pattern) {
15-
//this is messy, could use a refactor.
16-
if (!patternlab.patternPaths[patternTypeName]) {
17-
patternlab.patternPaths[patternTypeName] = {};
14+
function addToPatternPaths(patternlab, pattern) {
15+
if (!patternlab.patternPaths[pattern.patternGroup]) {
16+
patternlab.patternPaths[pattern.patternGroup] = {};
1817
}
19-
patternlab.patternPaths[patternTypeName][pattern.patternBaseName] = pattern.subdir.replace(/\\/g, '/') + "/" + pattern.fileName.replace('~', '-');
18+
patternlab.patternPaths[pattern.patternGroup][pattern.patternBaseName] = pattern.name;
2019
}
2120

2221
//todo: refactor this as a method on the pattern object itself once we merge dev with pattern-engines branch
@@ -91,9 +90,6 @@ function buildNavigation(patternlab) {
9190

9291
var pattern = patternlab.patterns[i];
9392

94-
//todo: check if this is already available
95-
var patternTypeName = pattern.name.replace(/\\/g, '-').split('-')[1];
96-
9793
//exclude any named defaultPattern from the navigation.
9894
//this is meant to be a homepage that is not navigable
9995
if (pattern.patternPartial === patternlab.config.defaultPattern) {
@@ -102,7 +98,7 @@ function buildNavigation(patternlab) {
10298
}
10399

104100
//add to patternPaths before continuing
105-
addToPatternPaths(patternlab, patternTypeName, pattern);
101+
addToPatternPaths(patternlab, pattern);
106102

107103
continue;
108104
}
@@ -134,17 +130,17 @@ function buildNavigation(patternlab) {
134130
patternSubTypeItem.patternPartial = pattern.patternPartial;
135131

136132
//check if the patternType already exists
137-
var patternTypeIndex = patternlab.patternTypeIndex.indexOf(patternTypeName);
133+
var patternTypeIndex = patternlab.patternTypeIndex.indexOf(pattern.patternGroup);
138134
if (patternTypeIndex === -1) {
139135
//add the patternType
140-
var patternType = new of.oPatternType(patternTypeName);
136+
var patternType = new of.oPatternType(pattern.patternGroup);
141137

142138
//add patternPath and viewAllPath
143-
patternlab.patternPaths[patternTypeName] = patternlab.patternPaths[patternTypeName] || {};
144-
patternlab.viewAllPaths[patternTypeName] = {};
139+
patternlab.patternPaths[pattern.patternGroup] = patternlab.patternPaths[pattern.patternGroup] || {};
140+
patternlab.viewAllPaths[pattern.patternGroup] = {};
145141

146142
//test whether the pattern structure is flat or not - usually due to a template or page
147-
flatPatternItem = patternSubTypeName === patternTypeName;
143+
flatPatternItem = patternSubTypeName === pattern.patternGroup;
148144

149145
//assume the patternSubType does not exist.
150146
patternSubType = new of.oPatternSubType(patternSubTypeName);
@@ -160,7 +156,7 @@ function buildNavigation(patternlab) {
160156
patternType.patternItems.push(patternSubTypeItem);
161157

162158
//add to patternPaths
163-
addToPatternPaths(patternlab, patternTypeName, pattern);
159+
addToPatternPaths(patternlab, pattern);
164160

165161
} else {
166162

@@ -170,21 +166,21 @@ function buildNavigation(patternlab) {
170166
patternSubType.patternSubtypeItemsIndex.push(patternSubTypeItemName);
171167

172168
//add to patternPaths
173-
addToPatternPaths(patternlab, patternTypeName, pattern);
169+
addToPatternPaths(patternlab, pattern);
174170

175171
//add the view all PatternSubTypeItem
176172
viewAllPatternSubTypeItem = new of.oPatternSubTypeItem("View All");
177173
viewAllPatternSubTypeItem.patternPath = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length) + "/index.html";
178174
viewAllPatternSubTypeItem.patternPartial = "viewall-" + pattern.patternGroup;
179175

180176
patternType.patternItems.push(viewAllPatternSubTypeItem);
181-
patternlab.viewAllPaths[patternTypeName].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length);
177+
patternlab.viewAllPaths[pattern.patternGroup].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length);
182178

183179
}
184180

185181
//add the patternType.
186182
patternlab.patternTypes.push(patternType);
187-
patternlab.patternTypeIndex.push(patternTypeName);
183+
patternlab.patternTypeIndex.push(pattern.patternGroup);
188184

189185
//done
190186

@@ -198,15 +194,15 @@ function buildNavigation(patternlab) {
198194
}
199195

200196
//test whether the pattern structure is flat or not - usually due to a template or page
201-
flatPatternItem = patternSubTypeName === patternTypeName;
197+
flatPatternItem = patternSubTypeName === pattern.patternGroup;
202198

203199
//if it is flat - we should not add the pattern to patternPaths
204200
if (flatPatternItem) {
205201
//add the patternSubType to patternItems
206202
patternType.patternItems.push(patternSubTypeItem);
207203

208204
//add to patternPaths
209-
addToPatternPaths(patternlab, patternTypeName, pattern);
205+
addToPatternPaths(patternlab, pattern);
210206

211207
} else {
212208

@@ -242,11 +238,11 @@ function buildNavigation(patternlab) {
242238
}
243239

244240
// just add to patternPaths
245-
addToPatternPaths(patternlab, patternTypeName, pattern);
241+
addToPatternPaths(patternlab, pattern);
246242
}
247243
}
248244

249-
patternlab.viewAllPaths[patternTypeName][pattern.patternSubGroup] = pattern.flatPatternPath;
245+
patternlab.viewAllPaths[pattern.patternGroup][pattern.patternSubGroup] = pattern.flatPatternPath;
250246
}
251247
return patternTypeIndex;
252248
}

test/files/_data/data.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ "data" : "test" }
2+

test/files/_data/foo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "foo" : "bar" }

test/files/_data/listitems.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"test_list_item" : "izzn thizzle"
3+
}
4+

test/lineage_hunter_tests.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ exports['lineage hunter '] = {
145145
]
146146
};
147147

148-
lineage_hunter.find_lineage(currentPattern, patternlab);
149148
lineage_hunter.find_lineage(currentPattern, patternlab);
150149

151150
test.equals(currentPattern.lineageIndex.length, 1);

test/list_item_hunter_tests.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,24 @@
149149

150150
'process_list_item_partials finds verbose partials and outputs repeated renders' : function(test){
151151
var pattern1 = createFakeListPattern({
152-
"template": "{{#listItems.one}}{{> 00-atoms/00-test/00-foo.mustache }}{{/listItems.one}}",
153-
"extendedTemplate" : "{{#listItems.one}}{{> 00-atoms/00-test/00-foo.mustache }}{{/listItems.one}}",
152+
"template": "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}",
153+
"extendedTemplate" : "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}",
154154
"key": "test-patternName1"
155155
});
156156

157157
var pattern2 = createFakeListPattern({
158-
"template": "{{#listItems.two}}{{> 00-atoms/00-test/00-bar.mustache }}{{/listItems.two}}",
159-
"extendedTemplate" : "{{#listItems.two}}{{> 00-atoms/00-test/00-bar.mustache }}{{/listItems.two}}",
158+
"template": "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}",
159+
"extendedTemplate" : "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}",
160160
"key": "test-patternName2"
161161
});
162162

163163
var patternlab = createFakePatternLab({
164164
"patterns": [
165-
Pattern.create('00-atoms/00-test/00-foo.mustache', null, {
165+
Pattern.create('00-test/00-foo.mustache', null, {
166166
"template": "{{ title }}",
167167
"extendedTemplate": "{{ title }}"
168168
}),
169-
Pattern.create('00-atoms/00-test/00-bar.mustache', null, {
169+
Pattern.create('00-test/00-bar.mustache', null, {
170170
"template": "{{ title }}",
171171
"extendedTemplate": "{{ title }}"
172172
})

test/object_factory_tests.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33

44
var of = require('../core/lib/object_factory');
55
var Pattern = require('../core/lib/object_factory').Pattern;
6+
var path = require('path');
67

78
exports['Pattern initialization'] = {
89
'test Pattern initializes correctly' : function (test) {
910
var p = new Pattern('00-atoms/00-global/00-colors.mustache', { d: 123});
10-
test.equals(p.relPath, '00-atoms/00-global/00-colors.mustache');
11+
test.equals(p.relPath, '00-atoms' + path.sep + '00-global' + path.sep + '00-colors.mustache');
1112
test.equals(p.name, '00-atoms-00-global-00-colors');
12-
test.equals(p.subdir, '00-atoms/00-global');
13+
test.equals(p.subdir, '00-atoms' + path.sep + '00-global');
1314
test.equals(p.fileName, '00-colors');
1415
test.equals(p.fileExtension, '.mustache');
1516
test.equals(p.jsonFileData.d, 123);
1617
test.equals(p.patternBaseName, 'colors');
1718
test.equals(p.patternName, 'Colors');
18-
test.equals(p.patternLink, '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html');
19+
test.equals(p.patternLink, '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.html');
1920
test.equals(p.patternGroup, 'atoms');
2021
test.equals(p.patternSubGroup, 'global');
2122
test.equals(p.flatPatternPath, '00-atoms-00-global');
@@ -31,15 +32,15 @@
3132
},
3233
'test Pattern with one-directory subdir works as expected' : function (test) {
3334
var p = new Pattern('00-atoms/00-colors.mustache', { d: 123});
34-
test.equals(p.relPath, '00-atoms/00-colors.mustache');
35+
test.equals(p.relPath, '00-atoms' + path.sep + '00-colors.mustache');
3536
test.equals(p.name, '00-atoms-00-colors');
3637
test.equals(p.subdir, '00-atoms');
3738
test.equals(p.fileName, '00-colors');
3839
test.equals(p.fileExtension, '.mustache');
3940
test.equals(p.jsonFileData.d, 123);
4041
test.equals(p.patternBaseName, 'colors');
4142
test.equals(p.patternName, 'Colors');
42-
test.equals(p.patternLink, '00-atoms-00-colors/00-atoms-00-colors.html');
43+
test.equals(p.patternLink, '00-atoms-00-colors' + path.sep + '00-atoms-00-colors.html');
4344
test.equals(p.patternGroup, 'atoms');
4445
test.equals(p.flatPatternPath, '00-atoms');
4546
test.equals(p.patternPartial, 'atoms-colors');
@@ -50,6 +51,18 @@
5051
test.equals(p.lineageRIndex.length, 0);
5152
test.done();
5253
},
54+
'test Pattern with no numbers in pattern group works as expected' : function (test) {
55+
var p = new Pattern('atoms/colors.mustache', { d: 123});
56+
test.equals(p.relPath, 'atoms' + path.sep + 'colors.mustache');
57+
test.equals(p.name, 'atoms-colors');
58+
test.equals(p.subdir, 'atoms');
59+
test.equals(p.fileName, 'colors');
60+
test.equals(p.patternLink, 'atoms-colors' + path.sep + 'atoms-colors.html');
61+
test.equals(p.patternGroup, 'atoms');
62+
test.equals(p.flatPatternPath, 'atoms');
63+
test.equals(p.patternPartial, 'atoms-colors');
64+
test.done();
65+
},
5366
'test Pattern capitalizes patternDisplayName correctly' : function(test){
5467
var p = new Pattern('00-atoms/00-global/00-colors-alt.mustache', { d: 123});
5568
test.equals(p.patternBaseName, 'colors-alt');

0 commit comments

Comments
 (0)