Skip to content

Commit 84db306

Browse files
Merge pull request #442 from pattern-lab/fix-patternlink-handling
Fix patternlink handling
2 parents b2c45c6 + 8812651 commit 84db306

File tree

7 files changed

+204
-110
lines changed

7 files changed

+204
-110
lines changed

core/lib/object_factory.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var extend = require('util')._extend;
66

77
// Pattern properties
88

9-
var Pattern = function (relPath, data) {
9+
var Pattern = function (relPath, data, patternlab) {
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.
1212
var pathObj = path.parse(path.normalize(relPath));
@@ -29,10 +29,6 @@ var Pattern = function (relPath, data) {
2929
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
3030
}, '').trim(); //this is the display name for the ui. strip numeric + hyphen prefixes
3131

32-
// calculated path from the root of the public directory to the generated html
33-
// file for this pattern
34-
this.patternLink = this.name + path.sep + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'
35-
3632
// the top-level pattern group this pattern belongs to. 'atoms'
3733
this.patternGroup = this.subdir.split(path.sep)[0].replace(/^\d*-/, '');
3834

@@ -48,6 +44,10 @@ var Pattern = function (relPath, data) {
4844
// the joined pattern group and subgroup directory
4945
this.flatPatternPath = this.subdir.replace(/[\/\\]/g, '-'); // '00-atoms-00-global'
5046

47+
// calculated path from the root of the public directory to the generated
48+
// (rendered!) html file for this pattern, to be shown in the iframe
49+
this.patternLink = patternlab ? this.getPatternLink(patternlab, 'rendered') : null;
50+
5151
// The canonical "key" by which this pattern is known. This is the callable
5252
// name of the pattern. UPDATE: this.key is now known as this.patternPartial
5353
this.patternPartial = this.patternGroup + '-' + this.patternBaseName;
@@ -84,6 +84,20 @@ Pattern.prototype = {
8484
}
8585
},
8686

87+
// calculated path from the root of the public directory to the generated html
88+
// file for this pattern.
89+
// Should look something like '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'
90+
getPatternLink: function (patternlab, suffixType) {
91+
// if no suffixType is provided, we default to rendered
92+
var suffixConfig = patternlab.config.outputFileSuffixes;
93+
var suffix = suffixType ? suffixConfig[suffixType] : suffixConfig.rendered;
94+
95+
if (suffixType === 'rawTemplate') {
96+
return this.name + path.sep + this.name + suffix + this.fileExtension;
97+
}
98+
return this.name + path.sep + this.name + suffix + '.html';
99+
},
100+
87101
// the finders all delegate to the PatternEngine, which also encapsulates all
88102
// appropriate regexes
89103
findPartials: function () {
@@ -111,16 +125,16 @@ Pattern.prototype = {
111125

112126
// factory: creates an empty Pattern for miscellaneous internal use, such as
113127
// by list_item_hunter
114-
Pattern.createEmpty = function (customProps) {
115-
var pattern = new Pattern('', null);
128+
Pattern.createEmpty = function (customProps, patternlab) {
129+
var pattern = new Pattern('', null, patternlab);
116130
return extend(pattern, customProps);
117131
};
118132

119133
// factory: creates an Pattern object on-demand from a hash; the hash accepts
120134
// parameters that replace the positional parameters that the Pattern
121135
// constructor takes.
122-
Pattern.create = function (relPath, data, customProps) {
123-
var newPattern = new Pattern(relPath || '', data || null);
136+
Pattern.create = function (relPath, data, customProps, patternlab) {
137+
var newPattern = new Pattern(relPath || '', data || null, patternlab);
124138
return extend(newPattern, customProps);
125139
};
126140

core/lib/pattern_assembler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ var pattern_assembler = function () {
9494
function addPattern(pattern, patternlab) {
9595

9696
//add the link to the global object
97-
patternlab.data.link[pattern.patternPartial] = '/patterns/' + pattern.patternLink.replace('.html', patternlab.config.outputFileSuffixes.rendered + '.html');
97+
patternlab.data.link[pattern.patternPartial] = '/patterns/' + pattern.patternLink;
9898

9999
//only push to array if the array doesn't contain this pattern
100100
var isNew = true;
@@ -213,7 +213,7 @@ var pattern_assembler = function () {
213213
if (proposedDirectoryStats.isDirectory()) {
214214
var subTypeMarkdownFileContents = fs.readFileSync(proposedDirectory + '.md', 'utf8');
215215
var subTypeMarkdown = markdown_parser.parse(subTypeMarkdownFileContents);
216-
var subTypePattern = new Pattern(relPath);
216+
var subTypePattern = new Pattern(relPath, null, patternlab);
217217
subTypePattern.patternSectionSubtype = true;
218218
subTypePattern.patternLink = subTypePattern.name + '/index.html';
219219
subTypePattern.patternDesc = subTypeMarkdown.markdown;
@@ -244,7 +244,7 @@ var pattern_assembler = function () {
244244
if (!patternEngines.isPatternFile(filename, patternlab)) { return null; }
245245

246246
//make a new Pattern Object
247-
var currentPattern = new Pattern(relPath);
247+
var currentPattern = new Pattern(relPath, null, patternlab);
248248

249249
//if file is named in the syntax for variants
250250
if (patternEngines.isPseudoPatternJSON(filename)) {

core/lib/patternlab.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* patternlab-node - v2.4.3 - 2016
33
*
44
* Brian Muenzenmeyer, Geoff Pursell, 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.
88
*
99
*/
1010

@@ -60,10 +60,10 @@ function processAllPatternsRecursive(pattern_assembler, patterns_dir, patternlab
6060
function checkConfiguration(patternlab) {
6161
//default the output suffixes if not present
6262
var outputFileSuffixes = {
63-
rendered: '',
63+
rendered: '.rendered',
6464
rawTemplate: '',
6565
markupOnly: '.markup-only'
66-
}
66+
};
6767

6868
if (!patternlab.config.outputFileSuffixes) {
6969
plutils.logOrange('Configuration Object "outputFileSuffixes" not found, and defaulted to the following:');
@@ -355,13 +355,13 @@ var patternlab_engine = function (config) {
355355

356356
//write the compiled template to the public patterns directory
357357
var patternPage = headHTML + pattern.patternPartialCode + footerHTML;
358-
fs.outputFileSync(paths.public.patterns + pattern.patternLink.replace('.html', patternlab.config.outputFileSuffixes.rendered + '.html'), patternPage);
358+
fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'rendered'), patternPage);
359359

360360
//write the mustache file too
361-
fs.outputFileSync(paths.public.patterns + pattern.patternLink.replace('.html', patternlab.config.outputFileSuffixes.rawTemplate + pattern.fileExtension), pattern.template);
361+
fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'rawTemplate'), pattern.template);
362362

363363
//write the encoded version too
364-
fs.outputFileSync(paths.public.patterns + pattern.patternLink.replace('.html', patternlab.config.outputFileSuffixes.markupOnly + '.html'), pattern.patternPartialCode);
364+
fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'markupOnly'), pattern.patternPartialCode);
365365

366366
return true;
367367
});

core/lib/ui_builder.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ var ui_builder = function () {
143143
engine: null,
144144
flatPatternPath: pattern.flatPatternPath,
145145
isDocPattern: true
146-
}
146+
},
147+
patternlab
147148
);
148149
return docPattern;
149150
}

test/lineage_hunter_tests.js

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ function createBasePatternLabObject() {
3030
}
3131
},
3232
outputFileSuffixes: {
33-
rendered: ''
34-
}
33+
rendered: '.rendered',
34+
rawTemplate: '',
35+
markupOnly: '.markup-only'
36+
},
37+
patternStateCascade: ["inprogress", "inreview", "complete"]
3538
};
3639
pl.data = {};
3740
pl.data.link = {};
3841
pl.config.debug = false;
3942
pl.patterns = [];
4043
pl.partials = {};
41-
pl.config.patternStateCascade = ["inprogress", "inreview", "complete"];
44+
4245
return pl;
4346
}
4447

