Skip to content

Commit 2031290

Browse files
author
e2tha-e
committed
recursive partial include
1 parent da94847 commit 2031290

File tree

4 files changed

+81
-16
lines changed

4 files changed

+81
-16
lines changed

builder/object_factory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
(function () {
1212
"use strict";
1313

14-
var oPattern = function(subdir, filename, data){
14+
var oPattern = function(abspath, subdir, filename, data){
1515
this.fileName = filename.substring(0, filename.indexOf('.'));
16+
this.abspath = abspath;
1617
this.subdir = subdir;
1718
this.name = subdir.replace(/[\/\\]/g, '-') + '-' + this.fileName; //this is the unique name with the subDir
1819
this.jsonFileData = data || {};

builder/pattern_assembler.js

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@
4444

4545
function addPattern(pattern, patternlab){
4646
patternlab.data.link[pattern.patternGroup + '-' + pattern.patternName] = '/patterns/' + pattern.patternLink;
47-
patternlab.patterns.push(pattern);
47+
48+
//only push to array if the array doesn't contain this pattern
49+
var isNew = true;
50+
for(var i = 0; i < patternlab.patterns.length; i++){
51+
//so we need the identifier to be unique, which patterns[i].abspath is
52+
if(pattern.abspath === patternlab.patterns[i].abspath){
53+
//if abspath already exists, overwrite that element
54+
patternlab.patterns[i] = pattern;
55+
isNew = false;
56+
break;
57+
}
58+
}
59+
//if the pattern is new, just push to the array
60+
if(isNew){
61+
patternlab.patterns.push(pattern);
62+
}
4863
}
4964

5065
function renderPattern(template, data, partials) {
@@ -58,13 +73,12 @@
5873
}
5974
}
6075

61-
function processPatternFile(file, patternlab){
76+
function processPatternIterative(file, patternlab){
6277
var fs = require('fs-extra'),
6378
of = require('./object_factory'),
6479
path = require('path');
6580

6681
//extract some information
67-
var abspath = file.substring(2);
6882
var subdir = path.dirname(path.relative(patternlab.config.patterns.source, file)).replace('\\', '/');
6983
var filename = path.basename(file);
7084

@@ -74,7 +88,7 @@
7488
}
7589

7690
//make a new Pattern Object
77-
var currentPattern = new of.oPattern(subdir, filename);
91+
var currentPattern = new of.oPattern(file, subdir, filename);
7892

7993
//see if this file has a state
8094
setState(currentPattern, patternlab);
@@ -98,13 +112,13 @@
98112
}
99113

100114
//add the raw template to memory
101-
currentPattern.template = fs.readFileSync(abspath, 'utf8');
115+
currentPattern.template = fs.readFileSync(file, 'utf8');
102116

103-
//our helper function that does a lot of heavy lifting
104-
processPattern(currentPattern, patternlab);
117+
//add currentPattern to patternlab.patterns array
118+
addPattern(currentPattern, patternlab);
105119
}
106120

107-
function processPattern(currentPattern, patternlab, additionalData){
121+
function processPatternRecursive(file, patternlab, additionalData){
108122

109123
var fs = require('fs-extra'),
110124
mustache = require('mustache'),
@@ -119,6 +133,18 @@
119133
list_item_hunter = new lih(),
120134
pseudopattern_hunter = new pph();
121135

136+
var currentPattern, i;
137+
for(i = 0; i < patternlab.patterns.length; i++){
138+
if(patternlab.patterns[i].abspath === file){
139+
currentPattern = patternlab.patterns[i];
140+
}
141+
}
142+
143+
//return if processing a .json file
144+
if(typeof currentPattern === 'undefined'){
145+
return;
146+
}
147+
122148
currentPattern.extendedTemplate = currentPattern.template;
123149

124150
//find how many partials there may be for the given pattern
@@ -137,8 +163,24 @@
137163
parameter_hunter.find_parameters(currentPattern, patternlab);
138164

139165
//do something with the regular old partials
140-
for(var i = 0; i < foundPatternPartials.length; i++){
166+
for(i = 0; i < foundPatternPartials.length; i++){
141167
var partialKey = foundPatternPartials[i].replace(/{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ])?}}/g, '$2');
168+
var partialPath;
169+
170+
//identify which pattern this partial corresponds to
171+
for(var j = 0; j < patternlab.patterns.length; j++){
172+
if(patternlab.patterns[j].key === partialKey ||
173+
patternlab.patterns[j].abspath === 'source/_patterns/' + partialKey ||
174+
patternlab.patterns[j].abspath === 'source/_patterns/' + partialKey + '.mustache')
175+
{
176+
partialPath = patternlab.patterns[j].abspath;
177+
}
178+
}
179+
180+
//recurse through nested partials to fill out this extended template.
181+
processPatternRecursive(partialPath, patternlab);
182+
183+
//complete assembly of extended template
142184
var partialPattern = getpatternbykey(partialKey, patternlab);
143185
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], partialPattern.extendedTemplate);
144186
}
@@ -248,11 +290,11 @@
248290
renderPattern: function(template, data, partials){
249291
return renderPattern(template, data, partials);
250292
},
251-
process_pattern_file: function(file, patternlab){
252-
processPatternFile(file, patternlab);
293+
process_pattern_iterative: function(file, patternlab){
294+
processPatternIterative(file, patternlab);
253295
},
254-
process_pattern: function(pattern, patternlab, additionalData){
255-
processPattern(pattern, patternlab, additionalData);
296+
process_pattern_recursive: function(file, patternlab, additionalData){
297+
processPatternRecursive(file, patternlab, additionalData);
256298
},
257299
get_pattern_by_key: function(key, patternlab){
258300
return getpatternbykey(key, patternlab);

builder/patternlab.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,27 @@ var patternlab_engine = function () {
8787
return;
8888
}
8989

90-
pattern_assembler.process_pattern_file(file, patternlab);
90+
pattern_assembler.process_pattern_iterative(file.substring(2), patternlab);
91+
});
92+
93+
diveSync(patterns_dir, {
94+
filter: function(path, dir) {
95+
if(dir){
96+
var remainingPath = path.replace(patterns_dir, '');
97+
var isValidPath = remainingPath.indexOf('/_') === -1;
98+
return isValidPath;
99+
}
100+
return true;
101+
}
102+
},
103+
function(err, file){
104+
//log any errors
105+
if(err){
106+
console.log(err);
107+
return;
108+
}
109+
110+
pattern_assembler.process_pattern_recursive(file.substring(2), patternlab);
91111
});
92112

93113
//delete the contents of config.patterns.public before writing

builder/pseudopattern_hunter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
variantFileData = pattern_assembler.merge_data(currentPattern.jsonFileData, variantFileData);
4949

5050
var variantName = pseudoPatterns[i].substring(pseudoPatterns[i].indexOf('~') + 1).split('.')[0];
51-
var patternVariant = new of.oPattern(currentPattern.subdir, currentPattern.fileName + '-' + variantName + '.mustache', variantFileData);
51+
var variantFilePath = 'source/_patterns/' + currentPattern.subdir + '/' + currentPattern.fileName + '-' + variantName + '.mustache';
52+
var variantFileName = currentPattern.fileName + '-' + variantName + '.mustache';
53+
var patternVariant = new of.oPattern(variantFilePath, currentPattern.subdir, variantFileName, variantFileData);
5254

5355
//see if this file has a state
5456
pattern_assembler.setPatternState(patternVariant, patternlab);

0 commit comments

Comments
 (0)