|
1 | | -/* |
2 | | - * patternlab-node - v0.15.1 - 2015 |
3 | | - * |
| 1 | +/* |
| 2 | + * patternlab-node - v1.0.0 - 2015 |
| 3 | + * |
4 | 4 | * Brian Muenzenmeyer, and the web community. |
5 | | - * Licensed under the MIT license. |
6 | | - * |
7 | | - * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice. |
| 5 | + * Licensed under the MIT license. |
| 6 | + * |
| 7 | + * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice. |
8 | 8 | * |
9 | 9 | */ |
10 | 10 |
|
|
46 | 46 | } |
47 | 47 |
|
48 | 48 | function setState(pattern, patternlab){ |
49 | | - if(patternlab.config.patternStates[pattern.patternName]){ |
| 49 | + if(patternlab.config.patternStates && patternlab.config.patternStates[pattern.patternName]){ |
50 | 50 | pattern.patternState = patternlab.config.patternStates[pattern.patternName]; |
51 | 51 | } else{ |
52 | 52 | pattern.patternState = ""; |
53 | 53 | } |
54 | 54 | } |
55 | 55 |
|
56 | 56 | function addPattern(pattern, patternlab){ |
| 57 | + //add the link to the global object |
57 | 58 | patternlab.data.link[pattern.patternGroup + '-' + pattern.patternName] = '/patterns/' + pattern.patternLink; |
58 | 59 |
|
59 | 60 | //only push to array if the array doesn't contain this pattern |
|
94 | 95 | var filename = path.basename(file); |
95 | 96 | var ext = path.extname(filename); |
96 | 97 |
|
97 | | - //ignore dotfiles and non-variant .json files |
98 | | - if(filename.charAt(0) === '.' || (ext === '.json' && filename.indexOf('~') === -1)){ |
| 98 | + //ignore dotfiles, underscored files, and non-variant .json files |
| 99 | + if(filename.charAt(0) === '.' || filename.charAt(0) === '_' || (ext === '.json' && filename.indexOf('~') === -1)){ |
99 | 100 | return; |
100 | 101 | } |
101 | 102 |
|
102 | 103 | //make a new Pattern Object |
103 | 104 | var currentPattern = new of.oPattern(file, subdir, filename); |
104 | 105 |
|
105 | | - //if file is named in the syntax for variants |
| 106 | + //if file is named in the syntax for variants, no need to process further |
| 107 | + //processPatternRecursive() will run find_pseudopatterns() and look at each pattern for a variant |
106 | 108 | if(ext === '.json' && filename.indexOf('~') > -1){ |
107 | | - //add current pattern to patternlab object with minimal data |
108 | | - //processPatternRecursive() will run find_pseudopatterns() to fill out |
109 | | - //the object in the next diveSync |
110 | | - addPattern(currentPattern, patternlab); |
111 | | - //no need to process further |
112 | 109 | return; |
113 | 110 | } |
114 | 111 |
|
|
321 | 318 | return o; |
322 | 319 | } |
323 | 320 |
|
| 321 | + //look for pattern links included in data files. |
| 322 | + //these will be in the form of link.* WITHOUT {{}}, which would still be there from direct pattern inclusion |
| 323 | + function parseDataLinks(patternlab){ |
| 324 | + |
| 325 | + //loop through all patterns |
| 326 | + for (var i = 0; i < patternlab.patterns.length; i++){ |
| 327 | + var pattern = patternlab.patterns[i]; |
| 328 | + //look for link.* such as link.pages-blog as a value |
| 329 | + var linkRE = /link.[A-z0-9-_]+/g; |
| 330 | + //convert to string for easier searching |
| 331 | + var dataObjAsString = JSON.stringify(pattern.jsonFileData); |
| 332 | + var linkMatches = dataObjAsString.match(linkRE); |
| 333 | + |
| 334 | + //if no matches found, escape current loop iteration |
| 335 | + if(linkMatches === null) { continue; } |
| 336 | + |
| 337 | + for(var i = 0; i < linkMatches.length; i++){ |
| 338 | + //for each match, find the expanded link within the already constructed patternlab.data.link object |
| 339 | + var expandedLink = patternlab.data.link[linkMatches[i].split('.')[1]]; |
| 340 | + if(patternlab.config.debug){ |
| 341 | + console.log('expanded data link from ' + linkMatches[i] + ' to ' + expandedLink + ' inside ' + pattern.key); |
| 342 | + } |
| 343 | + //replace value with expandedLink on the pattern |
| 344 | + dataObjAsString = dataObjAsString.replace(linkMatches[i], expandedLink); |
| 345 | + } |
| 346 | + //write back to data on the pattern |
| 347 | + pattern.jsonFileData = JSON.parse(dataObjAsString); |
| 348 | + } |
| 349 | + } |
| 350 | + |
324 | 351 | return { |
325 | 352 | find_pattern_partials: function(pattern){ |
326 | 353 | return findPartials(pattern); |
|
360 | 387 | }, |
361 | 388 | is_object_empty: function(obj){ |
362 | 389 | return isObjectEmpty(obj); |
| 390 | + }, |
| 391 | + parse_data_links: function(patternlab){ |
| 392 | + parseDataLinks(patternlab); |
363 | 393 | } |
364 | 394 | }; |
365 | 395 |
|
|
0 commit comments