Skip to content

Commit 777dd1c

Browse files
Merge pull request #509 from pattern-lab/fix-hidden-pattern-references
Fix hidden pattern references
2 parents 5b7dcf6 + a7aa23b commit 777dd1c

File tree

13 files changed

+125
-13
lines changed

13 files changed

+125
-13
lines changed

Gruntfile.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ module.exports = function (grunt) {
1616
dest: './core/lib/patternlab.js'
1717
}
1818
},
19-
nodeunit: {
20-
all: ['test/*_tests.js']
21-
},
2219
tape: {
2320
options: {
24-
pretty: true,
21+
pretty: false,
2522
output: 'console'
2623
},
2724
files: ['test/*_tests.js']
@@ -37,13 +34,12 @@ module.exports = function (grunt) {
3734
// load all grunt tasks
3835
grunt.loadNpmTasks('grunt-contrib-concat');
3936
grunt.loadNpmTasks('grunt-eslint');
40-
grunt.loadNpmTasks('grunt-contrib-nodeunit');
4137
grunt.loadNpmTasks('grunt-tape');
4238

4339
//travis CI task
44-
grunt.registerTask('travis', ['nodeunit', 'tape', 'eslint']);
40+
grunt.registerTask('travis', ['tape', 'eslint']);
4541

4642
//to be run prior to releasing a version
47-
grunt.registerTask('build', ['nodeunit', 'tape', 'eslint', 'concat']);
43+
grunt.registerTask('build', ['tape', 'eslint', 'concat']);
4844

4945
};

core/lib/object_factory.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
var patternEngines = require('./pattern_engines');
44
var path = require('path');
55
var extend = require('util')._extend;
6+
// patternPrefixMatcher is intended to match the leading maybe-underscore,
7+
// zero or more digits, and maybe-dash at the beginning of a pattern file name we can hack them
8+
// off and get at the good part.
9+
var patternPrefixMatcher = /^_?(\d+-)?/;
610

711
// Pattern properties
812

@@ -22,21 +26,21 @@ var Pattern = function (relPath, data, patternlab) {
2226
this.jsonFileData = data || {};
2327

2428
// strip leading "00-" from the file name and flip tildes to dashes
25-
this.patternBaseName = this.fileName.replace(/^\d*\-/, '').replace('~', '-'); // 'colors'
29+
this.patternBaseName = this.fileName.replace(patternPrefixMatcher, '').replace('~', '-'); // 'colors'
2630

2731
// Fancy name. No idea how this works. 'Colors'
2832
this.patternName = this.patternBaseName.split('-').reduce(function (val, working) {
2933
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
3034
}, '').trim(); //this is the display name for the ui. strip numeric + hyphen prefixes
3135

3236
// the top-level pattern group this pattern belongs to. 'atoms'
33-
this.patternGroup = this.subdir.split(path.sep)[0].replace(/^\d*-/, '');
37+
this.patternGroup = this.subdir.split(path.sep)[0].replace(patternPrefixMatcher, '');
3438

3539
//00-atoms if needed
3640
this.patternType = this.subdir.split(path.sep)[0];
3741

3842
// the sub-group this pattern belongs to.
39-
this.patternSubGroup = path.basename(this.subdir).replace(/^\d*-/, ''); // 'global'
43+
this.patternSubGroup = path.basename(this.subdir).replace(patternPrefixMatcher, ''); // 'global'
4044

4145
//00-colors if needed
4246
this.patternSubType = path.basename(this.subdir);
@@ -52,6 +56,10 @@ var Pattern = function (relPath, data, patternlab) {
5256
// name of the pattern. UPDATE: this.key is now known as this.patternPartial
5357
this.patternPartial = this.patternGroup + '-' + this.patternBaseName;
5458

59+
// Let's calculate the verbose name ahead of time! We don't use path.sep here
60+
// on purpose. This isn't a file name!
61+
this.verbosePartial = this.subdir + '/' + this.fileName;
62+
5563
this.isPattern = true;
5664
this.isFlatPattern = this.patternGroup === this.patternSubGroup;
5765
this.patternState = '';

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"devDependencies": {
1919
"grunt": "~1.0.1",
2020
"grunt-contrib-concat": "^1.0.1",
21-
"grunt-contrib-nodeunit": "^1.0.0",
2221
"grunt-eslint": "^18.0.0",
2322
"grunt-tape": "^0.1.0",
2423
"tap": "^7.1.2"

test/engine_handlebars_tests.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (!engineLoader.handlebars) {
1515
test.end()
1616
})
1717
return
18-
};
18+
}
1919

2020
// fake pattern lab constructor:
2121
// sets up a fake patternlab object, which is needed by the pattern processing
@@ -70,6 +70,7 @@ function testFindPartials(test, partialTests) {
7070
test.end();
7171
}
7272

73+
7374
tap.test('hello world handlebars pattern renders', function (test) {
7475
test.plan(1);
7576

@@ -200,3 +201,24 @@ tap.test('find_pattern_partials finds handlebars block partials', function (test
200201
'{{#> myPartial }}'
201202
]);
202203
});
204+
205+
tap.test('hidden handlebars patterns can be called by their nice names', function (test) {
206+
const util = require('./util/test_utils.js');
207+
208+
//arrange
209+
const testPatternsPath = path.resolve(__dirname, 'files', '_handlebars-test-patterns');
210+
const pl = util.fakePatternLab(testPatternsPath);
211+
var pattern_assembler = new pa();
212+
213+
var hiddenPatternPath = path.join('00-atoms', '00-global', '_00-hidden.hbs');
214+
var hiddenPattern = pattern_assembler.process_pattern_iterative(hiddenPatternPath, pl);
215+
pattern_assembler.process_pattern_recursive(hiddenPatternPath, pl);
216+
217+
var testPatternPath = path.join('00-molecules', '00-global', '00-hidden-pattern-tester.hbs');
218+
var testPattern = pattern_assembler.process_pattern_iterative(testPatternPath, pl);
219+
pattern_assembler.process_pattern_recursive(testPatternPath, pl);
220+
221+
//act
222+
test.equals(util.sanitized(testPattern.render()), util.sanitized('Here\'s the hidden atom: [I\'m the hidden atom\n]\n'));
223+
test.end();
224+
});

test/engine_underscore_tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,23 @@ tap.test('findPartial return the ID of the partial, given a whole partial call',
100100

101101
});
102102