@@ -57,7 +60,7 @@ exports['lineage hunter '] = {
5760

5861
var patternlab = {
5962
patterns: [
60-
{
63+
Pattern.createEmpty({
6164
"name": "00-atoms-03-images-00-logo",
6265
"subdir": "00-atoms\\03-images",
6366
"filename": "00-logo.mustache",
@@ -75,8 +78,8 @@ exports['lineage hunter '] = {
7578
"lineageIndex": [],
7679
"lineageR": [],
7780
"lineageRIndex": []
78-
},
79-
{
81+
}),
82+
Pattern.createEmpty({
8083
"name": "01-molecules-05-navigation-00-primary-nav",
8184
"subdir": "01-molecules\\05-navigation",
8285
"filename": "00-primary-nav.mustache",
@@ -94,8 +97,8 @@ exports['lineage hunter '] = {
9497
"lineageIndex": [],
9598
"lineageR": [],
9699
"lineageRIndex": []
97-
},
98-
{
100+
}),
101+
Pattern.createEmpty({
99102
"name": "01-molecules-04-forms-00-search",
100103
"subdir": "01-molecules\\04-forms",
101104
"filename": "00-search.mustache",
@@ -113,11 +116,13 @@ exports['lineage hunter '] = {
113116
"lineageIndex": [],
114117
"lineageR": [],
115118
"lineageRIndex": []
116-
}
119+
})
117120
],
118121
config: {
119122
outputFileSuffixes: {
120-
rendered: ''
123+
rendered: '.rendered',
124+
rawTemplate: '',
125+
markupOnly: '.markup-only'
121126
}
122127
}
123128
};
@@ -150,7 +155,14 @@ exports['lineage hunter '] = {
150155
"template": "<h1> {{message}} </h1>",
151156
"extendedTemplate": "<h1> {{message}} </h1>"
152157
})
153-
]
158+
],
159+
config: {
160+
outputFileSuffixes: {
161+
rendered: '.rendered',
162+
rawTemplate: '',
163+
markupOnly: '.markup-only'
164+
}
165+
}
154166
};
155167

