|
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); |
| 84 | + var ext = path.extname(filename); |
70 | 85 |
|
71 | | - //ignore _underscored patterns, json (for now), and dotfiles |
72 | | - if(filename.charAt(0) === '_' || path.extname(filename) === '.json' || filename.charAt(0) === '.'){ |
| 86 | + //ignore dotfiles and non-variant .json files |
| 87 | + if(filename.charAt(0) === '.' || (ext === '.json' && filename.indexOf('~') === -1)){ |
73 | 88 | return; |
74 | 89 | } |
75 | 90 |
|
76 | 91 | //make a new Pattern Object |
77 | | - var currentPattern = new of.oPattern(subdir, filename); |
| 92 | + var currentPattern = new of.oPattern(file, subdir, filename); |
| 93 | + |
| 94 | + //if file is named in the syntax for variants |
| 95 | + if(ext === '.json' && filename.indexOf('~') > -1){ |
| 96 | + //add current pattern to patternlab object with minimal data |
| 97 | + //processPatternRecursive() will run find_pseudopatterns() to fill out |
| 98 | + //the object in the next diveSync |
| 99 | + addPattern(currentPattern, patternlab); |
| 100 | + //no need to process further |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + //can ignore all non-mustache files at this point |
| 105 | + if(ext !== '.mustache'){ |
| 106 | + return; |
| 107 | + } |
78 | 108 |
|
79 | 109 | //see if this file has a state |
80 | 110 | setState(currentPattern, patternlab); |
|
98 | 128 | } |
99 | 129 |
|
100 | 130 | //add the raw template to memory |
101 | | - currentPattern.template = fs.readFileSync(abspath, 'utf8'); |
| 131 | + currentPattern.template = fs.readFileSync(file, 'utf8'); |
102 | 132 |
|
103 | | - //our helper function that does a lot of heavy lifting |
104 | | - processPattern(currentPattern, patternlab); |
| 133 | + //add currentPattern to patternlab.patterns array |
| 134 | + addPattern(currentPattern, patternlab); |
105 | 135 | } |
106 | 136 |
|
107 | | - function processPattern(currentPattern, patternlab, additionalData){ |
| 137 | + function processPatternRecursive(file, patternlab, additionalData){ |
108 | 138 |
|
109 | 139 | var fs = require('fs-extra'), |
110 | 140 | mustache = require('mustache'), |
|
119 | 149 | list_item_hunter = new lih(), |
120 | 150 | pseudopattern_hunter = new pph(); |
121 | 151 |
|
| 152 | + //find current pattern in patternlab object using var file as a key |
| 153 | + var currentPattern, |
| 154 | + i; |
| 155 | + |
| 156 | + for(i = 0; i < patternlab.patterns.length; i++){ |
| 157 | + if(patternlab.patterns[i].abspath === file){ |
| 158 | + currentPattern = patternlab.patterns[i]; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + //return if processing an ignored file |
| 163 | + if(typeof currentPattern === 'undefined'){ |
| 164 | + return; |
| 165 | + } |
| 166 | + |
122 | 167 | currentPattern.extendedTemplate = currentPattern.template; |
123 | 168 |
|
124 | 169 | //find how many partials there may be for the given pattern |
|
137 | 182 | parameter_hunter.find_parameters(currentPattern, patternlab); |
138 | 183 |
|
139 | 184 | //do something with the regular old partials |
140 | | - for(var i = 0; i < foundPatternPartials.length; i++){ |
| 185 | + for(i = 0; i < foundPatternPartials.length; i++){ |
141 | 186 | var partialKey = foundPatternPartials[i].replace(/{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ])?}}/g, '$2'); |
| 187 | + var partialPath; |
| 188 | + |
| 189 | + //identify which pattern this partial corresponds to |
| 190 | + for(var j = 0; j < patternlab.patterns.length; j++){ |
| 191 | + if(patternlab.patterns[j].key === partialKey || |
| 192 | + patternlab.patterns[j].abspath.indexOf(partialKey) > -1) |
| 193 | + { |
| 194 | + partialPath = patternlab.patterns[j].abspath; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + //recurse through nested partials to fill out this extended template. |
| 199 | + processPatternRecursive(partialPath, patternlab); |
| 200 | + |
| 201 | + //complete assembly of extended template |
142 | 202 | var partialPattern = getpatternbykey(partialKey, patternlab); |
143 | 203 | currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], partialPattern.extendedTemplate); |
144 | 204 | } |
|
248 | 308 | renderPattern: function(template, data, partials){ |
249 | 309 | return renderPattern(template, data, partials); |
250 | 310 | }, |
251 | | - process_pattern_file: function(file, patternlab){ |
252 | | - processPatternFile(file, patternlab); |
| 311 | + process_pattern_iterative: function(file, patternlab){ |
| 312 | + processPatternIterative(file, patternlab); |
253 | 313 | }, |
254 | | - process_pattern: function(pattern, patternlab, additionalData){ |
255 | | - processPattern(pattern, patternlab, additionalData); |
| 314 | + process_pattern_recursive: function(file, patternlab, additionalData){ |
| 315 | + processPatternRecursive(file, patternlab, additionalData); |
256 | 316 | }, |
257 | 317 | get_pattern_by_key: function(key, patternlab){ |
258 | 318 | return getpatternbykey(key, patternlab); |
|
0 commit comments