Skip to content

Commit f77f635

Browse files
Merge pull request #427 from pattern-lab/dev
Pattern Lab Node Core 2.4.0
2 parents 547c565 + 9baf45d commit f77f635

30 files changed

+921
-601
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ branches:
1616
only:
1717
- master
1818
- dev
19-
- dev-2.0-core
2019

2120
notifications:
2221
webhooks:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![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)
1+
[![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)
22

33
# Pattern Lab Node Core
44

core/lib/lineage_hunter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var lineage_hunter = function () {
1313
if (matches !== null) {
1414
matches.forEach(function (match) {
1515
//get the ancestorPattern
16-
var ancestorPattern = pattern_assembler.findPartial(pattern.findPartial(match), patternlab);
16+
var ancestorPattern = pattern_assembler.getPartial(pattern.findPartial(match), patternlab);
1717

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

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

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

9898
//only set patternState if pattern.patternState "is less than" the lineageRPattern.patternstate
9999
//or if lineageRPattern.patternstate (the consuming pattern) does not have a state

core/lib/list_item_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var list_item_hunter = function () {
8282

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

8787
//create a copy of the partial so as to not pollute it after the get_pattern_by_key call.
8888
var cleanPartialPattern;

core/lib/markdown_parser.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
"use strict";
22

33
var md = require('markdown-it')();
4+
var yaml = require('js-yaml');
45

56
var markdown_parser = function () {
67

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

1016
try {
1117
//for each block process the yaml frontmatter and markdown
1218
var frontmatterRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
1319
var chunks = frontmatterRE.exec(block);
14-
if (chunks && chunks[1]) {
15-
16-
//convert each yaml frontmatter key / value into an object key
17-
var frontmatter = chunks[1];
18-
var frontmatterLines = frontmatter.split(/\n/gm);
19-
for (var j = 0; j < frontmatterLines.length; j++) {
20-
21-
var frontmatterLine = frontmatterLines[j];
22-
if (frontmatterLine.length > 0) {
23-
24-
var frontmatterLineChunks = frontmatterLine.split(':'); //test this
25-
var frontmatterKey = frontmatterLineChunks[0].toLowerCase().trim();
26-
var frontmatterValueString = frontmatterLineChunks[1].trim();
27-
28-
returnObject[frontmatterKey] = frontmatterValueString;
29-
}
3020

21+
if (chunks) {
22+
//we got some frontmatter
23+
if (chunks && chunks[1]) {
24+
//parse the yaml if we got it
25+
var frontmatter = chunks[1];
26+
returnObject = yaml.safeLoad(frontmatter);
3127
}
32-
}
3328

34-
if (chunks && chunks[2]) {
35-
//parse the actual markdown
36-
returnObject.markdown = md.render(chunks[2]);
29+
if (chunks[2]) {
30+
//parse the actual markdown if it exists
31+
returnObject.markdown = md.render(chunks[2]);
32+
} else {
33+
returnObject.markdown = '';
34+
}
3735
} else {
38-
//assume the passed in block is raw markdown
36+
//assume the block was only markdown
3937
returnObject.markdown = md.render(block);
4038
}
4139
} catch (ex) {

core/lib/object_factory.js

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var Pattern = function (relPath, data) {
1616
this.fileExtension = pathObj.ext; // '.mustache'
1717

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

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

39+
//00-atoms if needed
40+
this.patternType = this.subdir.split(path.sep)[0];
41+
3942
// the sub-group this pattern belongs to.
4043
this.patternSubGroup = path.basename(this.subdir).replace(/^\d*-/, ''); // 'global'
4144

42-
// Not sure what this is used for.
45+
//00-colors if needed
46+
this.patternSubType = path.basename(this.subdir);
47+
48+
// the joined pattern group and subgroup directory
4349
this.flatPatternPath = this.subdir.replace(/[\/\\]/g, '-'); // '00-atoms-00-global'
4450

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

4955
this.isPattern = true;
56+
this.isFlatPattern = this.patternGroup === this.patternSubGroup;
5057
this.patternState = '';
5158
this.template = '';
5259
this.patternPartialCode = '';
@@ -117,41 +124,6 @@ Pattern.create = function (relPath, data, customProps) {
117124
return extend(newPattern, customProps);
118125
};
119126

120-
121-
var oPatternType = function (name) {
122-
this.patternTypeLC = name;
123-
this.patternTypeUC = name.split('-').reduce(function (val, working) {
124-
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
125-
}, '').trim();
126-
this.patternTypeItems = [];
127-
this.patternTypeItemsIndex = [];
128-
this.patternItems = [];
129-
this.patternItemsIndex = [];
130-
};
131-
132-
133-
var oPatternSubType = function (name) {
134-
this.patternSubtypeLC = name;
135-
this.patternSubtypeUC = name.split('-').reduce(function (val, working) {
136-
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
137-
}, '').trim();
138-
this.patternSubtypeItems = [];
139-
this.patternSubtypeItemsIndex = [];
140-
};
141-
142-
143-
var oPatternSubTypeItem = function (name) {
144-
this.patternPath = '';
145-
this.patternPartialCode = '';
146-
this.patternName = name.split(' ').reduce(function (val, working) {
147-
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
148-
}, '').trim();
149-
};
150-
151-
152127
module.exports = {
153-
Pattern: Pattern,
154-
oPatternType: oPatternType,
155-
oPatternSubType: oPatternSubType,
156-
oPatternSubTypeItem: oPatternSubTypeItem
128+
Pattern: Pattern
157129
};

core/lib/parameter_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ var parameter_hunter = function () {
240240
pattern.parameteredPartials.forEach(function (pMatch) {
241241
//find the partial's name and retrieve it
242242
var partialName = pMatch.match(/([\w\-\.\/~]+)/g)[0];
243-
var partialPattern = pattern_assembler.findPartial(partialName, patternlab);
243+
var partialPattern = pattern_assembler.getPartial(partialName, patternlab);
244244

245245
//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #190
246246
partialPattern.extendedTemplate = partialPattern.template;

core/lib/pattern_assembler.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ var pattern_assembler = function () {
2929
//else look by verbose syntax
3030
for (var i = 0; i < patternlab.patterns.length; i++) {
3131
switch (partialName) {
32+
case patternlab.patterns[i].relPath:
3233
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName:
33-
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName + '.mustache':
3434
return patternlab.patterns[i];
3535
}
3636
}
@@ -45,7 +45,8 @@ var pattern_assembler = function () {
4545
return patternlab.patterns[i];
4646
}
4747
}
48-
throw 'Could not find pattern with partial ' + partialName;
48+
console.error('Could not find pattern with partial ' + partialName);
49+
return undefined;
4950
}
5051

5152
function buildListItems(container) {
@@ -91,7 +92,7 @@ var pattern_assembler = function () {
9192
function addPattern(pattern, patternlab) {
9293

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

9697
//only push to array if the array doesn't contain this pattern
9798
var isNew = true;
@@ -214,7 +215,7 @@ var pattern_assembler = function () {
214215
subTypePattern.patternSectionSubtype = true;
215216
subTypePattern.patternLink = subTypePattern.name + '/index.html';
216217
subTypePattern.patternDesc = subTypeMarkdown.markdown;
217-
subTypePattern.patternPartial = 'viewall-' + subTypePattern.patternPartial;
218+
subTypePattern.flatPatternPath = subTypePattern.flatPatternPath + '-' + subTypePattern.fileName;
218219
subTypePattern.isPattern = false;
219220
subTypePattern.engine = null;
220221

@@ -417,8 +418,9 @@ var pattern_assembler = function () {
417418

418419
if (linkMatches) {
419420
for (var i = 0; i < linkMatches.length; i++) {
420-
expandedLink = patternlab.data.link[linkMatches[i].split('.')[1]];
421+
expandedLink = encodeURI(patternlab.data.link[linkMatches[i].split('.')[1]]);
421422
if (expandedLink) {
423+
expandedLink = expandedLink.replace('\\', '/');
422424
if (patternlab.config.debug) {
423425
console.log('expanded data link from ' + linkMatches[i] + ' to ' + expandedLink + ' inside ' + key);
424426
}
@@ -482,7 +484,7 @@ var pattern_assembler = function () {
482484
process_pattern_recursive: function (file, patternlab, additionalData) {
483485
processPatternRecursive(file, patternlab, additionalData);
484486
},
485-
findPartial: function (partial, patternlab) {
487+
getPartial: function (partial, patternlab) {
486488
return getPartial(partial, patternlab);
487489
},
488490
combine_listItems: function (patternlab) {

core/lib/patternlab.js

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v2.3.0 - 2016
2+
* patternlab-node - v2.4.0 - 2016
33
*
44
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
55
* Licensed under the MIT license.
@@ -21,27 +21,17 @@ var diveSync = require('diveSync'),
2121
function buildPatternData(dataFilesPath, fs) {
2222
var dataFilesPath = dataFilesPath;
2323
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
24-
var mergeObject = {}
24+
var mergeObject = {};
2525
dataFiles.forEach(function (filePath) {
26-
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8')
27-
mergeObject = _.merge(mergeObject, jsonData)
28-
})
26+
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8');
27+
mergeObject = _.merge(mergeObject, jsonData);
28+
});
2929
return mergeObject;
3030
}
3131

3232
function processAllPatternsIterative(pattern_assembler, patterns_dir, patternlab) {
3333
diveSync(
3434
patterns_dir,
35-
{
36-
filter: function (thisPath, dir) {
37-
if (dir) {
38-
var remainingPath = thisPath.replace(patterns_dir, '');
39-
var isValidPath = remainingPath.indexOf('/_') === -1;
40-
return isValidPath;
41-
}
42-
return true;
43-
}
44-
},
4535
function (err, file) {
4636
//log any errors
4737
if (err) {
@@ -56,16 +46,6 @@ function processAllPatternsIterative(pattern_assembler, patterns_dir, patternlab
5646
function processAllPatternsRecursive(pattern_assembler, patterns_dir, patternlab) {
5747
diveSync(
5848
patterns_dir,
59-
{
60-
filter: function (thisPath, dir) {
61-
if (dir) {
62-
var remainingPath = thisPath.replace(patterns_dir, '');
63-
var isValidPath = remainingPath.indexOf('/_') === -1;
64-
return isValidPath;
65-
}
66-
return true;
67-
}
68-
},
6949
function (err, file) {
7050
//log any errors
7151
if (err) {
@@ -85,7 +65,7 @@ var patternlab_engine = function (config) {
8565
pa = require('./pattern_assembler'),
8666
pe = require('./pattern_exporter'),
8767
lh = require('./lineage_hunter'),
88-
buildFrontEnd = require('./ui_builder'),
68+
ui = require('./ui_builder'),
8969
plutils = require('./utilities'),
9070
sm = require('./starterkit_manager'),
9171
patternlab = {};
@@ -99,7 +79,6 @@ var patternlab_engine = function (config) {
9979
console.log(patternlab.package.version);
10080
}
10181

102-
10382
function help() {
10483

10584
console.log('');
@@ -185,7 +164,7 @@ var patternlab_engine = function (config) {
185164
}
186165

187166
function listStarterkits() {
188-
var starterkit_manager = new sm(patternlab);
167+
var starterkit_manager = new sm(patternlab.config);
189168
return starterkit_manager.list_starterkits();
190169
}
191170

@@ -244,19 +223,17 @@ var patternlab_engine = function (config) {
244223
patternlab.userHead = fs.readFileSync(path.resolve(paths.source.meta, '_00-head.mustache'), 'utf8');
245224
}
246225
catch (ex) {
247-
if (patternlab.config.debug) {
248-
console.log(ex);
249-
console.log('Could not find optional user-defined header, usually found at ./source/_meta/_00-head.mustache. It was likely deleted.');
250-
}
226+
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');
227+
if (patternlab.config.debug) { console.log(ex); }
228+
process.exit(1);
251229
}
252230
try {
253231
patternlab.userFoot = fs.readFileSync(path.resolve(paths.source.meta, '_01-foot.mustache'), 'utf8');
254232
}
255233
catch (ex) {
256-
if (patternlab.config.debug) {
257-
console.log(ex);
258-
console.log('Could not find optional user-defined footer, usually found at ./source/_meta/_01-foot.mustache. It was likely deleted.');
259-
}
234+
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');
235+
if (patternlab.config.debug) { console.log(ex); }
236+
process.exit(1);
260237
}
261238

262239
//now that all the main patterns are known, look for any links that might be within data and expand them
@@ -275,7 +252,7 @@ var patternlab_engine = function (config) {
275252
//set pattern-specific header if necessary
276253
var head;
277254
if (patternlab.userHead) {
278-
head = patternlab.userHead.replace('{% pattern-lab-head %}', patternlab.header);
255+
head = patternlab.userHead;
279256
} else {
280257
head = patternlab.header;
281258
}
@@ -389,7 +366,7 @@ var patternlab_engine = function (config) {
389366
},
390367
build: function (callback, deletePatternDir) {
391368
buildPatterns(deletePatternDir);
392-
buildFrontEnd(patternlab);
369+
new ui().buildFrontend(patternlab);
393370
printDebug();
394371
callback();
395372
},

0 commit comments

Comments
 (0)