From b448484418e7187dde9852351e4aef7395a648bb Mon Sep 17 00:00:00 2001 From: stevemartin Date: Mon, 11 Jul 2016 21:22:04 +0100 Subject: [PATCH 1/5] fix for issue #385 - support foo.md inside _annotations * read in *.md files ( not recursive ) * clobber annotations and write * satisfies existing tests --- core/lib/annotation_exporter.js | 42 ++++++++++++++++++--------------- package.json | 5 ++-- test/files/annotations.md | 6 ----- test/files/nav.md | 6 +++++ 4 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 test/files/nav.md diff --git a/core/lib/annotation_exporter.js b/core/lib/annotation_exporter.js index e51ff1106..50fbea5a8 100644 --- a/core/lib/annotation_exporter.js +++ b/core/lib/annotation_exporter.js @@ -1,6 +1,7 @@ "use strict"; var path = require('path'), + readDir = require('readdir'), fs = require('fs-extra'), JSON5 = require('json5'), _ = require('lodash'), @@ -42,32 +43,35 @@ var annotations_exporter = function (pl) { function parseAnnotationsMD() { var markdown_parser = new mp(); var annotations = []; - - //attempt to read the file - var annotationsMD = ''; - try { - annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, 'annotations.md'), 'utf8'); - } catch (ex) { - if (pl.config.debug) { - console.log('annotations.md file missing from ' + paths.source.annotations + '. This may be expected.'); + var mdFiles = readDir.readSync(paths.source.annotations, ['*.md']) + + mdFiles.forEach(function (file) { + var annotationsMD = ''; + try { + annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, file), 'utf8'); + } catch (ex) { + if (pl.config.debug) { + console.log('annotations.md file missing from ' + paths.source.annotations + '. This may be expected.'); + } + return []; } - return []; - } //take the annotation snippets and split them on our custom delimiter - var annotationsYAML = annotationsMD.split('~*~'); + var annotationsYAML = annotationsMD.split('~*~'); - for (var i = 0; i < annotationsYAML.length; i++) { - var annotation = {}; + for (var i = 0; i < annotationsYAML.length; i++) { + var annotation = {}; - var markdownObj = markdown_parser.parse(annotationsYAML[i]); + var markdownObj = markdown_parser.parse(annotationsYAML[i]); - annotation.el = markdownObj.el || markdownObj.selector; - annotation.title = markdownObj.title; - annotation.comment = markdownObj.markdown; + annotation.el = markdownObj.el || markdownObj.selector; + annotation.title = markdownObj.title; + annotation.comment = markdownObj.markdown; - annotations.push(annotation); - } + annotations.push(annotation); + } + return false; + }) return annotations; } diff --git a/package.json b/package.json index 53992f55d..20b3c621c 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "grunt": "~1.0.1", "grunt-contrib-concat": "^1.0.1", "grunt-contrib-nodeunit": "^1.0.0", - "grunt-eslint": "^18.0.0" + "grunt-eslint": "^18.0.0", + "readdir": "0.0.13" }, "keywords": [ "Pattern Lab", @@ -37,7 +38,7 @@ }, "contributors": [ { - "name" : "Geoff Pursell" + "name": "Geoff Pursell" } ], "license": "MIT", diff --git a/test/files/annotations.md b/test/files/annotations.md index 7e2ad972a..cf895f081 100644 --- a/test/files/annotations.md +++ b/test/files/annotations.md @@ -10,9 +10,3 @@ selector: .logo title: Logo --- The _logo image_ is an SVG file. -~*~ ---- -el: #nav -title : Navigation ---- -Navigation for adaptive web experiences can be tricky. Refer to [these repsonsive patterns](https://bradfrost.github.io/this-is-responsive/patterns.html#navigation) when evaluating solutions. diff --git a/test/files/nav.md b/test/files/nav.md new file mode 100644 index 000000000..00de05203 --- /dev/null +++ b/test/files/nav.md @@ -0,0 +1,6 @@ +--- +el: #nav +title : Navigation +--- +Navigation for adaptive web experiences can be tricky. Refer to [these repsonsive patterns](https://bradfrost.github.io/this-is-responsive/patterns.html#navigation) when evaluating solutions. + From d2c4311d375d4d122c202b7390c366d4207bda6b Mon Sep 17 00:00:00 2001 From: stevemartin Date: Tue, 12 Jul 2016 06:40:37 +0100 Subject: [PATCH 2/5] refactor markdown parser code * extract functions * add test case for zero files --- core/lib/annotation_exporter.js | 55 ++++++++++++++++--------------- test/annotation_exporter_tests.js | 17 ++++++++-- test/files/empty/.gitkeep | 0 3 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 test/files/empty/.gitkeep diff --git a/core/lib/annotation_exporter.js b/core/lib/annotation_exporter.js index 50fbea5a8..962315e47 100644 --- a/core/lib/annotation_exporter.js +++ b/core/lib/annotation_exporter.js @@ -37,41 +37,42 @@ var annotations_exporter = function (pl) { return oldAnnotationsJSON.comments; } - /* - Converts the annotations.md file yaml list into an array of annotations - */ - function parseAnnotationsMD() { - var markdown_parser = new mp(); - var annotations = []; - var mdFiles = readDir.readSync(paths.source.annotations, ['*.md']) + function buildAnnotationMD(annotationsYAML, markdown_parser) { + var annotation = {}; + var markdownObj = markdown_parser.parse(annotationsYAML); + + annotation.el = markdownObj.el || markdownObj.selector; + annotation.title = markdownObj.title; + annotation.comment = markdownObj.markdown; + return annotation; + } - mdFiles.forEach(function (file) { - var annotationsMD = ''; - try { - annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, file), 'utf8'); - } catch (ex) { - if (pl.config.debug) { - console.log('annotations.md file missing from ' + paths.source.annotations + '. This may be expected.'); - } - return []; - } + function parseMDFile(annotations, parser) { + var annotations = annotations; + var markdown_parser = parser; + + return function (file) { + var annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, file), 'utf8'); //take the annotation snippets and split them on our custom delimiter var annotationsYAML = annotationsMD.split('~*~'); - for (var i = 0; i < annotationsYAML.length; i++) { - var annotation = {}; - - var markdownObj = markdown_parser.parse(annotationsYAML[i]); - - annotation.el = markdownObj.el || markdownObj.selector; - annotation.title = markdownObj.title; - annotation.comment = markdownObj.markdown; - + var annotation = buildAnnotationMD(annotationsYAML[i], markdown_parser) annotations.push(annotation); } return false; - }) + } + } + + /* + Converts the *.md file yaml list into an array of annotations + */ + function parseAnnotationsMD() { + var markdown_parser = new mp(); + var annotations = []; + var mdFiles = readDir.readSync(paths.source.annotations, ['*.md']) + + mdFiles.forEach(parseMDFile(annotations, markdown_parser)); return annotations; } diff --git a/test/annotation_exporter_tests.js b/test/annotation_exporter_tests.js index 70bbfec06..49ae049c8 100644 --- a/test/annotation_exporter_tests.js +++ b/test/annotation_exporter_tests.js @@ -3,13 +3,14 @@ var eol = require('os').EOL; var Pattern = require('../core/lib/object_factory').Pattern; var extend = require('util')._extend; +var anPath = './test/files/'; -function createFakePatternLab(customProps) { +function createFakePatternLab(anPath, customProps) { var pl = { "config": { "paths": { "source": { - "annotations": './test/files/' + "annotations": anPath } } } @@ -18,7 +19,7 @@ function createFakePatternLab(customProps) { return extend(pl, customProps); } -var patternlab = createFakePatternLab(); +var patternlab = createFakePatternLab(anPath); var ae = require('../core/lib/annotation_exporter')(patternlab); exports['annotaton_exporter'] = { @@ -65,5 +66,15 @@ exports['annotaton_exporter'] = { test.done(); + }, + + 'when there are 0 annotation files' : function (test) { + var emptyAnPath = './test/files/empty/'; + var patternlab2 = createFakePatternLab(emptyAnPath); + var ae2 = require('../core/lib/annotation_exporter')(patternlab2); + + var annotations = ae2.gather(); + test.equals(annotations.length, 0); + test.done(); } }; diff --git a/test/files/empty/.gitkeep b/test/files/empty/.gitkeep new file mode 100644 index 000000000..e69de29bb From b5dd1d04ef1806a191c771f0b3648edb5ea07102 Mon Sep 17 00:00:00 2001 From: stevemartin Date: Wed, 13 Jul 2016 00:10:04 +0100 Subject: [PATCH 3/5] use glob for globbing :) --- core/lib/annotation_exporter.js | 8 ++++---- package.json | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/core/lib/annotation_exporter.js b/core/lib/annotation_exporter.js index 962315e47..dc13698dd 100644 --- a/core/lib/annotation_exporter.js +++ b/core/lib/annotation_exporter.js @@ -1,7 +1,7 @@ "use strict"; var path = require('path'), - readDir = require('readdir'), + glob = require('glob'), fs = require('fs-extra'), JSON5 = require('json5'), _ = require('lodash'), @@ -51,8 +51,8 @@ var annotations_exporter = function (pl) { var annotations = annotations; var markdown_parser = parser; - return function (file) { - var annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, file), 'utf8'); + return function (filePath) { + var annotationsMD = fs.readFileSync(path.resolve(filePath), 'utf8'); //take the annotation snippets and split them on our custom delimiter var annotationsYAML = annotationsMD.split('~*~'); @@ -70,7 +70,7 @@ var annotations_exporter = function (pl) { function parseAnnotationsMD() { var markdown_parser = new mp(); var annotations = []; - var mdFiles = readDir.readSync(paths.source.annotations, ['*.md']) + var mdFiles = glob.sync(paths.source.annotations + '/*.md') mdFiles.forEach(parseMDFile(annotations, markdown_parser)); return annotations; diff --git a/package.json b/package.json index 20b3c621c..8148364b7 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,7 @@ "grunt": "~1.0.1", "grunt-contrib-concat": "^1.0.1", "grunt-contrib-nodeunit": "^1.0.0", - "grunt-eslint": "^18.0.0", - "readdir": "0.0.13" + "grunt-eslint": "^18.0.0" }, "keywords": [ "Pattern Lab", From cd4e7b150cd3b3a207d31245871fbd9fb413299c Mon Sep 17 00:00:00 2001 From: Geoffrey Pursell Date: Fri, 15 Jul 2016 13:33:18 -0500 Subject: [PATCH 4/5] Pass the sorted styleguidePatterns data structure to buildViewAllPages() rather than overwriting patternlab's main patterns data structure with the pared down list of patterns. --- core/lib/ui_builder.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/core/lib/ui_builder.js b/core/lib/ui_builder.js index eb3c33170..6e17fcd35 100644 --- a/core/lib/ui_builder.js +++ b/core/lib/ui_builder.js @@ -299,15 +299,15 @@ function buildViewAllHTML(patternlab, patterns, patternPartial) { return viewAllHTML; } -function buildViewAllPages(mainPageHeadHtml, patternlab) { +function buildViewAllPages(mainPageHeadHtml, patternlab, styleguidePatterns) { var paths = patternlab.config.paths; var prevSubdir = ''; var prevGroup = ''; var i; - for (i = 0; i < patternlab.patterns.length; i++) { + for (i = 0; i < styleguidePatterns.length; i++) { - var pattern = patternlab.patterns[i]; + var pattern = styleguidePatterns[i]; // skip underscore-prefixed files if (isPatternExcluded(pattern)) { @@ -336,21 +336,21 @@ function buildViewAllPages(mainPageHeadHtml, patternlab) { var j; - for (j = 0; j < patternlab.patterns.length; j++) { + for (j = 0; j < styleguidePatterns.length; j++) { - if (patternlab.patterns[j].patternGroup === pattern.patternGroup) { + if (styleguidePatterns[j].patternGroup === pattern.patternGroup) { //again, skip any sibling patterns to the current one that may have underscores - if (isPatternExcluded(patternlab.patterns[j])) { + if (isPatternExcluded(styleguidePatterns[j])) { if (patternlab.config.debug) { - console.log('Omitting ' + patternlab.patterns[j].patternPartial + " from view all sibling rendering."); + console.log('Omitting ' + styleguidePatterns[j].patternPartial + " from view all sibling rendering."); } continue; } //this is meant to be a homepage that is not present anywhere else - if (patternlab.patterns[j].patternPartial === patternlab.config.defaultPattern) { + if (styleguidePatterns[j].patternPartial === patternlab.config.defaultPattern) { if (patternlab.config.debug) { console.log('Omitting ' + pattern.patternPartial + ' from view all sibling rendering because it is defined as a defaultPattern'); } @@ -358,7 +358,7 @@ function buildViewAllPages(mainPageHeadHtml, patternlab) { } - viewAllPatterns.push(patternlab.patterns[j]); + viewAllPatterns.push(styleguidePatterns[j]); } } @@ -379,26 +379,26 @@ function buildViewAllPages(mainPageHeadHtml, patternlab) { viewAllPatterns = []; patternPartial = "viewall-" + pattern.patternGroup + "-" + pattern.patternSubGroup; - for (j = 0; j < patternlab.patterns.length; j++) { + for (j = 0; j < styleguidePatterns.length; j++) { - if (patternlab.patterns[j].subdir === pattern.subdir) { + if (styleguidePatterns[j].subdir === pattern.subdir) { //again, skip any sibling patterns to the current one that may have underscores - if (isPatternExcluded(patternlab.patterns[j])) { + if (isPatternExcluded(styleguidePatterns[j])) { if (patternlab.config.debug) { - console.log('Omitting ' + patternlab.patterns[j].patternPartial + " from view all sibling rendering."); + console.log('Omitting ' + styleguidePatterns[j].patternPartial + " from view all sibling rendering."); } continue; } //this is meant to be a homepage that is not present anywhere else - if (patternlab.patterns[j].patternPartial === patternlab.config.defaultPattern) { + if (styleguidePatterns[j].patternPartial === patternlab.config.defaultPattern) { if (patternlab.config.debug) { console.log('Omitting ' + pattern.patternPartial + ' from view all sibling rendering because it is defined as a defaultPattern'); } continue; } - viewAllPatterns.push(patternlab.patterns[j]); + viewAllPatterns.push(styleguidePatterns[j]); } } @@ -446,7 +446,7 @@ function buildFrontEnd(patternlab) { styleguidePatterns = assembleStyleguidePatterns(patternlab); //sort all patterns explicitly. - patternlab.patterns = sortPatterns(styleguidePatterns); + styleguidePatterns = sortPatterns(styleguidePatterns); //set the pattern-specific header by compiling the general-header with data, and then adding it to the meta header var headerPartial = pattern_assembler.renderPattern(patternlab.header, { From b2242e5dfa070762b166a36bd7189b0459db6369 Mon Sep 17 00:00:00 2001 From: Brian Muenzenmeyer Date: Mon, 18 Jul 2016 15:35:46 -0500 Subject: [PATCH 5/5] prepping for release --- core/lib/patternlab.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index 809646ee0..d0518ab77 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -1,5 +1,5 @@ /* - * patternlab-node - v2.2.0 - 2016 + * patternlab-node - v2.2.1 - 2016 * * Brian Muenzenmeyer, Geoff Pursell, and the web community. * Licensed under the MIT license. diff --git a/package.json b/package.json index bf38bcd41..c48a2821d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "patternlab-node", "description": "Pattern Lab is a collection of tools to help you create atomic design systems. This is the node command line interface (CLI).", - "version": "2.2.0", + "version": "2.2.1", "main": "./core/lib/patternlab.js", "dependencies": { "diveSync": "^0.3.0",