103+
tap.test('hidden underscore patterns can be called by their nice names', function(test){
104+
const util = require('./util/test_utils.js');
105+
106+
//arrange
107+
const testPatternsPath = path.resolve(__dirname, 'files', '_underscore-test-patterns');
108+
const pl = util.fakePatternLab(testPatternsPath);
109+
var pattern_assembler = new pa();
110+
111+
var hiddenPatternPath = path.join('00-atoms', '00-global', '_00-hidden.html');
112+
var hiddenPattern = pattern_assembler.process_pattern_iterative(hiddenPatternPath, pl);
113+
pattern_assembler.process_pattern_recursive(hiddenPatternPath, pl);
114+
115+
var testPatternPath = path.join('00-molecules', '00-global', '00-hidden-pattern-tester.html');
116+
var testPattern = pattern_assembler.process_pattern_iterative(testPatternPath, pl);
117+
pattern_assembler.process_pattern_recursive(testPatternPath, pl);
118+
119+
//act
120+
test.equals(util.sanitized(testPattern.render()), util.sanitized('Here\'s the hidden atom: [I\'m the hidden atom\n]\n'));
121+
test.end();
122+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I'm the hidden atom
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Here's the hidden atom: [{{> atoms-hidden}}]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello there!
2+
Here's the hidden atom: [{{> test-hidden-pattern}}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is the hidden atom
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I'm the hidden atom

0 commit comments

Comments
 (0)