Skip to content

Commit c6a5740

Browse files
committed
progress on the re-write of the parse
1 parent 8d52aa9 commit c6a5740

15 files changed

+380
-161
lines changed

.jslintrc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"ass": false,
3+
"bitwise": false,
4+
"browser": false,
5+
"closure": false,
6+
"continue": false,
7+
"couch": false,
8+
"debug": false,
9+
"devel": true,
10+
"eqeq": false,
11+
"eval": true,
12+
"evil": false,
13+
"forin": false,
14+
"indent": 2,
15+
"maxerr": 50,
16+
"maxlen": false,
17+
"newcap": false,
18+
"node": true,
19+
"nomen": false,
20+
"passfail": false,
21+
"plusplus": false,
22+
"predef": [
23+
"node",
24+
"$",
25+
"require"
26+
],
27+
"regexp": false,
28+
"rhino": false,
29+
"sloppy": false,
30+
"stupid": false,
31+
"sub": false,
32+
"todo": false,
33+
"unparam": true,
34+
"vars": false,
35+
"white": true
36+
}

Gruntfile.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,21 @@ module.exports = function(grunt) {
3232
src: './builder/patternlab_grunt.js',
3333
dest: './builder/patternlab_grunt.js'
3434
},
35+
parameter_hunter: {
36+
src: './builder/parameter_hunter.js',
37+
dest: './builder/parameter_hunter.js'
38+
},
3539
pattern_exporter: {
3640
src: './builder/pattern_exporter.js',
3741
dest: './builder/pattern_exporter.js'
42+
},
43+
pattern_assembler: {
44+
src: './builder/pattern_assembler.js',
45+
dest: './builder/pattern_assembler.js'
46+
},
47+
pseudopattern_hunter: {
48+
src: './builder/pseudopattern_hunter.js',
49+
dest: './builder/pseudopattern_hunter.js'
3850
}
3951
},
4052
copy: {
@@ -125,4 +137,4 @@ module.exports = function(grunt) {
125137

126138
grunt.registerTask('serve', ['clean', 'concat', 'patternlab', /*'sass',*/ 'copy', 'connect', 'watch']);
127139

128-
};
140+
};

builder/lineage_hunter.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.9.1 - 2015
2+
* patternlab-node - v0.10.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
@@ -13,14 +13,17 @@
1313

1414
var lineage_hunter = function(){
1515

16+
var pa = require('./pattern_assembler');
17+
var pattern_assembler = new pa();
18+
1619
function findlineage(pattern, patternlab){
1720

1821
//find the {{> template-name }} within patterns
19-
var matches = pattern.template.match(/{{>([ ]+)?([A-Za-z0-9-]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ]+)}}/g);
22+
var matches = pattern_assembler.find_pattern_partials(pattern);
2023
if(matches !== null){
2124
matches.forEach(function(match, index, matches){
2225
//strip out the template cruft
23-
var foundPattern = match.replace("{{> ", "").replace(" }}", "");
26+
var foundPattern = match.replace("{{> ", "").replace(" }}", "").replace("{{>", "").replace("}}", "");
2427

2528
//add if it doesnt exist
2629
if (pattern.lineageIndex.indexOf(foundPattern) === -1){
@@ -71,4 +74,4 @@
7174

7275
module.exports = lineage_hunter;
7376

74-
}());
77+
}());

builder/media_hunter.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.9.1 - 2015
2+
* patternlab-node - v0.10.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
@@ -32,7 +32,7 @@
3232
}
3333
}
3434
}
35-
});
35+
});
3636
//alpha sort for now, but should meet most use-cases except greater than 100ems. you are using ems right?
3737
patternlab.mediaQueries.sort();
3838
}
@@ -48,22 +48,3 @@
4848
module.exports = media_hunter;
4949

5050
}());
51-
52-
// protected function gatherMQs() {
53-
54-
// $mqs = array();
55-
56-
// foreach(glob($this->sd."/css/*.css") as $filename) {
57-
// $data = file_get_contents($filename);
58-
// preg_match_all("/(min|max)-width:([ ]+)?(([0-9]{1,5})(\.[0-9]{1,20}|)(px|em))/",$data,$matches);
59-
// foreach ($matches[3] as $match) {
60-
// if (!in_array($match,$mqs)) {
61-
// $mqs[] = $match;
62-
// }
63-
// }
64-
// }
65-
66-
// sort($mqs);
67-
// return $mqs;
68-
69-
// }

