From c35e4babbeb10ab9ad758dc13014c4cf0d4f76e1 Mon Sep 17 00:00:00 2001 From: BRIAN MUENZENMEYER Date: Wed, 20 Jul 2016 00:12:51 -0500 Subject: [PATCH] pattern groups no longer need to be prefixed with two integers and a hyphen closes #388 --- core/lib/object_factory.js | 8 +++---- core/lib/ui_builder.js | 40 +++++++++++++++------------------ test/lineage_hunter_tests.js | 1 - test/list_item_hunter_tests.js | 12 +++++----- test/object_factory_tests.js | 23 ++++++++++++++----- test/pattern_assembler_tests.js | 12 +++++----- 6 files changed, 52 insertions(+), 44 deletions(-) diff --git a/core/lib/object_factory.js b/core/lib/object_factory.js index c1aa23fe6..74b62f39e 100644 --- a/core/lib/object_factory.js +++ b/core/lib/object_factory.js @@ -9,8 +9,8 @@ var extend = require('util')._extend; var Pattern = function (relPath, data) { // We expect relPath to be the path of the pattern template, relative to the // root of the pattern tree. Parse out the path parts and save the useful ones. - var pathObj = path.parse(relPath); - this.relPath = relPath; // '00-atoms/00-global/00-colors.mustache' + var pathObj = path.parse(path.normalize(relPath)); + this.relPath = path.normalize(relPath); // '00-atoms/00-global/00-colors.mustache' this.fileName = pathObj.name; // '00-colors' this.subdir = pathObj.dir; // '00-atoms/00-global' this.fileExtension = pathObj.ext; // '.mustache' @@ -31,10 +31,10 @@ var Pattern = function (relPath, data) { // calculated path from the root of the public directory to the generated html // file for this pattern - this.patternLink = this.name + '/' + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html' + this.patternLink = this.name + path.sep + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html' // the top-level pattern group this pattern belongs to. 'atoms' - this.patternGroup = this.name.substring(this.name.indexOf('-') + 1, this.name.indexOf('-', 4) + 1 - this.name.indexOf('-') + 1); + this.patternGroup = this.subdir.split(path.sep)[0].replace(/^\d*-/, ''); // the sub-group this pattern belongs to. this.patternSubGroup = path.basename(this.subdir).replace(/^\d*-/, ''); // 'global' diff --git a/core/lib/ui_builder.js b/core/lib/ui_builder.js index 6e17fcd35..e8845bc6c 100644 --- a/core/lib/ui_builder.js +++ b/core/lib/ui_builder.js @@ -11,12 +11,11 @@ var eol = require('os').EOL; // PRIVATE FUNCTIONS -function addToPatternPaths(patternlab, patternTypeName, pattern) { - //this is messy, could use a refactor. - if (!patternlab.patternPaths[patternTypeName]) { - patternlab.patternPaths[patternTypeName] = {}; +function addToPatternPaths(patternlab, pattern) { + if (!patternlab.patternPaths[pattern.patternGroup]) { + patternlab.patternPaths[pattern.patternGroup] = {}; } - patternlab.patternPaths[patternTypeName][pattern.patternBaseName] = pattern.subdir.replace(/\\/g, '/') + "/" + pattern.fileName.replace('~', '-'); + patternlab.patternPaths[pattern.patternGroup][pattern.patternBaseName] = pattern.name; } //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) { var pattern = patternlab.patterns[i]; - //todo: check if this is already available - var patternTypeName = pattern.name.replace(/\\/g, '-').split('-')[1]; - //exclude any named defaultPattern from the navigation. //this is meant to be a homepage that is not navigable if (pattern.patternPartial === patternlab.config.defaultPattern) { @@ -102,7 +98,7 @@ function buildNavigation(patternlab) { } //add to patternPaths before continuing - addToPatternPaths(patternlab, patternTypeName, pattern); + addToPatternPaths(patternlab, pattern); continue; } @@ -134,17 +130,17 @@ function buildNavigation(patternlab) { patternSubTypeItem.patternPartial = pattern.patternPartial; //check if the patternType already exists - var patternTypeIndex = patternlab.patternTypeIndex.indexOf(patternTypeName); + var patternTypeIndex = patternlab.patternTypeIndex.indexOf(pattern.patternGroup); if (patternTypeIndex === -1) { //add the patternType - var patternType = new of.oPatternType(patternTypeName); + var patternType = new of.oPatternType(pattern.patternGroup); //add patternPath and viewAllPath - patternlab.patternPaths[patternTypeName] = patternlab.patternPaths[patternTypeName] || {}; - patternlab.viewAllPaths[patternTypeName] = {}; + patternlab.patternPaths[pattern.patternGroup] = patternlab.patternPaths[pattern.patternGroup] || {}; + patternlab.viewAllPaths[pattern.patternGroup] = {}; //test whether the pattern structure is flat or not - usually due to a template or page - flatPatternItem = patternSubTypeName === patternTypeName; + flatPatternItem = patternSubTypeName === pattern.patternGroup; //assume the patternSubType does not exist. patternSubType = new of.oPatternSubType(patternSubTypeName); @@ -160,7 +156,7 @@ function buildNavigation(patternlab) { patternType.patternItems.push(patternSubTypeItem); //add to patternPaths - addToPatternPaths(patternlab, patternTypeName, pattern); + addToPatternPaths(patternlab, pattern); } else { @@ -170,7 +166,7 @@ function buildNavigation(patternlab) { patternSubType.patternSubtypeItemsIndex.push(patternSubTypeItemName); //add to patternPaths - addToPatternPaths(patternlab, patternTypeName, pattern); + addToPatternPaths(patternlab, pattern); //add the view all PatternSubTypeItem viewAllPatternSubTypeItem = new of.oPatternSubTypeItem("View All"); @@ -178,13 +174,13 @@ function buildNavigation(patternlab) { viewAllPatternSubTypeItem.patternPartial = "viewall-" + pattern.patternGroup; patternType.patternItems.push(viewAllPatternSubTypeItem); - patternlab.viewAllPaths[patternTypeName].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length); + patternlab.viewAllPaths[pattern.patternGroup].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length); } //add the patternType. patternlab.patternTypes.push(patternType); - patternlab.patternTypeIndex.push(patternTypeName); + patternlab.patternTypeIndex.push(pattern.patternGroup); //done @@ -198,7 +194,7 @@ function buildNavigation(patternlab) { } //test whether the pattern structure is flat or not - usually due to a template or page - flatPatternItem = patternSubTypeName === patternTypeName; + flatPatternItem = patternSubTypeName === pattern.patternGroup; //if it is flat - we should not add the pattern to patternPaths if (flatPatternItem) { @@ -206,7 +202,7 @@ function buildNavigation(patternlab) { patternType.patternItems.push(patternSubTypeItem); //add to patternPaths - addToPatternPaths(patternlab, patternTypeName, pattern); + addToPatternPaths(patternlab, pattern); } else { @@ -242,11 +238,11 @@ function buildNavigation(patternlab) { } // just add to patternPaths - addToPatternPaths(patternlab, patternTypeName, pattern); + addToPatternPaths(patternlab, pattern); } } - patternlab.viewAllPaths[patternTypeName][pattern.patternSubGroup] = pattern.flatPatternPath; + patternlab.viewAllPaths[pattern.patternGroup][pattern.patternSubGroup] = pattern.flatPatternPath; } return patternTypeIndex; } diff --git a/test/lineage_hunter_tests.js b/test/lineage_hunter_tests.js index c1e978859..f689d049b 100644 --- a/test/lineage_hunter_tests.js +++ b/test/lineage_hunter_tests.js @@ -145,7 +145,6 @@ exports['lineage hunter '] = { ] }; - lineage_hunter.find_lineage(currentPattern, patternlab); lineage_hunter.find_lineage(currentPattern, patternlab); test.equals(currentPattern.lineageIndex.length, 1); diff --git a/test/list_item_hunter_tests.js b/test/list_item_hunter_tests.js index 7fe3cea56..2ab591d6f 100644 --- a/test/list_item_hunter_tests.js +++ b/test/list_item_hunter_tests.js @@ -149,24 +149,24 @@ 'process_list_item_partials finds verbose partials and outputs repeated renders' : function(test){ var pattern1 = createFakeListPattern({ - "template": "{{#listItems.one}}{{> 00-atoms/00-test/00-foo.mustache }}{{/listItems.one}}", - "extendedTemplate" : "{{#listItems.one}}{{> 00-atoms/00-test/00-foo.mustache }}{{/listItems.one}}", + "template": "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}", + "extendedTemplate" : "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}", "key": "test-patternName1" }); var pattern2 = createFakeListPattern({ - "template": "{{#listItems.two}}{{> 00-atoms/00-test/00-bar.mustache }}{{/listItems.two}}", - "extendedTemplate" : "{{#listItems.two}}{{> 00-atoms/00-test/00-bar.mustache }}{{/listItems.two}}", + "template": "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}", + "extendedTemplate" : "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}", "key": "test-patternName2" }); var patternlab = createFakePatternLab({ "patterns": [ - Pattern.create('00-atoms/00-test/00-foo.mustache', null, { + Pattern.create('00-test/00-foo.mustache', null, { "template": "{{ title }}", "extendedTemplate": "{{ title }}" }), - Pattern.create('00-atoms/00-test/00-bar.mustache', null, { + Pattern.create('00-test/00-bar.mustache', null, { "template": "{{ title }}", "extendedTemplate": "{{ title }}" }) diff --git a/test/object_factory_tests.js b/test/object_factory_tests.js index 685f74fbd..cec3172ae 100644 --- a/test/object_factory_tests.js +++ b/test/object_factory_tests.js @@ -3,19 +3,20 @@ var of = require('../core/lib/object_factory'); var Pattern = require('../core/lib/object_factory').Pattern; + var path = require('path'); exports['Pattern initialization'] = { 'test Pattern initializes correctly' : function (test) { var p = new Pattern('00-atoms/00-global/00-colors.mustache', { d: 123}); - test.equals(p.relPath, '00-atoms/00-global/00-colors.mustache'); + test.equals(p.relPath, '00-atoms' + path.sep + '00-global' + path.sep + '00-colors.mustache'); test.equals(p.name, '00-atoms-00-global-00-colors'); - test.equals(p.subdir, '00-atoms/00-global'); + test.equals(p.subdir, '00-atoms' + path.sep + '00-global'); test.equals(p.fileName, '00-colors'); test.equals(p.fileExtension, '.mustache'); test.equals(p.jsonFileData.d, 123); test.equals(p.patternBaseName, 'colors'); test.equals(p.patternName, 'Colors'); - test.equals(p.patternLink, '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'); + test.equals(p.patternLink, '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.html'); test.equals(p.patternGroup, 'atoms'); test.equals(p.patternSubGroup, 'global'); test.equals(p.flatPatternPath, '00-atoms-00-global'); @@ -31,7 +32,7 @@ }, 'test Pattern with one-directory subdir works as expected' : function (test) { var p = new Pattern('00-atoms/00-colors.mustache', { d: 123}); - test.equals(p.relPath, '00-atoms/00-colors.mustache'); + test.equals(p.relPath, '00-atoms' + path.sep + '00-colors.mustache'); test.equals(p.name, '00-atoms-00-colors'); test.equals(p.subdir, '00-atoms'); test.equals(p.fileName, '00-colors'); @@ -39,7 +40,7 @@ test.equals(p.jsonFileData.d, 123); test.equals(p.patternBaseName, 'colors'); test.equals(p.patternName, 'Colors'); - test.equals(p.patternLink, '00-atoms-00-colors/00-atoms-00-colors.html'); + test.equals(p.patternLink, '00-atoms-00-colors' + path.sep + '00-atoms-00-colors.html'); test.equals(p.patternGroup, 'atoms'); test.equals(p.flatPatternPath, '00-atoms'); test.equals(p.patternPartial, 'atoms-colors'); @@ -50,6 +51,18 @@ test.equals(p.lineageRIndex.length, 0); test.done(); }, + 'test Pattern with no numbers in pattern group works as expected' : function (test) { + var p = new Pattern('atoms/colors.mustache', { d: 123}); + test.equals(p.relPath, 'atoms' + path.sep + 'colors.mustache'); + test.equals(p.name, 'atoms-colors'); + test.equals(p.subdir, 'atoms'); + test.equals(p.fileName, 'colors'); + test.equals(p.patternLink, 'atoms-colors' + path.sep + 'atoms-colors.html'); + test.equals(p.patternGroup, 'atoms'); + test.equals(p.flatPatternPath, 'atoms'); + test.equals(p.patternPartial, 'atoms-colors'); + test.done(); + }, 'test Pattern capitalizes patternDisplayName correctly' : function(test){ var p = new Pattern('00-atoms/00-global/00-colors-alt.mustache', { d: 123}); test.equals(p.patternBaseName, 'colors-alt'); diff --git a/test/pattern_assembler_tests.js b/test/pattern_assembler_tests.js index 002ef35aa..b0fa5e4a0 100644 --- a/test/pattern_assembler_tests.js +++ b/test/pattern_assembler_tests.js @@ -94,7 +94,7 @@ //act - pattern_assembler.process_pattern_recursive('00-test/04-group.mustache', pl, {}); + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '04-group.mustache', pl, {}); //assert var expectedValue = '
{{message}} {{message}} {{message}} {{message}}
'; @@ -135,7 +135,7 @@ pattern_assembler.addPattern(groupPattern, pl); //act - pattern_assembler.process_pattern_recursive('00-test/10-multiple-classes-numeric.mustache', pl, {}); + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '10-multiple-classes-numeric.mustache', pl, {}); //assert var expectedValue = '
{{message}} {{message}} bar
'; @@ -174,7 +174,7 @@ pattern_assembler.addPattern(mixedPattern, pl); //act - pattern_assembler.process_pattern_recursive('00-test/06-mixed.mustache', pl, {}); + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '06-mixed.mustache', pl, {}); //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) var expectedValue = '
{{message}} {{message}} {{message}} {{message}}
'; @@ -213,7 +213,7 @@ pattern_assembler.addPattern(bookendPattern, pl); //act - pattern_assembler.process_pattern_recursive('00-test/09-bookend.mustache', pl, {}); + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '09-bookend.mustache', pl, {}); //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) var expectedValue = '
{{message}} {{message}} {{message}} {{message}}
'; @@ -255,7 +255,7 @@ pattern_assembler.addPattern(mixedPattern, pl); //act - pattern_assembler.process_pattern_recursive('00-test/07-mixed-params.mustache', pl, {}); + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '07-mixed-params.mustache', pl, {}); //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) var expectedValue = '
{{message}} 2 3 4
'; @@ -296,7 +296,7 @@ pattern_assembler.addPattern(bookendPattern, pl); //act - pattern_assembler.process_pattern_recursive('00-test/08-bookend-params.mustache', pl, {}); + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '08-bookend-params.mustache', pl, {}); //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) var expectedValue = '
{{message}} 2 3 {{message}}
';