|
16 | 16 | var fs = require('fs-extra'), |
17 | 17 | of = require('./object_factory'), |
18 | 18 | path = require('path'), |
19 | | - patternEngines = require('./pattern_engines/pattern_engines'); |
| 19 | + patternEngines = require('./pattern_engines/pattern_engines'), |
| 20 | + config = fs.readJSONSync('./config.json'); |
20 | 21 |
|
21 | 22 | function isObjectEmpty(obj) { |
22 | 23 | for(var prop in obj) { |
|
26 | 27 |
|
27 | 28 | return true; |
28 | 29 | } |
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 | | - } |
52 | 30 |
|
53 | 31 | function setState(pattern, patternlab){ |
54 | 32 | if(patternlab.config.patternStates[pattern.patternName]){ |
|
82 | 60 | // if we've been passed a full oPattern, it knows what kind of template it |
83 | 61 | // is, and how to render itself, so we just call its render method |
84 | 62 | 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 | + } |
86 | 66 | return pattern.render(data, partials); |
87 | 67 | } else { |
88 | 68 | // otherwise, assume it's a plain mustache template string and act |
89 | 69 | // 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 | + } |
91 | 73 | return patternEngines.mustache.renderPattern(pattern, data, partials); |
92 | 74 | } |
93 | 75 | } |
|
106 | 88 | } |
107 | 89 |
|
108 | 90 | // 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(); |
113 | 92 | return (supportedPatternFileExtensions.lastIndexOf(extension) !== -1); |
114 | 93 | } |
115 | 94 |
|
|
123 | 102 | var filename = path.basename(file); |
124 | 103 | var ext = path.extname(filename); |
125 | 104 |
|
126 | | - console.log('processPatternIterative:', 'filename:', filename); |
| 105 | + if (config.debug) { |
| 106 | + console.log('processPatternIterative:', 'filename:', filename); |
| 107 | + } |
127 | 108 |
|
128 | 109 | // skip non-pattern files |
129 | 110 | if (!isPatternFile(filename, patternlab)) { return; } |
130 | | - console.log('found pattern', file); |
| 111 | + if (config.debug) { |
| 112 | + console.log('processPatternIterative:', 'found pattern', file); |
| 113 | + } |
131 | 114 |
|
132 | 115 | //make a new Pattern Object |
133 | 116 | var currentPattern = new of.oPattern(file, subdir, filename); |
|
144 | 127 |
|
145 | 128 | //can ignore all non-mustache files at this point |
146 | 129 | if(ext !== '.mustache'){ |
147 | | - console.log('==================== FOUND NON-MUSTACHE FILE'); |
| 130 | + if (config.debug) { |
| 131 | + console.log('==================== FOUND NON-MUSTACHE FILE'); |
| 132 | + } |
148 | 133 | return; |
149 | 134 | } |
150 | 135 |
|
|
178 | 163 | currentPattern.template = fs.readFileSync(file, 'utf8'); |
179 | 164 |
|
180 | 165 | //find any stylemodifiers that may be in the current pattern |
181 | | - currentPattern.stylePartials = findPartialsWithStyleModifiers(currentPattern); |
| 166 | + currentPattern.stylePartials = currentPattern.findPartialsWithStyleModifiers(); |
182 | 167 |
|
183 | 168 | //find any pattern parameters that may be in the current pattern |
184 | | - currentPattern.parameteredPartials = findPartialsWithPatternParameters(currentPattern); |
| 169 | + currentPattern.parameteredPartials = currentPattern.findPartialsWithPatternParameters(); |
185 | 170 |
|
186 | 171 | //add currentPattern to patternlab.patterns array |
187 | 172 | addPattern(currentPattern, patternlab); |
|
218 | 203 | currentPattern.extendedTemplate = currentPattern.template; |
219 | 204 |
|
220 | 205 | //find how many partials there may be for the given pattern |
221 | | - var foundPatternPartials = findPartials(currentPattern); |
| 206 | + var foundPatternPartials = currentPattern.findPartials(currentPattern); |
222 | 207 |
|
223 | 208 | if(foundPatternPartials !== null && foundPatternPartials.length > 0){ |
224 | 209 |
|
|
356 | 341 |
|
357 | 342 | return { |
358 | 343 | find_pattern_partials: function(pattern){ |
359 | | - return findPartials(pattern); |
| 344 | + return pattern.findPartials(); |
360 | 345 | }, |
361 | 346 | find_pattern_partials_with_style_modifiers: function(pattern){ |
362 | | - return findPartialsWithStyleModifiers(pattern); |
| 347 | + return pattern.findPartialsWithStyleModifiers(); |
363 | 348 | }, |
364 | 349 | find_pattern_partials_with_parameters: function(pattern){ |
365 | | - return findPartialsWithPatternParameters(pattern); |
| 350 | + return pattern.findPartialsWithPatternParameters(); |
366 | 351 | }, |
367 | 352 | find_list_items: function(pattern){ |
368 | | - return findListItems(pattern) |
| 353 | + return pattern.findListItems(); |
369 | 354 | }, |
370 | 355 | setPatternState: function(pattern, patternlab){ |
371 | 356 | setState(pattern, patternlab); |
|
0 commit comments