builder/object_factory.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.9.1 - 2015
2+
* patternlab-node - v0.10.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
@@ -16,6 +16,7 @@
1616
this.subdir = subdir;
1717
this.name = subdir.replace(/[\/\\]/g, '-') + '-' + this.fileName; //this is the unique name with the subDir
1818
this.data = data || null;
19+
this.jsonFileData = {};
1920
this.patternName = this.fileName.substring(this.fileName.indexOf('-') + 1); //this is the display name for the ui
2021
this.patternLink = this.name + '/' + this.name + '.html';
2122
this.patternGroup = this.name.substring(this.name.indexOf('-') + 1, this.name.indexOf('-', 4) + 1 - this.name.indexOf('-') + 1);
@@ -59,4 +60,4 @@
5960
oNavSubItem: oNavSubItem
6061
};
6162

62-
}());
63+
}());

builder/parameter_hunter.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* patternlab-node - v0.10.0 - 2015
3+
*
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.
8+
*
9+
*/
10+
11+
(function () {
12+
"use strict";
13+
14+
var parameter_hunter = function(){
15+
16+
var extend = require('util')._extend,
17+
pa = require('./pattern_assembler'),
18+
mustache = require('mustache'),
19+
pattern_assembler = new pa();
20+
21+
function findparameters(pattern, patternlab){
22+
23+
//find the {{> template-name(*) }} within patterns
24+
var matches = pattern.template.match(/{{>([ ]+)?([A-Za-z0-9-]+)(\()(.+)(\))([ ]+)?}}/g);
25+
if(matches !== null){
26+
matches.forEach(function(pMatch, index, matches){
27+
//find the partial's name
28+
var partialName = pMatch.match(/([a-z-]+)/ig)[0]
29+
30+
//strip out the additional data and eval
31+
var leftParen = pMatch.indexOf('(');
32+
var rightParen = pMatch.indexOf(')');
33+
var paramString = '({' + pMatch.substring(leftParen + 1, rightParen) + '})';
34+
35+
//do no evil. there is no good way to do this that I can think of without using a split, which then makes commas and colons special characters and unusable within the pattern params
36+
var paramData = eval(paramString);
37+
38+
//compile this partial immeadiately, essentially consuming it.
39+
//TODO: see how this affects lineage. perhaps add manually here.
40+
var partial = patternlab.partials[partialName];
41+
var existingData = pattern.data || patternlab.data;
42+
43+
//merge paramData with any other data that exists.
44+
for (var prop in paramData) {
45+
if (existingData.hasOwnProperty(prop)) {
46+
existingData[prop] = paramData[prop];
47+
}
48+
}
49+
50+
//extend pattern data links into link for pattern link shortcuts to work. we do this locally and globally
51+
existingData.link = extend({}, patternlab.data.link);
52+
53+
var renderedPartial = pattern_assembler.renderPattern(partial, existingData, patternlab.partials);
54+
55+
//remove the parameter from the partial and replace it with the rendered partial + paramData
56+
pattern.template = pattern.template.replace(pMatch, renderedPartial);
57+
58+
//TODO see if other data works
59+
60+
});
61+
}
62+
return matches.length;
63+
}
64+
65+
return {
66+
find_parameters: function(pattern, patternlab){
67+
return findparameters(pattern, patternlab);
68+
}
69+
};
70+
71+
};
72+
73+
module.exports = parameter_hunter;
74+
75+
}());

