Skip to content

Commit c35e4ba

Browse files
committed
pattern groups no longer need to be prefixed with two integers and a hyphen
closes #388
1 parent b2242e5 commit c35e4ba

6 files changed

+52
-44
lines changed

core/lib/object_factory.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ var extend = require('util')._extend;
99
var Pattern = function (relPath, data) {
1010
// We expect relPath to be the path of the pattern template, relative to the
1111
// root of the pattern tree. Parse out the path parts and save the useful ones.
12-
var pathObj = path.parse(relPath);
13-
this.relPath = relPath; // '00-atoms/00-global/00-colors.mustache'
12+
var pathObj = path.parse(path.normalize(relPath));
13+
this.relPath = path.normalize(relPath); // '00-atoms/00-global/00-colors.mustache'
1414
this.fileName = pathObj.name; // '00-colors'
1515
this.subdir = pathObj.dir; // '00-atoms/00-global'
1616
this.fileExtension = pathObj.ext; // '.mustache'
@@ -31,10 +31,10 @@ var Pattern = function (relPath, data) {
3131

3232
// calculated path from the root of the public directory to the generated html
3333
// file for this pattern
34-
this.patternLink = this.name + '/' + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'
34+
this.patternLink = this.name + path.sep + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'
3535

3636
// the top-level pattern group this pattern belongs to. 'atoms'
37-
this.patternGroup = this.name.substring(this.name.indexOf('-') + 1, this.name.indexOf('-', 4) + 1 - this.name.indexOf('-') + 1);
37+
this.patternGroup = this.subdir.split(path.sep)[0].replace(/^\d*-/, '');
3838

3939
// the sub-group this pattern belongs to.
4040
this.patternSubGroup = path.basename(this.subdir).replace(/^\d*-/, ''); // 'global'

core/lib/ui_builder.js

+18-22
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ var eol = require('os').EOL;
1111

1212
// PRIVATE FUNCTIONS
1313

14-
function addToPatternPaths(patternlab, patternTypeName, pattern) {
15-
//this is messy, could use a refactor.
16-
if (!patternlab.patternPaths[patternTypeName]) {
17-
patternlab.patternPaths[patternTypeName] = {};
14+
function addToPatternPaths(patternlab, pattern) {
15+
if (!patternlab.patternPaths[pattern.patternGroup]) {
16+
patternlab.patternPaths[pattern.patternGroup] = {};
1817
}
19-
patternlab.patternPaths[patternTypeName][pattern.patternBaseName] = pattern.subdir.replace(/\\/g, '/') + "/" + pattern.fileName.replace('~', '-');
18+
patternlab.patternPaths[pattern.patternGroup][pattern.patternBaseName] = pattern.name;
2019
}
2120

2221
//todo: refactor this as a method on the pattern object itself once we merge dev with pattern-engines branch
@@ -91,9 +90,6 @@ function buildNavigation(patternlab) {
9190

9291
var pattern = patternlab.patterns[i];
9392

94-
//todo: check if this is already available
95-
var patternTypeName = pattern.name.replace(/\\/g, '-').split('-')[1];
96-
9793
//exclude any named defaultPattern from the navigation.
9894
//this is meant to be a homepage that is not navigable
9995
if (pattern.patternPartial === patternlab.config.defaultPattern) {
@@ -102,7 +98,7 @@ function buildNavigation(patternlab) {
10298
}
10399

104100
//add to patternPaths before continuing
105-
addToPatternPaths(patternlab, patternTypeName, pattern);
101+
addToPatternPaths(patternlab, pattern);
106102

107103
continue;
108104
}
@@ -134,17 +130,17 @@ function buildNavigation(patternlab) {
134130
patternSubTypeItem.patternPartial = pattern.patternPartial;
135131

136132
//check if the patternType already exists
137-
var patternTypeIndex = patternlab.patternTypeIndex.indexOf(patternTypeName);
133+
var patternTypeIndex = patternlab.patternTypeIndex.indexOf(pattern.patternGroup);
138134
if (patternTypeIndex === -1) {
139135
//add the patternType
140-
var patternType = new of.oPatternType(patternTypeName);
136+
var patternType = new of.oPatternType(pattern.patternGroup);
141137

142138
//add patternPath and viewAllPath
143-
patternlab.patternPaths[patternTypeName] = patternlab.patternPaths[patternTypeName] || {};
144-
patternlab.viewAllPaths[patternTypeName] = {};
139+
patternlab.patternPaths[pattern.patternGroup] = patternlab.patternPaths[pattern.patternGroup] || {};
140+
patternlab.viewAllPaths[pattern.patternGroup] = {};
145141

146142
//test whether the pattern structure is flat or not - usually due to a template or page
147-
flatPatternItem = patternSubTypeName === patternTypeName;
143+
flatPatternItem = patternSubTypeName === pattern.patternGroup;
148144

149145
//assume the patternSubType does not exist.
150146
patternSubType = new of.oPatternSubType(patternSubTypeName);
@@ -160,7 +156,7 @@ function buildNavigation(patternlab) {
160156
patternType.patternItems.push(patternSubTypeItem);
161157

162158
//add to patternPaths
163-
addToPatternPaths(patternlab, patternTypeName, pattern);
159+
addToPatternPaths(patternlab, pattern);
164160

165161
} else {
166162

@@ -170,21 +166,21 @@ function buildNavigation(patternlab) {
170166
patternSubType.patternSubtypeItemsIndex.push(patternSubTypeItemName);
171167

172168
//add to patternPaths
173-
addToPatternPaths(patternlab, patternTypeName, pattern);
169+
addToPatternPaths(patternlab, pattern);
174170

175171
//add the view all PatternSubTypeItem
176172
viewAllPatternSubTypeItem = new of.oPatternSubTypeItem("View All");
177173
viewAllPatternSubTypeItem.patternPath = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length) + "/index.html";
178174
viewAllPatternSubTypeItem.patternPartial = "viewall-" + pattern.patternGroup;
179175

180176
patternType.patternItems.push(viewAllPatternSubTypeItem);
181-
patternlab.viewAllPaths[patternTypeName].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length);
177+
patternlab.viewAllPaths[pattern.patternGroup].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length);
182178

183179
}
184180

185181
//add the patternType.
186182
patternlab.patternTypes.push(patternType);
187-
patternlab.patternTypeIndex.push(patternTypeName);
183+
patternlab.patternTypeIndex.push(pattern.patternGroup);
188184

189185
//done
190186

@@ -198,15 +194,15 @@ function buildNavigation(patternlab) {
198194
}
199195

200196
//test whether the pattern structure is flat or not - usually due to a template or page
201-
flatPatternItem = patternSubTypeName === patternTypeName;
197+
flatPatternItem = patternSubTypeName === pattern.patternGroup;
202198

203199
//if it is flat - we should not add the pattern to patternPaths
204200
if (flatPatternItem) {
205201
//add the patternSubType to patternItems
206202
patternType.patternItems.push(patternSubTypeItem);
207203

208204
//add to patternPaths
209-
addToPatternPaths(patternlab, patternTypeName, pattern);
205+
addToPatternPaths(patternlab, pattern);
210206

211207
} else {
212208

@@ -242,11 +238,11 @@ function buildNavigation(patternlab) {
242238
}
243239

244240
// just add to patternPaths
245-
addToPatternPaths(patternlab, patternTypeName, pattern);
241+
addToPatternPaths(patternlab, pattern);
246242
}
247243
}
248244

249-
patternlab.viewAllPaths[patternTypeName][pattern.patternSubGroup] = pattern.flatPatternPath;
245+
patternlab.viewAllPaths[pattern.patternGroup][pattern.patternSubGroup] = pattern.flatPatternPath;
250246
}
251247
return patternTypeIndex;
252248
}

test/lineage_hunter_tests.js

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ exports['lineage hunter '] = {
145145
]
146146
};
147147

148-
lineage_hunter.find_lineage(currentPattern, patternlab);
149148
lineage_hunter.find_lineage(currentPattern, patternlab);
150149

151150
test.equals(currentPattern.lineageIndex.length, 1);

test/list_item_hunter_tests.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,24 @@
149149

150150
'process_list_item_partials finds verbose partials and outputs repeated renders' : function(test){
151151
var pattern1 = createFakeListPattern({
152-
"template": "{{#listItems.one}}{{> 00-atoms/00-test/00-foo.mustache }}{{/listItems.one}}",
153-
"extendedTemplate" : "{{#listItems.one}}{{> 00-atoms/00-test/00-foo.mustache }}{{/listItems.one}}",
152+
"template": "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}",
153+
"extendedTemplate" : "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}",
154154
"key": "test-patternName1"
155155
});
156156

157157
var pattern2 = createFakeListPattern({
158-
"template": "{{#listItems.two}}{{> 00-atoms/00-test/00-bar.mustache }}{{/listItems.two}}",
159-
"extendedTemplate" : "{{#listItems.two}}{{> 00-atoms/00-test/00-bar.mustache }}{{/listItems.two}}",
158+
"template": "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}",
159+
"extendedTemplate" : "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}",
160160
"key": "test-patternName2"
161161
});
162162

163163
var patternlab = createFakePatternLab({
164164
"patterns": [
165-
Pattern.create('00-atoms/00-test/00-foo.mustache', null, {
165+
Pattern.create('00-test/00-foo.mustache', null, {
166166
"template": "{{ title }}",
167167
"extendedTemplate": "{{ title }}"
168168
}),
169-
Pattern.create('00-atoms/00-test/00-bar.mustache', null, {
169+
Pattern.create('00-test/00-bar.mustache', null, {
170170
"template": "{{ title }}",
171171
"extendedTemplate": "{{ title }}"
172172
})

test/object_factory_tests.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33

44
var of = require('../core/lib/object_factory');
55
var Pattern = require('../core/lib/object_factory').Pattern;
6+
var path = require('path');
67

78
exports['Pattern initialization'] = {
89
'test Pattern initializes correctly' : function (test) {
910
var p = new Pattern('00-atoms/00-global/00-colors.mustache', { d: 123});
10-
test.equals(p.relPath, '00-atoms/00-global/00-colors.mustache');
11+
test.equals(p.relPath, '00-atoms' + path.sep + '00-global' + path.sep + '00-colors.mustache');
1112
test.equals(p.name, '00-atoms-00-global-00-colors');
12-
test.equals(p.subdir, '00-atoms/00-global');
13+
test.equals(p.subdir, '00-atoms' + path.sep + '00-global');
1314
test.equals(p.fileName, '00-colors');
1415
test.equals(p.fileExtension, '.mustache');
1516
test.equals(p.jsonFileData.d, 123);
1617
test.equals(p.patternBaseName, 'colors');
1718
test.equals(p.patternName, 'Colors');
18-
test.equals(p.patternLink, '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html');
19+
test.equals(p.patternLink, '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.html');
1920
test.equals(p.patternGroup, 'atoms');
2021
test.equals(p.patternSubGroup, 'global');
2122
test.equals(p.flatPatternPath, '00-atoms-00-global');
@@ -31,15 +32,15 @@
3132
},
3233
'test Pattern with one-directory subdir works as expected' : function (test) {
3334
var p = new Pattern('00-atoms/00-colors.mustache', { d: 123});
34-
test.equals(p.relPath, '00-atoms/00-colors.mustache');
35+
test.equals(p.relPath, '00-atoms' + path.sep + '00-colors.mustache');
3536
test.equals(p.name, '00-atoms-00-colors');
3637
test.equals(p.subdir, '00-atoms');
3738
test.equals(p.fileName, '00-colors');
3839
test.equals(p.fileExtension, '.mustache');
3940
test.equals(p.jsonFileData.d, 123);
4041
test.equals(p.patternBaseName, 'colors');
4142
test.equals(p.patternName, 'Colors');
42-
test.equals(p.patternLink, '00-atoms-00-colors/00-atoms-00-colors.html');
43+
test.equals(p.patternLink, '00-atoms-00-colors' + path.sep + '00-atoms-00-colors.html');
4344
test.equals(p.patternGroup, 'atoms');
4445
test.equals(p.flatPatternPath, '00-atoms');
4546
test.equals(p.patternPartial, 'atoms-colors');
@@ -50,6 +51,18 @@
5051
test.equals(p.lineageRIndex.length, 0);
5152
test.done();
5253
},
54+
'test Pattern with no numbers in pattern group works as expected' : function (test) {
55+
var p = new Pattern('atoms/colors.mustache', { d: 123});
56+
test.equals(p.relPath, 'atoms' + path.sep + 'colors.mustache');
57+
test.equals(p.name, 'atoms-colors');
58+
test.equals(p.subdir, 'atoms');
59+
test.equals(p.fileName, 'colors');
60+
test.equals(p.patternLink, 'atoms-colors' + path.sep + 'atoms-colors.html');
61+
test.equals(p.patternGroup, 'atoms');
62+
test.equals(p.flatPatternPath, 'atoms');
63+
test.equals(p.patternPartial, 'atoms-colors');
64+
test.done();
65+
},
5366
'test Pattern capitalizes patternDisplayName correctly' : function(test){
5467
var p = new Pattern('00-atoms/00-global/00-colors-alt.mustache', { d: 123});
5568
test.equals(p.patternBaseName, 'colors-alt');

test/pattern_assembler_tests.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494

9595
//act
9696

97-
pattern_assembler.process_pattern_recursive('00-test/04-group.mustache', pl, {});
97+
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '04-group.mustache', pl, {});
9898

9999
//assert
100100
var expectedValue = '<div class="test_group"> <span class="test_base test_1"> {{message}} </span> <span class="test_base test_2"> {{message}} </span> <span class="test_base test_3"> {{message}} </span> <span class="test_base test_4"> {{message}} </span> </div>';
@@ -135,7 +135,7 @@
135135
pattern_assembler.addPattern(groupPattern, pl);
136136

137137
//act
138-
pattern_assembler.process_pattern_recursive('00-test/10-multiple-classes-numeric.mustache', pl, {});
138+
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '10-multiple-classes-numeric.mustache', pl, {});
139139

140140
//assert
141141
var expectedValue = '<div class="test_group"> <span class="test_base foo1"> {{message}} </span> <span class="test_base foo1 foo2"> {{message}} </span> <span class="test_base foo1 foo2"> bar </span> </div>';
@@ -174,7 +174,7 @@
174174
pattern_assembler.addPattern(mixedPattern, pl);
175175

176176
//act
177-
pattern_assembler.process_pattern_recursive('00-test/06-mixed.mustache', pl, {});
177+
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '06-mixed.mustache', pl, {});
178178

179179
//assert. here we expect {{styleModifier}} to be in the first group, since it was not replaced by anything. rendering with data will then remove this (correctly)
180180
var expectedValue = '<div class="test_group"> <span class="test_base {{styleModifier}}"> {{message}} </span> <span class="test_base test_2"> {{message}} </span> <span class="test_base test_3"> {{message}} </span> <span class="test_base test_4"> {{message}} </span> </div>';
@@ -213,7 +213,7 @@
213213
pattern_assembler.addPattern(bookendPattern, pl);
214214

215215
//act
216-
pattern_assembler.process_pattern_recursive('00-test/09-bookend.mustache', pl, {});
216+
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '09-bookend.mustache', pl, {});
217217

218218
//assert. here we expect {{styleModifier}} to be in the first and last group, since it was not replaced by anything. rendering with data will then remove this (correctly)
219219
var expectedValue = '<div class="test_group"> <span class="test_base {{styleModifier}}"> {{message}} </span> <span class="test_base test_2"> {{message}} </span> <span class="test_base test_3"> {{message}} </span> <span class="test_base {{styleModifier}}"> {{message}} </span> </div>';
@@ -255,7 +255,7 @@
255255
pattern_assembler.addPattern(mixedPattern, pl);
256256

257257
//act
258-
pattern_assembler.process_pattern_recursive('00-test/07-mixed-params.mustache', pl, {});
258+
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '07-mixed-params.mustache', pl, {});
259259

260260
//assert. here we expect {{styleModifier}} to be in the first span, since it was not replaced by anything. rendering with data will then remove this (correctly)
261261
var expectedValue = '<div class="test_group"> <span class="test_base {{styleModifier}}"> {{message}} </span> <span class="test_base test_2"> 2 </span> <span class="test_base test_3"> 3 </span> <span class="test_base test_4"> 4 </span> </div>';
@@ -296,7 +296,7 @@
296296
pattern_assembler.addPattern(bookendPattern, pl);
297297

298298
//act
299-
pattern_assembler.process_pattern_recursive('00-test/08-bookend-params.mustache', pl, {});
299+
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '08-bookend-params.mustache', pl, {});
300300

301301
//assert. here we expect {{styleModifier}} to be in the first and last span, since it was not replaced by anything. rendering with data will then remove this (correctly)
302302
var expectedValue = '<div class="test_group"> <span class="test_base {{styleModifier}}"> {{message}} </span> <span class="test_base test_2"> 2 </span> <span class="test_base test_3"> 3 </span> <span class="test_base {{styleModifier}}"> {{message}} </span> </div>';

0 commit comments

Comments
 (0)