156168
lineage_hunter.find_lineage(currentPattern, patternlab);
@@ -286,7 +298,7 @@ exports['lineage hunter '] = {
286298

287299
var patternlab = {
288300
patterns: [
289-
{
301+
Pattern.createEmpty({
290302
"name": "01-atoms-05-alerts-00-error",
291303
"subdir": "01-atoms\\05-alerts",
292304
"filename": "00-error.mustache",
@@ -304,8 +316,15 @@ exports['lineage hunter '] = {
304316
"lineageIndex": [],
305317
"lineageR": [],
306318
"lineageRIndex": []
319+
})
320+
],
321+
config: {
322+
outputFileSuffixes: {
323+
rendered: '.rendered',
324+
rawTemplate: '',
325+
markupOnly: '.markup-only'
307326
}
308-
]
327+
}
309328
};
310329

311330
var lineage_hunter = new lh();
@@ -361,7 +380,14 @@ exports['lineage hunter '] = {
361380
"lineageR": [],
362381
"lineageRIndex": []
363382
})
364-
]
383+
],
384+
config: {
385+
outputFileSuffixes: {
386+
rendered: '.rendered',
387+
rawTemplate: '',
388+
markupOnly: '.markup-only'
389+
}
390+
}
365391
};
366392

367393
var lineage_hunter = new lh();
@@ -415,7 +441,14 @@ exports['lineage hunter '] = {
415441
"lineageR": [],
416442
"lineageRIndex": []
417443
})
418-
]
444+
],
445+
config: {
446+
outputFileSuffixes: {
447+
rendered: '.rendered',
448+
rawTemplate: '',
449+
markupOnly: '.markup-only'
450+
}
451+
}
419452
};
420453

421454
var lineage_hunter = new lh();
@@ -469,7 +502,14 @@ exports['lineage hunter '] = {
469502
"lineageR": [],
470503
"lineageRIndex": []
471504
})
472-
]
505+
],
506+
config: {
507+
outputFileSuffixes: {
508+
rendered: '.rendered',
509+
rawTemplate: '',
510+
markupOnly: '.markup-only'
511+
}
512+
}
473513
};
474514

475515
var lineage_hunter = new lh();
@@ -490,7 +530,7 @@ exports['lineage hunter '] = {
490530
});
491531
var patternlab = {
492532
patterns: [
493-
{
533+
Pattern.createEmpty({
494534
"name": "01-atoms-05-alerts-00-error",
495535
"subdir": "01-atoms\\05-alerts",
496536
"filename": "00-error.mustache",
@@ -508,8 +548,15 @@ exports['lineage hunter '] = {
508548
"lineageIndex": [],
509549
"lineageR": [],
510550
"lineageRIndex": []
551+
})
552+
],
553+
config: {
554+
outputFileSuffixes: {
555+
rendered: '.rendered',
556+
rawTemplate: '',
557+
markupOnly: '.markup-only'
511558
}
512-
]
559+
}
513560
};
514561

515562
var lineage_hunter = new lh();

0 commit comments

Comments
 (0)