|
44 | 44 |
|
45 | 45 | function addPattern(pattern, patternlab){
|
46 | 46 | 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 | + } |
48 | 63 | }
|
49 | 64 |
|
50 | 65 | function renderPattern(template, data, partials) {
|
|
58 | 73 | }
|
59 | 74 | }
|
60 | 75 |
|
61 |
| - function processPatternFile(file, patternlab){ |
| 76 | + function processPatternIterative(file, patternlab){ |
62 | 77 | var fs = require('fs-extra'),
|
63 | 78 | of = require('./object_factory'),
|
64 | 79 | path = require('path');
|
65 | 80 |
|
66 | 81 | //extract some information
|
67 |
| - var abspath = file.substring(2); |
68 | 82 | var subdir = path.dirname(path.relative(patternlab.config.patterns.source, file)).replace('\\', '/');
|
69 | 83 | var filename = path.basename(file);
|
70 | 84 |
|
|
74 | 88 | }
|
75 | 89 |
|
76 | 90 | //make a new Pattern Object
|
77 |
| - var currentPattern = new of.oPattern(subdir, filename); |
| 91 | + var currentPattern = new of.oPattern(file, subdir, filename); |
78 | 92 |
|
79 | 93 | //see if this file has a state
|
80 | 94 | setState(currentPattern, patternlab);
|
|
98 | 112 | }
|
99 | 113 |
|
100 | 114 | //add the raw template to memory
|
101 |
| - currentPattern.template = fs.readFileSync(abspath, 'utf8'); |
| 115 | + currentPattern.template = fs.readFileSync(file, 'utf8'); |
102 | 116 |
|
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); |
105 | 119 | }
|
106 | 120 |
|
107 |
| - function processPattern(currentPattern, patternlab, additionalData){ |
| 121 | + function processPatternRecursive(file, patternlab, additionalData){ |
108 | 122 |
|
109 | 123 | var fs = require('fs-extra'),
|
110 | 124 | mustache = require('mustache'),
|
|
119 | 133 | list_item_hunter = new lih(),
|
120 | 134 | pseudopattern_hunter = new pph();
|
121 | 135 |
|
| 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 | + |
122 | 148 | currentPattern.extendedTemplate = currentPattern.template;
|
123 | 149 |
|
124 | 150 | //find how many partials there may be for the given pattern
|
|
137 | 163 | parameter_hunter.find_parameters(currentPattern, patternlab);
|
138 | 164 |
|
139 | 165 | //do something with the regular old partials
|
140 |
| - for(var i = 0; i < foundPatternPartials.length; i++){ |
| 166 | + for(i = 0; i < foundPatternPartials.length; i++){ |
141 | 167 | 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 |
142 | 184 | var partialPattern = getpatternbykey(partialKey, patternlab);
|
143 | 185 | currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], partialPattern.extendedTemplate);
|
144 | 186 | }
|
|
248 | 290 | renderPattern: function(template, data, partials){
|
249 | 291 | return renderPattern(template, data, partials);
|
250 | 292 | },
|
251 |
| - process_pattern_file: function(file, patternlab){ |
252 |
| - processPatternFile(file, patternlab); |
| 293 | + process_pattern_iterative: function(file, patternlab){ |
| 294 | + processPatternIterative(file, patternlab); |
253 | 295 | },
|
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); |
256 | 298 | },
|
257 | 299 | get_pattern_by_key: function(key, patternlab){
|
258 | 300 | return getpatternbykey(key, patternlab);
|
|
0 commit comments