Skip to content

Commit

Permalink
Merge pull request #380 from pattern-lab/dev
Browse files Browse the repository at this point in the history
Pattern Lab Node 2.1.0
  • Loading branch information
Brian Muenzenmeyer authored Jul 4, 2016
2 parents 45643c9 + 0860540 commit 781e2d5
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 329 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[![Build Status](https://travis-ci.org/pattern-lab/patternlab-node.png?branch=master)](https://travis-ci.org/pattern-lab/patternlab-node) [![Join the chat at Gitter](https://badges.gitter.im/pattern-lab/node.svg)](https://gitter.im/pattern-lab/node)
[![Build Status](https://travis-ci.org/pattern-lab/patternlab-node.png?branch=master)](https://travis-ci.org/pattern-lab/patternlab-node) ![current release](https://img.shields.io/github/release/pattern-lab/patternlab-node.svg?maxAge=2592000) ![license](https://img.shields.io/github/license/pattern-lab/patternlab-node.svg?maxAge=2592000) [![Join the chat at Gitter](https://badges.gitter.im/pattern-lab/node.svg)](https://gitter.im/pattern-lab/node)

# Pattern Lab Node Core

This repository contains the core functionality for Pattern Lab Node. Pattern Lab Core is designed to be included as a dependency within [Node Editions](https://github.com/pattern-lab?utf8=%E2%9C%93&query=edition-node).
If this looks **REALLY DIFFERENT** from what you expected, check out the [ChangeLog](https://github.com/pattern-lab/patternlab-node/wiki/ChangeLog).

* [Pattern Lab/Node: Gulp Edition](https://github.com/pattern-lab/edition-node-gulp) contains info how to get started within a Gulp task running environment.
* [Pattern Lab/Node: Grunt Node Edition](https://github.com/pattern-lab/edition-node-grunt) contains info how to get started within a Grunt task running environment.
* [Pattern Lab/Node: Grunt Edition](https://github.com/pattern-lab/edition-node-grunt) contains info how to get started within a Grunt task running environment.

## Core Team

Expand Down
59 changes: 18 additions & 41 deletions core/lib/annotation_exporter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"use strict";

var path = require('path'),
fs = require('fs-extra'),
JSON5 = require('json5'),
_ = require('lodash'),
mp = require('./markdown_parser');

var annotations_exporter = function (pl) {
var path = require('path'),
fs = require('fs-extra'),
JSON5 = require('json5'),
_ = require('lodash'),
md = require('markdown-it')(),
paths = pl.config.paths;

var paths = pl.config.paths;

/*
Returns the array of comments that used to be wrapped in raw JS.
Expand Down Expand Up @@ -38,6 +40,7 @@ var annotations_exporter = function (pl) {
Converts the annotations.md file yaml list into an array of annotations
*/
function parseAnnotationsMD() {
var markdown_parser = new mp();
var annotations = [];

//attempt to read the file
Expand All @@ -53,51 +56,25 @@ var annotations_exporter = function (pl) {

//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 = {};

//for each annotation process the yaml frontmatter and markdown
var annotationSnippet = annotationsYAML[i];
var annotationsRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
var chunks = annotationsRE.exec(annotationSnippet);
if (chunks && chunks[1] && chunks[2]) {

//convert each yaml frontmatter key into an object key
var frontmatter = chunks[1];
var frontmatterLines = frontmatter.split(/\n/gm);
for (var j = 0; j < frontmatterLines.length; j++) {
var frontmatterLine = frontmatterLines[j];
if (frontmatterLine.length > 0) {
var frontmatterLineChunks = frontmatterLine.split(':'); //test this
var frontmatterKey = frontmatterLineChunks[0].toLowerCase().trim();
var frontmatterValueString = frontmatterLineChunks[1].trim();
var frontmatterValue = frontmatterValueString.substring(1, frontmatterValueString.length - 1);
if (frontmatterKey === 'el' || frontmatterKey === 'selector') {
annotation.el = frontmatterValue;
}
if (frontmatterKey === 'title') {
annotation.title = frontmatterValue;
}
}
}

//set the comment to the parsed markdown
var annotationMarkdown = chunks[2];
annotation.comment = md.render(annotationMarkdown);

annotations.push(annotation);
} else {
console.log('annotations.md file not formatted as expected. Error parsing frontmatter and markdown out of ' + annotationSnippet);
}
var markdownObj = markdown_parser.parse(annotationsYAML[i]);

annotation.el = markdownObj.el || markdownObj.selector;
annotation.title = markdownObj.title;
annotation.comment = markdownObj.markdown;

annotations.push(annotation);
}
return annotations;
}

function gatherAnnotations() {
var annotationsJS = parseAnnotationsJS();
var annotationsMD = parseAnnotationsMD();
var mergedAnnotations = _.unionBy(annotationsJS, annotationsMD, 'el');
return mergedAnnotations;
return _.unionBy(annotationsJS, annotationsMD, 'el');
}

return {
Expand Down
53 changes: 53 additions & 0 deletions core/lib/markdown_parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

var md = require('markdown-it')();

var markdown_parser = function () {

function parseMarkdownBlock(block) {
var returnObject = {};

try {
//for each block process the yaml frontmatter and markdown
var frontmatterRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
var chunks = frontmatterRE.exec(block);
if (chunks && chunks[1] && chunks[2]) {

//convert each yaml frontmatter key / value into an object key
var frontmatter = chunks[1];
var frontmatterLines = frontmatter.split(/\n/gm);
for (var j = 0; j < frontmatterLines.length; j++) {

var frontmatterLine = frontmatterLines[j];
if (frontmatterLine.length > 0) {

var frontmatterLineChunks = frontmatterLine.split(':'); //test this
var frontmatterKey = frontmatterLineChunks[0].toLowerCase().trim();
var frontmatterValueString = frontmatterLineChunks[1].trim();

returnObject[frontmatterKey] = frontmatterValueString.substring(1, frontmatterValueString.length - 1);
}

}

//parse the actual markdown
returnObject.markdown = md.render(chunks[2]);
}
} catch (ex) {
console.log(ex);
console.log('error parsing markdown block', block);
}

//return the frontmatter keys and markdown for a consumer to decide what to do with
return returnObject;
}

return {
parse: function (block) {
return parseMarkdownBlock(block);
}
};

};

module.exports = markdown_parser;
1 change: 1 addition & 0 deletions core/lib/object_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var Pattern = function (relPath, data) {
// name of the pattern. UPDATE: this.key is now known as this.patternPartial
this.patternPartial = this.patternGroup + '-' + this.patternBaseName;

this.patternState = '';
this.template = '';
this.patternPartialCode = '';
this.lineage = [];
Expand Down
89 changes: 69 additions & 20 deletions core/lib/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var pattern_assembler = function () {
fs = require('fs-extra'),
Pattern = require('./object_factory').Pattern,
pph = require('./pseudopattern_hunter'),
md = require('markdown-it')(),
mp = require('./markdown_parser'),
plutils = require('./utilities'),
patternEngines = require('./pattern_engines');

Expand Down Expand Up @@ -66,11 +66,19 @@ var pattern_assembler = function () {
}
}

function setState(pattern, patternlab) {
/*
* Deprecated in favor of .md 'status' frontmatter inside a pattern. Still used for unit tests at this time.
* Will be removed in future versions
*/
function setState(pattern, patternlab, displayDeprecatedWarning) {
if (patternlab.config.patternStates && patternlab.config.patternStates[pattern.patternPartial]) {

if (displayDeprecatedWarning) {
plutils.logRed("Deprecation Warning: Using patternlab-config.json patternStates object will be deprecated in favor of the state frontmatter key associated with individual pattern markdown files.");
console.log("This feature will still work in it's current form this release (but still be overridden by the new parsing method), and will be removed in the future.");
}

pattern.patternState = patternlab.config.patternStates[pattern.patternPartial];
} else {
pattern.patternState = "";
}
}

Expand Down Expand Up @@ -123,6 +131,54 @@ var pattern_assembler = function () {
}
}

function parsePatternMarkdown(currentPattern, patternlab) {

var markdown_parser = new mp();

try {
var markdownFileName = path.resolve(patternlab.config.paths.source.patterns, currentPattern.subdir, currentPattern.fileName + ".md");
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');

var markdownObject = markdown_parser.parse(markdownFileContents);
if (!plutils.isObjectEmpty(markdownObject)) {
//set keys and markdown itself
currentPattern.patternDescExists = true;
currentPattern.patternDesc = markdownObject.markdown;

//consider looping through all keys eventually. would need to blacklist some properties and whitelist others
if (markdownObject.state) {
currentPattern.patternState = markdownObject.state;
}
if (markdownObject.order) {
currentPattern.order = markdownObject.order;
}
if (markdownObject.hidden) {
currentPattern.hidden = markdownObject.hidden;
}
if (markdownObject.excludeFromStyleguide) {
currentPattern.excludeFromStyleguide = markdownObject.excludeFromStyleguide;
}
if (markdownObject.tags) {
currentPattern.tags = markdownObject.tags;
}
if (markdownObject.links) {
currentPattern.links = markdownObject.links;
}
} else {
if (patternlab.config.debug) {
console.log('error processing markdown for ' + currentPattern.patternPartial);
}
}

if (patternlab.config.debug) {
console.log('found pattern-specific markdown for ' + currentPattern.patternPartial);
}
}
catch (e) {
// do nothing
}
}

function processPatternIterative(relPath, patternlab) {

var pseudopattern_hunter = new pph();
Expand All @@ -149,7 +205,7 @@ var pattern_assembler = function () {
}

//see if this file has a state
setState(currentPattern, patternlab);
setState(currentPattern, patternlab, true);

//look for a json file for this template
try {
Expand Down Expand Up @@ -193,18 +249,7 @@ var pattern_assembler = function () {
}

//look for a markdown file for this template
try {
var markdownFileName = path.resolve(patternlab.config.paths.source.patterns, currentPattern.subdir, currentPattern.fileName + ".md");
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
currentPattern.patternDescExists = true;
currentPattern.patternDesc = md.render(markdownFileContents);
if (patternlab.config.debug) {
console.log('found pattern-specific markdown-documentation.md for ' + currentPattern.patternPartial);
}
}
catch (e) {
// do nothing
}
parsePatternMarkdown(currentPattern, patternlab);

//add the raw template to memory
currentPattern.template = fs.readFileSync(path.resolve(patternsPath, relPath), 'utf8');
Expand Down Expand Up @@ -246,14 +291,15 @@ var pattern_assembler = function () {
currentPattern.extendedTemplate = currentPattern.template;

//find how many partials there may be for the given pattern
var foundPatternPartials = currentPattern.findPartials(currentPattern);
var foundPatternPartials = currentPattern.findPartials();

//find any listItem blocks that within the pattern, even if there are no partials
list_item_hunter.process_list_item_partials(currentPattern, patternlab);

// expand any partials present in this pattern; that is, drill down into
// the template and replace their calls in this template with rendered
// results

if (currentPattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {
// eslint-disable-next-line
expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern);
Expand Down Expand Up @@ -371,8 +417,8 @@ var pattern_assembler = function () {
find_list_items: function (pattern) {
return pattern.findListItems();
},
setPatternState: function (pattern, patternlab) {
setState(pattern, patternlab);
setPatternState: function (pattern, patternlab, displayDeprecatedWarning) {
setState(pattern, patternlab, displayDeprecatedWarning);
},
addPattern: function (pattern, patternlab) {
addPattern(pattern, patternlab);
Expand All @@ -397,6 +443,9 @@ var pattern_assembler = function () {
},
parse_data_links_specific: function (patternlab, data, label) {
return parseDataLinksHelper(patternlab, data, label)
},
parse_pattern_markdown: function (pattern, patternlab) {
parsePatternMarkdown(pattern, patternlab);
}
};

Expand Down
2 changes: 1 addition & 1 deletion core/lib/patternlab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v2.0.0 - 2016
* patternlab-node - v2.1.0 - 2016
*
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
* Licensed under the MIT license.
Expand Down
6 changes: 3 additions & 3 deletions core/lib/pseudopattern_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var pseudopattern_hunter = function () {
console.log('There was an error parsing pseudopattern JSON for ' + currentPattern.relPath);
console.log(err);
}

//extend any existing data with variant data
variantFileData = plutils.mergeData(currentPattern.jsonFileData, variantFileData);

Expand All @@ -56,8 +56,8 @@ var pseudopattern_hunter = function () {
engine: currentPattern.engine
});

//see if this file has a state
pattern_assembler.setPatternState(patternVariant, patternlab);
//process the companion markdown file if it exists
pattern_assembler.parse_pattern_markdown(patternVariant, patternlab);

//find pattern lineage
lineage_hunter.find_lineage(patternVariant, patternlab);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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.0.1",
"version": "2.1.0",
"main": "./core/lib/patternlab.js",
"dependencies": {
"diveSync": "^0.3.0",
Expand Down
1 change: 0 additions & 1 deletion patternlab-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"ishMaximum": "2600",
"patternStateCascade": ["inprogress", "inreview", "complete"],
"patternStates": {
"molecules-block-hero" : "inreview"
},
"patternExportPatternPartials": [],
"patternExportDirectory": "./pattern_exports/",
Expand Down
Loading

0 comments on commit 781e2d5

Please sign in to comment.