Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit b37e5d7

Browse files
committed
Factor the finders out of pattern_assembler and add them as methods on
oPatterns that delegate to PatternEngines. Remove requests to the pattern_assembler functions from hunters and replace them with calls to methods on oPattern. Also add new factory methods for oPatterns to be able to produce empty oPatterns and oPatterns with custom contents, which is useful both for unit testing and other internal use. This helps to cut down on usage of "bare" mustache strings.
1 parent 7989cfb commit b37e5d7

File tree

6 files changed

+127
-76
lines changed

6 files changed

+127
-76
lines changed

builder/lineage_hunter.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
2-
* patternlab-node - v0.14.0 - 2015
3-
*
1+
/*
2+
* patternlab-node - v0.14.0 - 2015
3+
*
44
* 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.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
88
*
99
*/
1010

@@ -19,7 +19,8 @@
1919
var pattern_assembler = new pa();
2020

2121
//find the {{> template-name }} within patterns
22-
var matches = pattern_assembler.find_pattern_partials(pattern);
22+
console.log('===\n', pattern, '\n===');
23+
var matches = pattern.findPartials();
2324
if(matches !== null){
2425
matches.forEach(function(match, index, matches){
2526
//strip out the template cruft

builder/list_item_hunter.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
2-
* patternlab-node - v0.14.0 - 2015
3-
*
1+
/*
2+
* patternlab-node - v0.14.0 - 2015
3+
*
44
* 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.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
88
*
99
*/
1010

@@ -14,20 +14,23 @@
1414
var list_item_hunter = function(){
1515

1616
var extend = require('util')._extend,
17-
pa = require('./pattern_assembler'),
18-
smh = require('./style_modifier_hunter'),
19-
mustache = require('mustache'),
20-
pattern_assembler = new pa(),
21-
style_modifier_hunter = new smh(),
22-
items = [ 'zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty'];
17+
pa = require('./pattern_assembler'),
18+
smh = require('./style_modifier_hunter'),
19+
config = require('../config.json'),
20+
of = require('./object_factory');
21+
22+
var pattern_assembler = new pa(),
23+
style_modifier_hunter = new smh(),
24+
items = [ 'zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty'];
2325

2426
function processListItemPartials(pattern, patternlab){
2527
//find any listitem blocks
26-
var matches = pattern_assembler.find_list_items(pattern, patternlab);
28+
var matches = pattern.findListItems();
29+
2730
if(matches !== null){
2831
matches.forEach(function(liMatch, index, matches){
2932

30-
if(patternlab.config.debug){
33+
if(config.debug){
3134
console.log('found listItem of size ' + liMatch + ' inside ' + pattern.key);
3235
}
3336

@@ -39,6 +42,7 @@
3942
var repeatedBlockTemplate = [];
4043
var repeatedBlockHtml = '';
4144
for(var i = 0; i < items.indexOf(loopNumberString); i++){
45+
console.log('adding', patternBlock, 'to repeatedBlockTemplate');
4246
repeatedBlockTemplate.push(patternBlock);
4347
}
4448

@@ -62,7 +66,7 @@
6266
allData.link = extend({}, patternlab.data.link);
6367

6468
//check for partials within the repeated block
65-
var foundPartials = pattern_assembler.find_pattern_partials({ 'template' : thisBlockTemplate });
69+
var foundPartials = of.oPattern.createEmpty({'template': thisBlockTemplate}).findPartials();
6670

6771
if(foundPartials && foundPartials.length > 0){
6872

builder/object_factory.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313

1414
var patternEngines = require('./pattern_engines/pattern_engines');
1515
var path = require('path');
16+
var fs = require('fs-extra');
17+
var config = fs.readJSONSync('./config.json');
18+
var extend = require('util')._extend;
1619

1720
// oPattern properties
1821

1922
var oPattern = function(abspath, subdir, filename, data){
20-
console.log('NEW OPATTERN. ', 'absPath:', abspath, 'subdir:', subdir, 'filename:', filename, 'data:', data);
23+
if (config.debug) {
24+
console.log('=== NEW OPATTERN.', '\nabsPath:', abspath, '\nsubdir:', subdir, '\nfilename:', filename, '\ndata:\n', data);
25+
}
2126
this.fileName = filename.substring(0, filename.indexOf('.'));
2227
this.fileExtension = path.extname(abspath);
2328
this.abspath = abspath;
@@ -48,16 +53,43 @@
4853
// render method on oPatterns; this acts as a proxy for the PatternEngine's
4954
// render function
5055
oPattern.prototype.render = function (data, partials) {
51-
if (this.isPseudoPattern) {
52-
console.log(this.name + ' is a pseudo-pattern');
53-
} else {
54-
console.log('this is NOT a pseudo-pattern');
56+
if (config.debug && this.isPseudoPattern) {
57+
console.log('===', this.name + ' IS A PSEUDO-PATTERN ===');
5558
}
56-
// console.log('this does ' + (this.template ? '' : 'NOT ') + 'have template');
57-
// console.log('this does ' + (this.extendedTemplate ? '' : 'NOT ') + 'have extendedTemplate');
58-
5959
return this.engine.renderPattern(this.extendedTemplate, data, partials);
6060
};
61+
// the finders all delegate to the PatternEngine, which also encapsulates all
62+
// appropriate regexes
63+
oPattern.prototype.findPartials = function () {
64+
return this.engine.findPartials(this);
65+
};
66+
oPattern.prototype.findPartialsWithStyleModifiers = function () {
67+
return this.engine.findPartialsWithStyleModifiers(this);
68+
};
69+
oPattern.prototype.findPartialsWithPatternParameters = function () {
70+
return this.engine.findPartialsWithPatternParameters(this);
71+
};
72+
oPattern.prototype.findListItems = function () {
73+
return this.engine.findListItems(this);
74+
};
75+
76+
// oPattern static methods
77+
78+
// factory: creates an empty oPattern for miscellaneous internal use, such as
79+
// by list_item_hunter
80+
oPattern.createEmpty = function (customProps) {
81+
var pattern = new oPattern('', '', '', null);
82+
return extend(pattern, customProps);
83+
};
84+
85+
// factory: creates an oPattern object on-demand from a hash; the hash accepts
86+
// parameters that replace the positional parameters that the oPattern
87+
// constructor takes.
88+
oPattern.create = function (abspath, subdir, filename, data, customProps) {
89+
var newPattern = new oPattern(abspath || '', subdir || '', filename || '', data || null);
90+
return extend(newPattern, customProps);
91+
};
92+
6193

6294
var oBucket = function(name){
6395
this.bucketNameLC = name;
@@ -97,4 +129,4 @@
97129
oNavSubItem: oNavSubItem
98130
};
99131

100-
}());
132+
})();

builder/pattern_assembler.js

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
var fs = require('fs-extra'),
1717
of = require('./object_factory'),
1818
path = require('path'),
19-
patternEngines = require('./pattern_engines/pattern_engines');
19+
patternEngines = require('./pattern_engines/pattern_engines'),
20+
config = fs.readJSONSync('./config.json');
2021

2122
function isObjectEmpty(obj) {
2223
for(var prop in obj) {
@@ -26,29 +27,6 @@
2627

2728
return true;
2829
}
29-
// GTP: I need to factor out all these regexes
30-
// returns any patterns that match {{> value:mod }} or {{> value:mod(foo:"bar") }} within the pattern
31-
function findPartialsWithStyleModifiers(pattern){
32-
var matches = pattern.template.match(/{{>([ ])?([\w\-\.\/~]+)(?!\()(\:[A-Za-z0-9-_]+)+(?:(| )\(.*)?([ ])?}}/g);
33-
return matches;
34-
}
35-
36-
// returns any patterns that match {{> value(foo:"bar") }} or {{> value:mod(foo:"bar") }} within the pattern
37-
function findPartialsWithPatternParameters(pattern){
38-
var matches = pattern.template.match(/{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-_]+)?(?:(| )\(.*)+([ ])?}}/g);
39-
return matches;
40-
}
41-
42-
//find and return any {{> template-name* }} within pattern
43-
function findPartials(pattern){
44-
var matches = pattern.template.match(/{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-_]+)?(?:(| )\(.*)?([ ])?}}/g);
45-
return matches;
46-
}
47-
48-
function findListItems(pattern){
49-
var matches = pattern.template.match(/({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g);
50-
return matches;
51-
}
5230

5331
function setState(pattern, patternlab){
5432
if(patternlab.config.patternStates[pattern.patternName]){
@@ -82,12 +60,16 @@
8260
// if we've been passed a full oPattern, it knows what kind of template it
8361
// is, and how to render itself, so we just call its render method
8462
if (pattern instanceof of.oPattern) {
85-
console.log('rendering full oPattern: ' + pattern.name);
63+
if (config.debug) {
64+
console.log('rendering full oPattern: ' + pattern.name);
65+
}
8666
return pattern.render(data, partials);
8767
} else {
8868
// otherwise, assume it's a plain mustache template string and act
8969
// accordingly
90-
console.log('rendering plain mustache string:', pattern.substring(0, 20) + '...');
70+
if (config.debug) {
71+
console.log('rendering plain mustache string:', pattern.substring(0, 20) + '...');
72+
}
9173
return patternEngines.mustache.renderPattern(pattern, data, partials);
9274
}
9375
}
@@ -106,10 +88,7 @@
10688
}
10789

10890
// not a hidden pattern, let's dig deeper
109-
var engineNames = Object.keys(patternEngines);
110-
var supportedPatternFileExtensions = engineNames.map(function (engineName) {
111-
return patternEngines[engineName].fileExtension;
112-
});
91+
var supportedPatternFileExtensions = patternEngines.getSupportedFileExtensions();
11392
return (supportedPatternFileExtensions.lastIndexOf(extension) !== -1);
11493
}
11594

@@ -123,11 +102,15 @@
123102
var filename = path.basename(file);
124103
var ext = path.extname(filename);
125104

126-
console.log('processPatternIterative:', 'filename:', filename);
105+
if (config.debug) {
106+
console.log('processPatternIterative:', 'filename:', filename);
107+
}
127108

128109
// skip non-pattern files
129110
if (!isPatternFile(filename, patternlab)) { return; }
130-
console.log('found pattern', file);
111+
if (config.debug) {
112+
console.log('processPatternIterative:', 'found pattern', file);
113+
}
131114

132115
//make a new Pattern Object
133116
var currentPattern = new of.oPattern(file, subdir, filename);
@@ -144,7 +127,9 @@
144127

145128
//can ignore all non-mustache files at this point
146129
if(ext !== '.mustache'){
147-
console.log('==================== FOUND NON-MUSTACHE FILE');
130+
if (config.debug) {
131+
console.log('==================== FOUND NON-MUSTACHE FILE');
132+
}
148133
return;
149134
}
150135

@@ -178,10 +163,10 @@
178163
currentPattern.template = fs.readFileSync(file, 'utf8');
179164

180165
//find any stylemodifiers that may be in the current pattern
181-
currentPattern.stylePartials = findPartialsWithStyleModifiers(currentPattern);
166+
currentPattern.stylePartials = currentPattern.findPartialsWithStyleModifiers();
182167

183168
//find any pattern parameters that may be in the current pattern
184-
currentPattern.parameteredPartials = findPartialsWithPatternParameters(currentPattern);
169+
currentPattern.parameteredPartials = currentPattern.findPartialsWithPatternParameters();
185170

186171
//add currentPattern to patternlab.patterns array
187172
addPattern(currentPattern, patternlab);
@@ -218,7 +203,7 @@
218203
currentPattern.extendedTemplate = currentPattern.template;
219204

220205
//find how many partials there may be for the given pattern
221-
var foundPatternPartials = findPartials(currentPattern);
206+
var foundPatternPartials = currentPattern.findPartials(currentPattern);
222207

223208
if(foundPatternPartials !== null && foundPatternPartials.length > 0){
224209

@@ -356,16 +341,16 @@
356341

357342
return {
358343
find_pattern_partials: function(pattern){
359-
return findPartials(pattern);
344+
return pattern.findPartials();
360345
},
361346
find_pattern_partials_with_style_modifiers: function(pattern){
362-
return findPartialsWithStyleModifiers(pattern);
347+
return pattern.findPartialsWithStyleModifiers();
363348
},
364349
find_pattern_partials_with_parameters: function(pattern){
365-
return findPartialsWithPatternParameters(pattern);
350+
return pattern.findPartialsWithPatternParameters();
366351
},
367352
find_list_items: function(pattern){
368-
return findListItems(pattern)
353+
return pattern.findListItems();
369354
},
370355
setPatternState: function(pattern, patternlab){
371356
setState(pattern, patternlab);

builder/pattern_engines/engine_mustache.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515

1616
var engine_mustache = {
1717
engine: Mustache,
18-
name: 'mustache',
19-
fileExtension: '.mustache',
18+
engineName: 'mustache',
19+
engineFileExtension: '.mustache',
20+
21+
// regexes, stored here so they're only compiled once
22+
findPartialsRE: /{{>([ ])?([A-Za-z0-9-]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ])?}}/g,
23+
findPartialsWithStyleModifiersRE: /{{>([ ])?([\w\-\.\/~]+)(?!\()(\:[A-Za-z0-9-_]+)+(?:(| )\(.*)?([ ])?}}/g,
24+
findPartialsWithPatternParametersRE: /{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-_]+)?(?:(| )\(.*)+([ ])?}}/g,
25+
findListItemsRE: /({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g,
2026

2127
// render it
2228
renderPattern: function renderPattern(template, data, partials) {
@@ -28,7 +34,20 @@
2834

2935
// find and return any {{> template-name }} within pattern
3036
findPartials: function findPartials(pattern) {
31-
var matches = pattern.template.match(/{{>([ ])?([A-Za-z0-9-]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ])?}}/g);
37+
var matches = pattern.template.match(this.findPartialsRE);
38+
return matches;
39+
},
40+
findPartialsWithStyleModifiers: function(pattern){
41+
var matches = pattern.template.match(this.findPartialsWithStyleModifiersRE);
42+
return matches;
43+
},
44+
// returns any patterns that match {{> value(foo:"bar") }} or {{> value:mod(foo:"bar") }} within the pattern
45+
findPartialsWithPatternParameters: function(pattern){
46+
var matches = pattern.template.match(this.findPartialsWithPatternParametersRE);
47+
return matches;
48+
},
49+
findListItems: function(pattern){
50+
var matches = pattern.template.match(this.findListItemsRE);
3251
return matches;
3352
}
3453
};

builder/pattern_engines/pattern_engines.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// avoid circular dependency by putting this in here. TODO: is this slow?
3636
var of = require('../object_factory');
3737

38-
if (pattern instanceof of.oPattern) {
38+
if (pattern instanceof of.oPattern && typeof pattern.fileExtension === 'string' && pattern.fileExtension) {
3939
return engineNameForExtension[pattern.fileExtension];
4040
}
4141
// otherwise, assume it's a plain mustache template string and act
@@ -49,6 +49,16 @@
4949
var engineName = this.getEngineNameForPattern(pattern);
5050
return this[engineName];
5151
}
52+
},
53+
getSupportedFileExtensions: function () {
54+
var engineNames = Object.keys(PatternEngines);
55+
return engineNames.map(function (engineName) {
56+
return PatternEngines[engineName].engineFileExtension;
57+
});
58+
},
59+
isFileExtensionSupported: function (fileExtension) {
60+
var supportedExtensions = PatternEngines.getSupportedFileExtensions();
61+
return (supportedExtensions.lastIndexOf(fileExtension) !== -1);
5262
}
5363
});
5464

@@ -69,7 +79,7 @@
6979
var mapping = {};
7080

7181
Object.keys(PatternEngines).forEach(function (engineName) {
72-
var extensionForEngine = PatternEngines[engineName].fileExtension;
82+
var extensionForEngine = PatternEngines[engineName].engineFileExtension;
7383
mapping[extensionForEngine] = engineName;
7484
});
7585

0 commit comments

Comments
 (0)