Skip to content

Commit

Permalink
Merge pull request #391 from pattern-lab/dev
Browse files Browse the repository at this point in the history
Pattern Lab Node 2.2.0
  • Loading branch information
Brian Muenzenmeyer authored Jul 13, 2016
2 parents 2fea50c + 1d651c2 commit 7c7ff39
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 44 deletions.
4 changes: 3 additions & 1 deletion core/lib/annotation_exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ var annotations_exporter = function (pl) {

//parse as JSON by removing the old wrapping js syntax. comments and the trailing semi-colon
oldAnnotations = oldAnnotations.replace('var comments = ', '');
oldAnnotations = oldAnnotations.replace('};', '}');

try {
var oldAnnotationsJSON = JSON5.parse(oldAnnotations.trim().slice(0, -1));
var oldAnnotationsJSON = JSON5.parse(oldAnnotations);
} catch (ex) {
console.log('There was an error parsing JSON for ' + paths.source.annotations + 'annotations.js');
console.log(ex);
Expand Down
8 changes: 6 additions & 2 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.isPattern = true;
this.patternState = '';
this.template = '';
this.patternPartialCode = '';
Expand All @@ -64,11 +65,14 @@ Pattern.prototype = {
// render method on oPatterns; this acts as a proxy for the PatternEngine's
// render function
render: function (data, partials) {
return this.engine.renderPattern(this, data || this.jsonFileData, partials);
if (this.engine) {
return this.engine.renderPattern(this, data || this.jsonFileData, partials);
}
return null;
},

registerPartial: function () {
if (typeof this.engine.registerPartial === 'function') {
if (this.engine && typeof this.engine.registerPartial === 'function') {
this.engine.registerPartial(this);
}
},
Expand Down
88 changes: 67 additions & 21 deletions core/lib/pattern_assembler.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
"use strict";

var pattern_assembler = function () {
var path = require('path'),
fs = require('fs-extra'),
Pattern = require('./object_factory').Pattern,
pph = require('./pseudopattern_hunter'),
mp = require('./markdown_parser'),
plutils = require('./utilities'),
patternEngines = require('./pattern_engines');

var path = require('path'),
fs = require('fs-extra'),
Pattern = require('./object_factory').Pattern,
pph = require('./pseudopattern_hunter'),
mp = require('./markdown_parser'),
plutils = require('./utilities'),
patternEngines = require('./pattern_engines'),
lh = require('./lineage_hunter'),
lih = require('./list_item_hunter'),
smh = require('./style_modifier_hunter'),
ph = require('./parameter_hunter'),
JSON5 = require('json5');

var markdown_parser = new mp();

var pattern_assembler = function () {
// HELPER FUNCTIONS

function getPartial(partialName, patternlab) {
Expand Down Expand Up @@ -108,14 +114,26 @@ var pattern_assembler = function () {
}

// do global registration


if (pattern.isPattern) {
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate || pattern.template;

// do plugin-specific registration
pattern.registerPartial();
} else {
patternlab.partials[pattern.patternPartial] = pattern.patternDesc;
}

patternlab.patterns.push(pattern);
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate || pattern.template;

// do plugin-specific registration
pattern.registerPartial();
}
}

function addSubtypePattern(subtypePattern, patternlab) {
patternlab.subtypePatterns[subtypePattern.patternPartial] = subtypePattern;
}

// Render a pattern on request. Long-term, this should probably go away.
function renderPattern(pattern, data, partials) {
// if we've been passed a full Pattern, it knows what kind of template it
Expand All @@ -133,8 +151,6 @@ 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');
Expand Down Expand Up @@ -185,11 +201,40 @@ var pattern_assembler = function () {

function processPatternIterative(relPath, patternlab) {

//check if the found file is a top-level markdown file
var fileObject = path.parse(relPath);
if (fileObject.ext === '.md') {
try {
var proposedDirectory = path.resolve(patternlab.config.paths.source.patterns, fileObject.dir, fileObject.name);
var proposedDirectoryStats = fs.statSync(proposedDirectory);
if (proposedDirectoryStats.isDirectory()) {
var subTypeMarkdownFileContents = fs.readFileSync(proposedDirectory + '.md', 'utf8');
var subTypeMarkdown = markdown_parser.parse(subTypeMarkdownFileContents);
var subTypePattern = new Pattern(relPath);
subTypePattern.patternSectionSubtype = true;
subTypePattern.patternLink = subTypePattern.name + '/index.html';
subTypePattern.patternDesc = subTypeMarkdown.markdown;
subTypePattern.patternPartial = 'viewall-' + subTypePattern.patternPartial;
subTypePattern.isPattern = false;
subTypePattern.engine = null;

addSubtypePattern(subTypePattern, patternlab)
return subTypePattern;
}
} catch (err) {
// no file exists, meaning it's a pattern markdown file
if (err.code !== 'ENOENT') {
console.log(err);
}
}

}

var pseudopattern_hunter = new pph();

//extract some information
var filename = path.basename(relPath);
var ext = path.extname(filename);
var filename = fileObject.base;
var ext = fileObject.ext;
var patternsPath = patternlab.config.paths.source.patterns;

// skip non-pattern files
Expand Down Expand Up @@ -274,8 +319,6 @@ var pattern_assembler = function () {
}

function processPatternRecursive(file, patternlab) {
var lh = require('./lineage_hunter'),
lih = require('./list_item_hunter');

var lineage_hunter = new lh(),
list_item_hunter = new lih();
Expand All @@ -292,6 +335,9 @@ var pattern_assembler = function () {
//return if processing an ignored file
if (typeof currentPattern === 'undefined') { return; }

//we are processing a markdown only pattern
if (currentPattern.engine === null) { return; }

currentPattern.extendedTemplate = currentPattern.template;

//find how many partials there may be for the given pattern
Expand Down Expand Up @@ -321,8 +367,6 @@ var pattern_assembler = function () {
}

function expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern) {
var smh = require('./style_modifier_hunter'),
ph = require('./parameter_hunter');

var style_modifier_hunter = new smh(),
parameter_hunter = new ph();
Expand Down Expand Up @@ -365,7 +409,6 @@ var pattern_assembler = function () {
}

function parseDataLinksHelper(patternlab, obj, key) {
var JSON5 = require('json5');
var linkRE, dataObjAsString, linkMatches, expandedLink;

linkRE = /link\.[A-z0-9-_]+/g;
Expand Down Expand Up @@ -427,6 +470,9 @@ var pattern_assembler = function () {
addPattern: function (pattern, patternlab) {
addPattern(pattern, patternlab);
},
addSubtypePattern: function (subtypePattern, patternlab) {
addSubtypePattern(subtypePattern, patternlab);
},
renderPattern: function (template, data, partials) {
return renderPattern(template, data, partials);
},
Expand Down
19 changes: 12 additions & 7 deletions core/lib/patternlab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* patternlab-node - v2.1.1 - 2016
* patternlab-node - v2.2.0 - 2016
*
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
* Licensed under the MIT license.
Expand Down Expand Up @@ -73,7 +73,6 @@ var patternlab_engine = function (config) {
pe = require('./pattern_exporter'),
lh = require('./lineage_hunter'),
buildFrontEnd = require('./ui_builder'),
he = require('html-entities').AllHtmlEntities,
plutils = require('./utilities'),
sm = require('./starterkit_manager'),
patternlab = {};
Expand Down Expand Up @@ -148,7 +147,7 @@ var patternlab_engine = function (config) {
// references. This happens specifically with the Handlebars engine. Remove
// if you like 180MB log files.
function propertyStringReplacer(key, value) {
if (key === 'engine' && value.engineName) {
if (key === 'engine' && value && value.engineName) {
return '{' + value.engineName + ' engine object}';
}
return value;
Expand Down Expand Up @@ -207,13 +206,13 @@ var patternlab_engine = function (config) {
process.exit(1);
}
patternlab.patterns = [];
patternlab.subtypePatterns = {};
patternlab.partials = {};
patternlab.data.link = {};

setCacheBust();

var pattern_assembler = new pa(),
entity_encoder = new he(),
pattern_exporter = new pe(),
lineage_hunter = new lh(),
patterns_dir = paths.source.patterns;
Expand Down Expand Up @@ -276,6 +275,10 @@ var patternlab_engine = function (config) {
//render all patterns last, so lineageR works
patternlab.patterns.forEach(function (pattern) {

if (!pattern.isPattern) {
return false;
}

pattern.header = head;

//todo move this into lineage_hunter
Expand All @@ -294,15 +297,15 @@ var patternlab_engine = function (config) {
console.log(err);
}
allData = plutils.mergeData(allData, pattern.jsonFileData);
allData.cacheBuster = patternlab.cacheBuster;

//var headHTML = pattern_assembler.renderPattern(patternlab.userHead, allData);
var headHTML = pattern_assembler.renderPattern(pattern.header, allData);

//render the extendedTemplate with all data
pattern.patternPartialCode = pattern_assembler.renderPattern(pattern, allData);

//todo see if this is still needed
pattern.patternPartialCodeE = entity_encoder.encode(pattern.patternPartialCode);
//pattern.patternPartialCodeE = entity_encoder.encode(pattern.patternPartialCode);

// stringify this data for individual pattern rendering and use on the styleguide
// see if patternData really needs these other duped values
Expand Down Expand Up @@ -333,7 +336,7 @@ var patternlab_engine = function (config) {

//set the pattern-specific footer by compiling the general-footer with data, and then adding it to the meta footer
var footerPartial = pattern_assembler.renderPattern(patternlab.footer, {
isPattern: true,
isPattern: pattern.isPattern,
patternData: pattern.patternData,
cacheBuster: patternlab.cacheBuster
});
Expand All @@ -351,6 +354,8 @@ var patternlab_engine = function (config) {

//write the encoded version too
fs.outputFileSync(paths.public.patterns + pattern.patternLink.replace('.html', '.markup-only.html'), pattern.patternPartialCode);

return true;
});

//export patterns if necessary
Expand Down
Loading

0 comments on commit 7c7ff39

Please sign in to comment.