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

Commit 8ac9b6e

Browse files
committed
(mostly?) working handlebars rendering on a real tree -- there's a
problem now with patternlab.json having cyclical references that needs to be fixed.
1 parent 6b22356 commit 8ac9b6e

File tree

6 files changed

+107
-132
lines changed

6 files changed

+107
-132
lines changed

builder/lineage_hunter.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
var config = require('../config.json');
2121

2222
//find the {{> template-name }} within patterns
23-
if (config.debug) {
24-
console.log('===\n', pattern, '\n===');
25-
}
2623
var matches = pattern.findPartials();
2724
if(matches !== null){
2825
matches.forEach(function(match, index, matches){

builder/object_factory.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
// oPattern properties
2121

2222
var oPattern = function(abspath, subdir, filename, data) {
23-
if (config.debug) {
24-
console.log('=== NEW OPATTERN.', '\nabsPath:', abspath, '\nsubdir:', subdir, '\nfilename:', filename, '\ndata:\n', data);
25-
}
2623
this.fileName = filename.substring(0, filename.indexOf('.'));
2724
this.fileExtension = path.extname(abspath);
2825
this.abspath = abspath;
@@ -61,6 +58,12 @@
6158
return this.engine.renderPattern(this.extendedTemplate, data, partials);
6259
},
6360

61+
registerPartial: function () {
62+
if (typeof this.engine.registerPartial === 'function') {
63+
this.engine.registerPartial(this);
64+
}
65+
},
66+
6467
// the finders all delegate to the PatternEngine, which also encapsulates all
6568
// appropriate regexes
6669
findPartials: function () {

builder/pattern_assembler.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
function addPattern(pattern, patternlab){
3232
//add the link to the global object
3333
patternlab.data.link[pattern.patternGroup + '-' + pattern.patternName] = '/patterns/' + pattern.patternLink;
34+
if (!patternlab.patternsByKey) { patternlab.patternsByKey = {}; }
35+
if (!patternlab.patternsByAbsPath) { patternlab.patternsByAbsPath = {}; }
3436

3537
//only push to array if the array doesn't contain this pattern
3638
var isNew = true;
@@ -45,7 +47,12 @@
4547
}
4648
//if the pattern is new, just push to the array
4749
if(isNew){
50+
// do global registration
4851
patternlab.patterns.push(pattern);
52+
patternlab.patternsByKey[pattern.key] = pattern;
53+
patternlab.patternsByAbsPath[pattern.asbpath] = pattern;
54+
// do plugin-specific registration
55+
pattern.registerPartial();
4956
}
5057
}
5158

@@ -74,14 +81,11 @@
7481
var ext = path.extname(filename);
7582

7683
if (config.debug) {
77-
console.log('processPatternIterative:', 'filename:', filename);
84+
console.log('processPatternIterative:', filename);
7885
}
7986

8087
// skip non-pattern files
8188
if (!patternEngines.isPatternFile(filename, patternlab)) { return null; }
82-
if (config.debug) {
83-
console.log('processPatternIterative:', 'found pattern', file);
84-
}
8589

8690
//make a new Pattern Object
8791
var currentPattern = new of.oPattern(file, subdir, filename);
@@ -104,7 +108,7 @@
104108
var jsonFilename = patternlab.config.patterns.source + currentPattern.subdir + '/' + currentPattern.fileName + ".json";
105109
currentPattern.jsonFileData = fs.readJSONSync(jsonFilename.substring(2));
106110
if(patternlab.config.debug){
107-
console.log('found pattern-specific data.json for ' + currentPattern.key);
111+
console.log('processPatternIterative: found pattern-specific data.json for ' + currentPattern.key);
108112
}
109113
}
110114
catch(e) {

builder/pattern_engines/engine_handlebars.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
// regexes, stored here so they're only compiled once
2222
// GTP warning: unchanged copypasta from mustache engine
23-
findPartialsRE: /{{>\s*((?:\d+-[\w-]+\/)+(\d+-[\w-]+(\.\w+)?)|[A-Za-z0-9-]+)(\:[\w-]+)?(\(\s*\w+\s*:\s*(?:'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")\))?\s*}}/g,
23+
// findPartialsRE: /{{>\s*((?:\d+-[\w-]+\/)+(\d+-[\w-]+(\.\w+)?)|[A-Za-z0-9-]+)(\:[\w-]+)?(\(\s*\w+\s*:\s*(?:'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")\))?\s*}}/g,
24+
findPartialsRE: /{{#?>\s*([\w-\/.]+)(?:.|\s+)*?}}/g,
2425
findPartialsWithStyleModifiersRE: /{{>([ ])?([\w\-\.\/~]+)(?!\()(\:[A-Za-z0-9-_|]+)+(?:(| )\(.*)?([ ])?}}/g,
2526
findPartialsWithPatternParametersRE: /{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-_|]+)?(?:(| )\(.*)+([ ])?}}/g,
2627
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,
27-
getPartialKeyRE: /{{>([ ])?([\w\-\.\/~]+)(:[A-z-_|]+)?(?:\:[A-Za-z0-9-_]+)?(?:(| )\(.*)?([ ])?}}/g,
2828

2929
// render it
3030
renderPattern: function renderPattern(template, data, partials) {
@@ -35,6 +35,10 @@
3535
return compiled(data);
3636
},
3737

38+
registerPartial: function (oPattern) {
39+
Handlebars.registerPartial(oPattern.key, oPattern.template);
40+
},
41+
3842
// find and return any {{> template-name }} within pattern
3943
findPartials: function findPartials(pattern) {
4044
var matches = pattern.template.match(this.findPartialsRE);
@@ -57,7 +61,7 @@
5761
// given a pattern, and a partial string, tease out the "pattern key" and
5862
// return it.
5963
getPartialKey: function(pattern, partialString) {
60-
var partialKey = partialString.replace(this.getPartialKeyRE, '$2');
64+
var partialKey = partialString.replace(this.findPartialsRE, '$1');
6165
return partialKey;
6266
}
6367
};

builder/patternlab.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/*
2-
* patternlab-node - v1.0.0 - 2015
3-
*
1+
/*
2+
* patternlab-node - v1.0.0 - 2015
3+
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
66
*
@@ -19,6 +19,7 @@ var patternlab_engine = function () {
1919
mh = require('./media_hunter'),
2020
pe = require('./pattern_exporter'),
2121
he = require('html-entities').AllHtmlEntities,
22+
util = require('util'),
2223
plutils = require('./utilities'),
2324
patternlab = {};
2425

@@ -49,7 +50,10 @@ var patternlab_engine = function () {
4950
//debug file can be written by setting flag on config.json
5051
if(patternlab.config.debug){
5152
console.log('writing patternlab debug file to ./patternlab.json');
52-
fs.outputFileSync('./patternlab.json', JSON.stringify(patternlab, null, 3));
53+
// fs.outputFileSync('./patternlab.json', JSON.stringify(patternlab, null, 3));
54+
fs.outputFileSync('./patternlab.json', util.inspect(patternlab, {
55+
depth: null
56+
}));
5357
}
5458
}
5559

@@ -114,7 +118,12 @@ var patternlab_engine = function () {
114118
}
115119

116120
pattern_assembler.process_pattern_recursive(file.substring(2), patternlab);
117-
});
121+
});
122+
123+
if (patternlab.config.debug) {
124+
console.log('pattern keys:', Object.keys(patternlab.patternsByKey));
125+
console.log('by keys length:', Object.keys(patternlab.patternsByKey).length, 'array length:', patternlab.patterns.length);
126+
}
118127

119128
//delete the contents of config.patterns.public before writing
120129
if(deletePatternDir){
@@ -145,7 +154,6 @@ var patternlab_engine = function () {
145154

146155
//export patterns if necessary
147156
pattern_exporter.export_patterns(patternlab);
148-
149157
}
150158

151159
function buildFrontEnd(){

test/engine_handlebars_tests.js

Lines changed: 71 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@
3232
}
3333

3434

35+
// function for testing sets of partials
36+
function testFindPartials(test, partialTests) {
37+
test.expect(partialTests.length + 1);
38+
39+
// setup current pattern from what we would have during execution
40+
// docs on partial syntax are here:
41+
// http://patternlab.io/docs/pattern-including.html
42+
var currentPattern = object_factory.oPattern.create(
43+
'/home/fakeuser/pl/source/_patterns/01-molecules/00-testing/00-test-mol.hbs', // abspath
44+
'01-molecules\\00-testing', // subdir
45+
'00-test-mol.hbs', // filename,
46+
null, // data
47+
{
48+
template: partialTests.join()
49+
}
50+
);
51+
52+
// act
53+
var results = currentPattern.findPartials();
54+
55+
// assert
56+
test.equals(results.length, partialTests.length);
57+
partialTests.forEach(function(testString, index) {
58+
test.equals(results[index], testString);
59+
});
60+
61+
test.done();
62+
}
63+
3564
exports['engine_handlebars'] = {
3665
'hello world handlebars pattern renders': function (test) {
3766
test.expect(1);
@@ -83,123 +112,53 @@
83112
test.equals(helloWorldsPattern.render(), 'Hello world!\n and Hello world!\n\n');
84113
test.done();
85114
},
86-
// GTP warning: unchanged copypasta from mustache engine
87115
'find_pattern_partials finds partials': function(test){
88-
// NOTES from GTP:
89-
// it's nice to have so much test coverage, but it retrospect, I'm not
90-
// happy with the structure I wound up with in this test; it's too
91-
// difficult to add test cases and test failure reporting is not very
92-
// granular.
93-
94-
test.expect(16);
95-
96-
// setup current pattern from what we would have during execution
97-
// docs on partial syntax are here:
98-
// http://patternlab.io/docs/pattern-including.html
99-
var currentPattern = object_factory.oPattern.create(
100-
'/home/fakeuser/pl/source/_patterns/01-molecules/00-testing/00-test-mol.hbs', // abspath
101-
'01-molecules\\00-testing', // subdir
102-
'00-test-mol.hbs', // filename,
103-
null, // data
104-
{
105-
template: "{{> molecules-comment-header}}asdfasdf" +
106-
"{{> molecules-comment-header}}" +
107-
"{{> \n molecules-comment-header\n}}" +
108-
"{{> }}" +
109-
"{{> molecules-weird-spacing }}" +
110-
"{{> molecules-ba_d-cha*rs }}" +
111-
"{{> molecules-single-comment(description: 'A life isn\\'t like a garden. Perfect moments can be had, but not preserved, except in memory.') }}" +
112-
'{{> molecules-single-comment(description: "A life is like a \\"garden\\". Perfect moments can be had, but not preserved, except in memory.") }}' +
113-
"{{> molecules-single-comment:foo }}" +
114-
// verbose partial syntax, introduced in v0.12.0, with file extension
115-
"{{> 01-molecules/06-components/03-comment-header.hbs }}" +
116-
"{{> 01-molecules/06-components/02-single-comment.hbs(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}" +
117-
"{{> molecules-single-comment:foo }}" +
118-
"{{>atoms-error(message: 'That\\'s no moon...')}}" +
119-
'{{>atoms-error(message: \'That\\\'s no moon...\')}}' +
120-
"{{> 00-atoms/00-global/ }}" +
121-
// verbose partial syntax, introduced in v0.12.0, no file extension
122-
"{{> 00-atoms/00-global/06-test }}" +
123-
"{{> molecules-single-comment:foo_1 }}" +
124-
"{{> molecules-single-comment:foo-1 }}"
125-
}
126-
);
127-
128-
var results = currentPattern.findPartials();
129-
console.log(results);
130-
test.equals(results.length, 15);
131-
test.equals(results[0], "{{> molecules-comment-header}}");
132-
test.equals(results[1], "{{> molecules-comment-header}}");
133-
test.equals(results[2], "{{> \n molecules-comment-header\n}}");
134-
test.equals(results[3], "{{> molecules-weird-spacing }}");
135-
test.equals(results[4], "{{> molecules-single-comment(description: 'A life isn\\'t like a garden. Perfect moments can be had, but not preserved, except in memory.') }}");
136-
test.equals(results[5], '{{> molecules-single-comment(description: "A life is like a \\"garden\\". Perfect moments can be had, but not preserved, except in memory.") }}');
137-
test.equals(results[6], "{{> molecules-single-comment:foo }}");
138-
test.equals(results[7], "{{> 01-molecules/06-components/03-comment-header.hbs }}");
139-
test.equals(results[8], "{{> 01-molecules/06-components/02-single-comment.hbs(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}");
140-
test.equals(results[9], "{{> molecules-single-comment:foo }}");
141-
test.equals(results[10], "{{>atoms-error(message: 'That\\'s no moon...')}}");
142-
test.equals(results[11], "{{>atoms-error(message: 'That\\'s no moon...')}}");
143-
test.equals(results[12], "{{> 00-atoms/00-global/06-test }}");
144-
test.equals(results[13], '{{> molecules-single-comment:foo_1 }}');
145-
test.equals(results[14], '{{> molecules-single-comment:foo-1 }}');
146-
test.done();
116+
testFindPartials(test, [
117+
"{{> molecules-comment-header}}",
118+
"{{> molecules-comment-header}}",
119+
"{{> \n molecules-comment-header\n}}",
120+
"{{> molecules-weird-spacing }}",
121+
"{{> molecules-ba_d-cha*rs }}"
122+
]);
147123
},
148-
// GTP warning: unchanged copypasta from mustache engine
149124
'find_pattern_partials finds verbose partials': function(test){
150-
test.expect(3);
151-
152-
//setup current pattern from what we would have during execution
153-
var currentPattern = new object_factory.oPattern(
154-
'/home/fakeuser/pl/source/_patterns/01-molecules/00-testing/00-test-mol.hbs', // abspath
155-
'01-molecules\\00-testing', // subdir
156-
'00-test-mol.hbs', // filename,
157-
null // data
158-
);
159-
currentPattern.template = "<h1>{{> 01-molecules/06-components/03-comment-header.hbs }}</h1><div>{{> 01-molecules/06-components/02-single-comment.hbs(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}</div>";
160-
161-
var results = currentPattern.findPartials();
162-
test.equals(results.length, 2);
163-
test.equals(results[0], '{{> 01-molecules/06-components/03-comment-header.hbs }}');
164-
test.equals(results[1], '{{> 01-molecules/06-components/02-single-comment.hbs(description: \'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.\') }}');
165-
test.done();
125+
testFindPartials(test, [
126+
'{{> 01-molecules/06-components/03-comment-header.hbs }}',
127+
"{{> 01-molecules/06-components/02-single-comment.hbs(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}",
128+
'{{> molecules-single-comment:foo }}',
129+
"{{>atoms-error(message: 'That\'s no moon...')}}",
130+
"{{> atoms-error(message: 'That\'s no moon...') }}",
131+
'{{> 00-atoms/00-global/06-test }}'
132+
]);
166133
},
167-
// GTP warning: unchanged copypasta from mustache engine
168-
'find_pattern_partials_with_parameters finds parameters with verbose partials': function(test){
169-
test.expect(2);
170-
171-
//setup current pattern from what we would have during execution
172-
var currentPattern = new object_factory.oPattern(
173-
'/home/fakeuser/pl/source/_patterns/01-molecules/00-testing/00-test-mol.hbs', // abspath
174-
'01-molecules\\00-testing', // subdir
175-
'00-test-mol.hbs', // filename,
176-
null // data
177-
);
178-
currentPattern.template = "<h1>{{> 01-molecules/06-components/molecules-comment-header}}</h1><div>{{> 01-molecules/06-components/molecules-single-comment(bar:'baz') }}</div>";
179-
180-
var results = currentPattern.findPartialsWithPatternParameters();
181-
test.equals(results.length, 1);
182-
test.equals(results[0], "{{> 01-molecules/06-components/molecules-single-comment(bar:'baz') }}");
183-
184-
test.done();
134+
'find_pattern_partials finds simple partials with parameters': function(test){
135+
testFindPartials(test, [
136+
"{{> molecules-single-comment(description: 'A life isn\'t like a garden. Perfect moments can be had, but not preserved, except in memory.') }}",
137+
'{{> molecules-single-comment(description:"A life is like a \"garden\". Perfect moments can be had, but not preserved, except in memory.") }}'
138+
]);
185139
},
186-
// GTP warning: unchanged copypasta from mustache engine
187-
'find_pattern_partials_with_parameters finds no style modifiers when only partials present': function(test){
188-
test.expect(1);
189-
190-
//setup current pattern from what we would have during execution
191-
var currentPattern = new object_factory.oPattern(
192-
'/home/fakeuser/pl/source/_patterns/01-molecules/00-testing/00-test-mol.hbs', // abspath
193-
'01-molecules\\00-testing', // subdir
194-
'00-test-mol.hbs', // filename,
195-
null // data
196-
);
197-
currentPattern.template = "<h1>{{> molecules-comment-header}}</h1><div>{{> molecules-single-comment }}</div>";
198-
199-
var results = currentPattern.findPartialsWithPatternParameters();
200-
test.equals(results, null);
201-
202-
test.done();
140+
'find_pattern_partials finds simple partials with style modifiers': function(test){
141+
testFindPartials(test, [
142+
'{{> molecules-single-comment:foo }}'
143+
]);
144+
},
145+
'find_pattern_partials finds partials with handlebars parameters': function(test){
146+
testFindPartials(test, [
147+
'{{> atoms-title title="bravo" headingLevel="2" headingSize="bravo" position="left"}}',
148+
'{{> atoms-title title="bravo"\n headingLevel="2"\n headingSize="bravo"\n position="left"}}',
149+
'{{> atoms-title title="color &nbsp;<span style=\'font-weight:normal\'>midnight blue</span>" headingSize="charlie"}}',
150+
'{{> atoms-input label="city" required=true}}',
151+
'{{> organisms-product-filter filterData}}',
152+
'{{> atoms-input email required=true}}',
153+
'{{> molecules-storycard variants.flex }}',
154+
'{{> myPartial name=../name }}'
155+
]);
156+
},
157+
158+
'find_pattern_partials finds handlebars block partials': function(test){
159+
testFindPartials(test, [
160+
'{{#> myPartial }}'
161+
]);
203162
}
204163
};
205164
})();

0 commit comments

Comments
 (0)