Skip to content

Commit ea1b680

Browse files
geoffpBrian Muenzenmeyer
authored andcommitted
Dev 2.0 core oPattern refactor (#331)
* merge the oPattern refactor branch * Delete extra copy of expandPartials. How'd that get in there? * Factor out the iterative and recursive diveSync loops, and export them (sort of) so they can be re-used from the unit tests * Pattern.key -> Pattern.patternPartial * abspath -> relPath * Fix unit tests to use the new stuff, also factor out some diveSync loops that are now provided by the main patternlab module * "fixed" a couple unit tests by hopefully normalizing eol characters * addressed new lines in a couple more false positives. in one case, just removed them due to the length of the output being compared * removed a set of duplicate tests
1 parent fb3b240 commit ea1b680

19 files changed

+353
-427
lines changed

core/lib/lineage_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ var lineage_hunter = function () {
112112
< patternlab.config.patternStateCascade.indexOf(lineageRPattern.patternState))) {
113113

114114
if (patternlab.config.debug) {
115-
console.log('Found a lower common denominator pattern state: ' + pattern.patternState + ' on ' + pattern.key + '. Setting reverse lineage pattern ' + lineageRPattern.patternPartial + ' from ' + (lineageRPattern.patternState === '' ? '<<blank>>' : lineageRPattern.patternState));
115+
console.log('Found a lower common denominator pattern state: ' + pattern.patternState + ' on ' + pattern.patternPartial + '. Setting reverse lineage pattern ' + lineageRPattern.patternPartial + ' from ' + (lineageRPattern.patternState === '' ? '<<blank>>' : lineageRPattern.patternState));
116116
}
117117

118118
lineageRPattern.patternState = pattern.patternState;

core/lib/list_item_hunter.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var list_item_hunter = function () {
1717
pa = require('./pattern_assembler'),
1818
smh = require('./style_modifier_hunter'),
1919
plutils = require('./utilities'),
20-
of = require('./object_factory');
20+
Pattern = require('./object_factory').Pattern;
2121

2222
var pattern_assembler = new pa(),
2323
style_modifier_hunter = new smh(),
@@ -54,7 +54,7 @@ var list_item_hunter = function () {
5454
try {
5555
listData = JSON5.parse(JSON5.stringify(patternlab.listitems));
5656
} catch (err) {
57-
console.log('There was an error parsing JSON for ' + pattern.abspath);
57+
console.log('There was an error parsing JSON for ' + pattern.relPath);
5858
console.log(err);
5959
}
6060
listData = plutils.mergeData(listData, pattern.listitems);
@@ -73,7 +73,7 @@ var list_item_hunter = function () {
7373
globalData = JSON5.parse(JSON5.stringify(patternlab.data));
7474
localData = JSON5.parse(JSON5.stringify(pattern.jsonFileData));
7575
} catch (err) {
76-
console.log('There was an error parsing JSON for ' + pattern.abspath);
76+
console.log('There was an error parsing JSON for ' + pattern.relPath);
7777
console.log(err);
7878
}
7979

@@ -82,7 +82,7 @@ var list_item_hunter = function () {
8282
allData.link = extend({}, patternlab.data.link);
8383

8484
//check for partials within the repeated block
85-
var foundPartials = of.oPattern.createEmpty({'template': thisBlockTemplate}).findPartials();
85+
var foundPartials = Pattern.createEmpty({'template': thisBlockTemplate}).findPartials();
8686

8787
if (foundPartials && foundPartials.length > 0) {
8888

@@ -97,7 +97,7 @@ var list_item_hunter = function () {
9797
try {
9898
cleanPartialPattern = JSON5.parse(JSON5.stringify(partialPattern));
9999
} catch (err) {
100-
console.log('There was an error parsing JSON for ' + pattern.abspath);
100+
console.log('There was an error parsing JSON for ' + pattern.relPath);
101101
console.log(err);
102102
}
103103

core/lib/object_factory.js

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,48 @@ var patternEngines = require('./pattern_engines/pattern_engines');
1414
var path = require('path');
1515
var extend = require('util')._extend;
1616

17-
// oPattern properties
18-
19-
var oPattern = function (abspath, subdir, filename, data) {
20-
this.fileName = filename.substring(0, filename.indexOf('.'));
21-
this.fileExtension = path.extname(abspath);
22-
this.abspath = abspath;
23-
this.subdir = subdir;
24-
this.name = subdir.replace(/[\/\\]/g, '-') + '-' + this.fileName; //this is the unique name with the subDir
17+
// Pattern properties
18+
19+
var Pattern = function (relPath, data) {
20+
// We expect relPath to be the path of the pattern template, relative to the
21+
// root of the pattern tree. Parse out the path parts and save the useful ones.
22+
var pathObj = path.parse(relPath);
23+
this.relPath = relPath; // '00-atoms/00-global/00-colors.mustache'
24+
this.fileName = pathObj.name; // '00-colors'
25+
this.subdir = pathObj.dir; // '00-atoms/00-global'
26+
this.fileExtension = pathObj.ext; // '.mustache'
27+
28+
// this is the unique name, subDir + fileName (sans extension)
29+
this.name = this.subdir.replace(/[\/\\]/g, '-') + '-' + this.fileName; // '00-atoms-00-global-00-colors'
30+
31+
// the JSON used to render values in the pattern
2532
this.jsonFileData = data || {};
26-
this.patternName = this.fileName.replace(/^\d*\-/, '');
33+
34+
// strip leading "00-" from the file name and flip tildes to dashes
35+
this.patternName = this.fileName.replace(/^\d*\-/, '').replace('~', '-'); // 'colors'
36+
37+
// Fancy name. No idea how this works. 'Colors'
2738
this.patternDisplayName = this.patternName.split('-').reduce(function (val, working) {
2839
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
2940
}, '').trim(); //this is the display name for the ui. strip numeric + hyphen prefixes
30-
this.patternLink = this.name + '/' + this.name + '.html';
41+
42+
// calculated path from the root of the public directory to the generated html
43+
// file for this pattern
44+
this.patternLink = this.name + '/' + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'
45+
46+
// the top-level pattern group this pattern belongs to. 'atoms'
3147
this.patternGroup = this.name.substring(this.name.indexOf('-') + 1, this.name.indexOf('-', 4) + 1 - this.name.indexOf('-') + 1);
32-
this.patternSubGroup = subdir.substring(subdir.indexOf('/') + 4);
33-
this.flatPatternPath = subdir.replace(/[\/\\]/g, '-');
48+
49+
// the sub-group this pattern belongs to.
50+
this.patternSubGroup = this.subdir.substring(this.subdir.indexOf('/') + 4); // 'global'
51+
52+
// Not sure what this is used for.
53+
this.flatPatternPath = this.subdir.replace(/[\/\\]/g, '-'); // '00-atoms-00-global'
54+
55+
// The canonical "key" by which this pattern is known. This is the callable
56+
// name of the pattern. UPDATE: this.key is now known as this.patternPartial
3457
this.patternPartial = this.patternGroup + '-' + this.patternName;
58+
3559
this.template = '';
3660
this.patternPartialCode = '';
3761
this.lineage = [];
@@ -42,9 +66,9 @@ var oPattern = function (abspath, subdir, filename, data) {
4266
this.engine = patternEngines.getEngineForPattern(this);
4367
};
4468

45-
// oPattern methods
69+
// Pattern methods
4670

47-
oPattern.prototype = {
71+
Pattern.prototype = {
4872

4973
// render method on oPatterns; this acts as a proxy for the PatternEngine's
5074
// render function
@@ -81,20 +105,20 @@ oPattern.prototype = {
81105
}
82106
};
83107

84-
// oPattern static methods
108+
// Pattern static methods
85109

86-
// factory: creates an empty oPattern for miscellaneous internal use, such as
110+
// factory: creates an empty Pattern for miscellaneous internal use, such as
87111
// by list_item_hunter
88-
oPattern.createEmpty = function (customProps) {
89-
var pattern = new oPattern('', '', '', null);
112+
Pattern.createEmpty = function (customProps) {
113+
var pattern = new Pattern('', null);
90114
return extend(pattern, customProps);
91115
};
92116

93-
// factory: creates an oPattern object on-demand from a hash; the hash accepts
94-
// parameters that replace the positional parameters that the oPattern
117+
// factory: creates an Pattern object on-demand from a hash; the hash accepts
118+
// parameters that replace the positional parameters that the Pattern
95119
// constructor takes.
96-
oPattern.create = function (abspath, subdir, filename, data, customProps) {
97-
var newPattern = new oPattern(abspath || '', subdir || '', filename || '', data || null);
120+
Pattern.create = function (relPath, data, customProps) {
121+
var newPattern = new Pattern(relPath || '', data || null);
98122
return extend(newPattern, customProps);
99123
};
100124

@@ -131,7 +155,7 @@ var oPatternSubTypeItem = function (name) {
131155

132156

133157
module.exports = {
134-
oPattern: oPattern,
158+
Pattern: Pattern,
135159
oPatternType: oPatternType,
136160
oPatternSubType: oPatternSubType,
137161
oPatternSubTypeItem: oPatternSubTypeItem

core/lib/parameter_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ var parameter_hunter = function () {
274274
globalData = JSON5.parse(JSON5.stringify(patternlab.data));
275275
localData = JSON5.parse(JSON5.stringify(pattern.jsonFileData || {}));
276276
} catch (err) {
277-
console.log('There was an error parsing JSON for ' + pattern.abspath);
277+
console.log('There was an error parsing JSON for ' + pattern.relPath);
278278
console.log(err);
279279
}
280280

core/lib/pattern_assembler.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
var pattern_assembler = function () {
1515
var path = require('path'),
1616
fs = require('fs-extra'),
17-
of = require('./object_factory'),
17+
Pattern = require('./object_factory').Pattern,
1818
md = require('markdown-it')(),
1919
plutils = require('./utilities'),
2020
patternEngines = require('./pattern_engines/pattern_engines');
@@ -92,9 +92,9 @@ var pattern_assembler = function () {
9292
//only push to array if the array doesn't contain this pattern
9393
var isNew = true;
9494
for (var i = 0; i < patternlab.patterns.length; i++) {
95-
//so we need the identifier to be unique, which patterns[i].abspath is
96-
if (pattern.abspath === patternlab.patterns[i].abspath) {
97-
//if abspath already exists, overwrite that element
95+
//so we need the identifier to be unique, which patterns[i].relPath is
96+
if (pattern.relPath === patternlab.patterns[i].relPath) {
97+
//if relPath already exists, overwrite that element
9898
patternlab.patterns[i] = pattern;
9999
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate || pattern.template;
100100
isNew = false;
@@ -120,30 +120,30 @@ var pattern_assembler = function () {
120120

121121
// Render a pattern on request. Long-term, this should probably go away.
122122
function renderPattern(pattern, data, partials) {
123-
// if we've been passed a full oPattern, it knows what kind of template it
123+
// if we've been passed a full Pattern, it knows what kind of template it
124124
// is, and how to render itself, so we just call its render method
125-
if (pattern instanceof of.oPattern) {
125+
if (pattern instanceof Pattern) {
126126
return pattern.render(data, partials);
127127
} else {
128128
// otherwise, assume it's a plain mustache template string, and we
129129
// therefore just need to create a dummpy pattern to be able to render
130130
// it
131-
var dummyPattern = of.oPattern.createEmpty({extendedTemplate: pattern});
131+
var dummyPattern = Pattern.createEmpty({extendedTemplate: pattern});
132132
return patternEngines.mustache.renderPattern(dummyPattern, data, partials);
133133
}
134134
}
135135

136-
function processPatternIterative(file, patternlab) {
136+
function processPatternIterative(relPath, patternlab) {
137137
//extract some information
138-
var subdir = path.dirname(path.relative(patternlab.config.paths.source.patterns, file)).replace('\\', '/');
139-
var filename = path.basename(file);
138+
var filename = path.basename(relPath);
140139
var ext = path.extname(filename);
140+
var patternsPath = patternlab.config.paths.source.patterns;
141141

142142
// skip non-pattern files
143143
if (!patternEngines.isPatternFile(filename, patternlab)) { return null; }
144144

145145
//make a new Pattern Object
146-
var currentPattern = new of.oPattern(file, subdir, filename);
146+
var currentPattern = new Pattern(relPath);
147147

148148
//if file is named in the syntax for variants
149149
if (patternEngines.isPseudoPatternJSON(filename)) {
@@ -160,7 +160,7 @@ var pattern_assembler = function () {
160160

161161
//look for a json file for this template
162162
try {
163-
var jsonFilename = path.resolve(patternlab.config.paths.source.patterns, currentPattern.subdir, currentPattern.fileName + ".json");
163+
var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".json");
164164
currentPattern.jsonFileData = fs.readJSONSync(jsonFilename);
165165
if (patternlab.config.debug) {
166166
console.log('processPatternIterative: found pattern-specific data.json for ' + currentPattern.patternPartial);
@@ -172,7 +172,7 @@ var pattern_assembler = function () {
172172

173173
//look for a listitems.json file for this template
174174
try {
175-
var listJsonFileName = path.resolve(patternlab.config.paths.source.patterns, currentPattern.subdir, currentPattern.fileName + ".listitems.json");
175+
var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems.json");
176176
currentPattern.listitems = fs.readJSONSync(listJsonFileName);
177177
buildListItems(currentPattern);
178178
if (patternlab.config.debug) {
@@ -198,7 +198,7 @@ var pattern_assembler = function () {
198198
}
199199

200200
//add the raw template to memory
201-
currentPattern.template = fs.readFileSync(file, 'utf8');
201+
currentPattern.template = fs.readFileSync(path.resolve(patternsPath, relPath), 'utf8');
202202

203203
//find any stylemodifiers that may be in the current pattern
204204
currentPattern.stylePartials = currentPattern.findPartialsWithStyleModifiers();
@@ -225,7 +225,7 @@ var pattern_assembler = function () {
225225
var currentPattern, i;
226226

227227
for (i = 0; i < patternlab.patterns.length; i++) {
228-
if (patternlab.patterns[i].abspath === file) {
228+
if (patternlab.patterns[i].relPath === file) {
229229
currentPattern = patternlab.patterns[i];
230230
}
231231
}
@@ -285,9 +285,9 @@ var pattern_assembler = function () {
285285
//identify which pattern this partial corresponds to
286286
for (var j = 0; j < patternlab.patterns.length; j++) {
287287
if (patternlab.patterns[j].patternPartial === partial ||
288-
patternlab.patterns[j].abspath.indexOf(partial) > -1)
288+
patternlab.patterns[j].relPath.indexOf(partial) > -1)
289289
{
290-
partialPath = patternlab.patterns[j].abspath;
290+
partialPath = patternlab.patterns[j].relPath;
291291
}
292292
}
293293

core/lib/pattern_engines/engine_handlebars.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ var engine_handlebars = {
4444
return compiled(data);
4545
},
4646

47-
registerPartial: function (oPattern) {
48-
Handlebars.registerPartial(oPattern.patternPartial, oPattern.template);
47+
registerPartial: function (Pattern) {
48+
Handlebars.registerPartial(Pattern.patternPartial, Pattern.template);
4949
},
5050

5151
// find and return any {{> template-name }} within pattern
@@ -54,15 +54,15 @@ var engine_handlebars = {
5454
return matches;
5555
},
5656
findPartialsWithStyleModifiers: function () {
57-
// TODO: make the call to this from oPattern objects conditional on their
57+
// TODO: make the call to this from Pattern objects conditional on their
5858
// being implemented here.
5959
return [];
6060
},
6161

6262
// returns any patterns that match {{> value(foo:"bar") }} or {{>
6363
// value:mod(foo:"bar") }} within the pattern
6464
findPartialsWithPatternParameters: function () {
65-
// TODO: make the call to this from oPattern objects conditional on their
65+
// TODO: make the call to this from Pattern objects conditional on their
6666
// being implemented here.
6767
return [];
6868
},

core/lib/pattern_engines/engine_twig.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ var engine_twig = {
5151
return matches;
5252
},
5353
findPartialsWithStyleModifiers: function () {
54-
// TODO: make the call to this from oPattern objects conditional on their
54+
// TODO: make the call to this from Pattern objects conditional on their
5555
// being implemented here.
5656
return [];
5757
},
5858

5959
// returns any patterns that match {{> value(foo:"bar") }} or {{>
6060
// value:mod(foo:"bar") }} within the pattern
6161
findPartialsWithPatternParameters: function () {
62-
// TODO: make the call to this from oPattern objects conditional on their
62+
// TODO: make the call to this from Pattern objects conditional on their
6363
// being implemented here.
6464
return [];
6565
},

core/lib/pattern_engines/engine_underscore.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@
7373
return renderedHTML;
7474
},
7575

76-
// registerPartial: function (oPattern) {
76+
// registerPartial: function (Pattern) {
7777
// debugger;
78-
// _.registerPartial(oPattern.patternPartial, oPattern.template);
78+
// _.registerPartial(Pattern.patternPartial, Pattern.template);
7979
// },
8080

8181
// find and return any {{> template-name }} within pattern

core/lib/pattern_engines/pattern_engines.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ PatternEngines = Object.create({
107107
// avoid circular dependency by putting this in here. TODO: is this slow?
108108
var of = require('../object_factory');
109109

110-
if (pattern instanceof of.oPattern && typeof pattern.fileExtension === 'string' && pattern.fileExtension) {
110+
if (pattern instanceof of.Pattern && typeof pattern.fileExtension === 'string' && pattern.fileExtension) {
111111
return engineNameForExtension[pattern.fileExtension];
112112
}
113113

0 commit comments

Comments
 (0)