builder/pattern_assembler.js

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1+
/*
2+
* patternlab-node - v0.10.0 - 2015
3+
*
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.
8+
*
9+
*/
10+
111
(function () {
212
"use strict";
313

414
var fs = require('fs-extra'),
15+
mustache = require('mustache'),
16+
ph = require('./parameter_hunter'),
517
path = require('path');
618

19+
var parameter_hunter = new ph();
20+
721
var pattern_assembler = function(){
822

23+
//find and return any {{> template-name }} within pattern
24+
function findPartials(pattern){
25+
var matches = pattern.template.match(/{{>([ ])?([A-Za-z0-9-]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ])?}}/g);
26+
return matches;
27+
}
28+
929
function setState(pattern, patternlab){
1030
if(patternlab.config.patternStates[pattern.patternName]){
1131
pattern.patternState = patternlab.config.patternStates[pattern.patternName];
@@ -14,22 +34,119 @@
1434
}
1535
}
1636

17-
function addPattern(pattern, patternLab){
18-
patternLab.data.link[pattern.patternGroup + '-' + pattern.patternName] = '/patterns/' + pattern.patternLink;
19-
patternLab.patterns.push(pattern);
37+
function addPattern(pattern, patternlab){
38+
patternlab.data.link[pattern.patternGroup + '-' + pattern.patternName] = '/patterns/' + pattern.patternLink;
39+
patternlab.patterns.push(pattern);
40+
}
41+
42+
function renderPattern(template, data, partials) {
43+
if(partials) {
44+
return mustache.render(template, data, partials);
45+
}else{
46+
return mustache.render(template, data);
47+
}
48+
}
49+
50+
function processPatternFile(file, patternlab){
51+
//extract some information
52+
var abspath = file.substring(2);
53+
var subdir = path.dirname(path.relative('./source/_patterns', file)).replace('\\', '/');
54+
var filename = path.basename(file);
55+
56+
//ignore _underscored patterns, json (for now), and dotfiles
57+
if(filename.charAt(0) === '_' || path.extname(filename) === '.json' || filename.charAt(0) === '.'){
58+
return;
59+
}
60+
61+
//make a new Pattern Object
62+
currentPattern = new of.oPattern(subdir, filename);
63+
64+
//see if this file has a state
65+
pattern_assembler.setPatternState(currentPattern, patternlab);
66+
67+
//look for a json file for this template
68+
try {
69+
var jsonFilename = abspath.substr(0, abspath.lastIndexOf(".")) + ".json";
70+
currentPattern.jsonFileData = fs.readJSONSync(jsonFilename);
71+
}
72+
catch(e) {
73+
}
74+
75+
//add the raw template to memory
76+
currentPattern.template = fs.readFileSync(abspath, 'utf8');
77+
78+
//our helper function that does a lot of heavy lifting
79+
processPattern(currentPattern, patternlab);
80+
}
81+
82+
function processPattern(pattern, patternlab, additionalData){
83+
//find how many partials there may be for the given pattern
84+
var foundPatternPartials = findPartials(currentPattern);
85+
86+
if(foundPatternPartials != null && foundPatternPartials.length > 0){
87+
88+
console.log(foundPatternPartials);
89+
90+
//determine if the template contains any pattern parameters. if so they must be immediately consumed
91+
var patternsConsumedWithParamaters = parameter_hunter.find_parameters(pattern, patternlab);
92+
93+
if(patternsConsumedWithParamaters === 0){
94+
//do something with the regular old partials
95+
}
96+
97+
} else{
98+
//we found no partials, so we are ready to render
99+
100+
}
101+
102+
//find pattern lineage
103+
lineage_hunter.find_lineage(currentPattern, patternlab);
104+
105+
//add as a partial in case this is referenced later. convert to syntax needed by existing patterns
106+
var sub = subdir.substring(subdir.indexOf('-') + 1);
107+
var folderIndex = sub.indexOf(path.sep);
108+
var cleanSub = sub.substring(0, folderIndex);
109+
110+
//add any templates found to an object of partials, so downstream templates may use them too
111+
//look for the full path on nested patterns, else expect it to be flat
112+
var partialname = '';
113+
if(cleanSub !== ''){
114+
partialname = cleanSub + '-' + currentPattern.patternName;
115+
} else{
116+
partialname = currentPattern.patternGroup + '-' + currentPattern.patternName;
117+
}
118+
patternlab.partials[partialname] = currentPattern.template;
119+
120+
//look for a pseudo pattern by checking if there is a file containing same name, with ~ in it, ending in .json
121+
pseudopattern_hunter.find_pseudopatterns(currentPattern, subdir, patternlab);
122+
123+
//add to patternlab object so we can look these up later.
124+
pattern_assembler.addPattern(currentPattern, patternlab);
20125
}
21126

22127
return {
128+
find_pattern_partials: function(pattern){
129+
return findPartials(pattern);
130+
},
23131
setPatternState: function(pattern, patternlab){
24132
setState(pattern, patternlab);
25133
},
26-
addPattern: function(pattern, patternLab){
27-
addPattern(pattern, patternLab);
134+
addPattern: function(pattern, patternlab){
135+
addPattern(pattern, patternlab);
136+
},
137+
renderPattern: function(template, data, partials){
138+
return renderPattern(template, data, partials);
139+
},
140+
process_pattern_file: function(file, patternlab){
141+
processPatternFile(file, patternlab);
142+
},
143+
process_pattern: function(pattern, patternlab, additionalData){
144+
processPattern(pattern, patternlab, additionalData);
28145
}
29146
};
30147

31148
};
32149

33150
module.exports = pattern_assembler;
34151

35-
}());
152+
}());

0 commit comments

Comments
 (0)