Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PatternEngines ongoing work, round 4 #208

Merged
merged 25 commits into from
Dec 14, 2015
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3dfe791
resolve issue with not hiding underscored patterns.
bmuenzenmeyer Nov 26, 2015
4faa3fa
Fix wrong class name for Error Input
Dec 2, 2015
f6ff240
Merge pull request #200 from johngerome/master
Dec 3, 2015
ed4f8dd
- ADD: Added a note in the README during installation to run with ele…
bmuenzenmeyer Dec 5, 2015
cfedea6
Merge branch 'dev' of https://github.com/pattern-lab/patternlab-node …
bmuenzenmeyer Dec 5, 2015
800f3b9
added two patternState unit tests as part of #97
bmuenzenmeyer Dec 5, 2015
277fdff
delete this file, it's an artifact and should not be included
bmuenzenmeyer Dec 5, 2015
bb47678
Generate the list of supported pattern engines based on the presence to
geoffp Dec 7, 2015
a66d0a8
Made pseudopattern_hunter a bit less hardcoded
bmuenzenmeyer Dec 8, 2015
e4b73e8
further streamline engine loading process and logging
geoffp Dec 8, 2015
d80df26
clean up require()s
geoffp Dec 8, 2015
c7adf8a
slight polish to engine loader console output
geoffp Dec 8, 2015
5cab80b
clean up eslintrc a bit
geoffp Dec 8, 2015
fe6206b
make process_pattern_iterative() return the oPattern to make it a little
geoffp Dec 8, 2015
865b38d
Handlebars engine, plus unit tests and proposed pattern tree for
geoffp Dec 8, 2015
c741d18
I am now realizing that I called an atom from inside an atom. The
geoffp Dec 8, 2015
5302a4d
oops, add molecules directory and pattern
geoffp Dec 8, 2015
5c745c2
add Handlebars as an npm dependency (for now)
geoffp Dec 8, 2015
ddfd363
Support for pattern link.* shortcut inside pattern data objects.
bmuenzenmeyer Dec 9, 2015
3f2efa9
Merge pull request #207 from pattern-lab/dev
Dec 9, 2015
6b22356
Merge remote-tracking branch 'upstream/master' into pattern-engines
geoffp Dec 9, 2015
8ac9b6e
(mostly?) working handlebars rendering on a real tree -- there's a
geoffp Dec 11, 2015
82c76ed
Fix the circular reference problem in the log file, which occurred when
geoffp Dec 14, 2015
6693ee2
remove speculative alternate pattern data structures
geoffp Dec 14, 2015
a2c6c71
Add pattern-engines branch to .travis.yml
geoffp Dec 14, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"env": {
"jasmine": true,
"node": true,
"mocha": true,
"browser": true,
"builtin": true
},
"globals": {},
Expand Down
27 changes: 13 additions & 14 deletions builder/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"use strict";

var pattern_assembler = function(){

var fs = require('fs-extra'),
var path = require('path'),
fs = require('fs-extra'),
of = require('./object_factory'),
plutils = require('./utilities'),
patternEngines = require('./pattern_engines/pattern_engines'),
config = fs.readJSONSync('./config.json');
patternEngines = require('./pattern_engines/pattern_engines');

var config = fs.readJSONSync('./config.json');

function setState(pattern, patternlab){
if(patternlab.config.patternStates[pattern.patternName]){
Expand Down Expand Up @@ -66,10 +67,6 @@
}

function processPatternIterative(file, patternlab){
var fs = require('fs-extra'),
of = require('./object_factory'),
path = require('path');

//extract some information
var subdir = path.dirname(path.relative(patternlab.config.patterns.source, file)).replace('\\', '/');
var filename = path.basename(file);
Expand All @@ -80,7 +77,7 @@
}

// skip non-pattern files
if (!patternEngines.isPatternFile(filename, patternlab)) { return; }
if (!patternEngines.isPatternFile(filename, patternlab)) { return null; }
if (config.debug) {
console.log('processPatternIterative:', 'found pattern', file);
}
Expand All @@ -95,15 +92,15 @@
//the object in the next diveSync
addPattern(currentPattern, patternlab);
//no need to process further
return;
return currentPattern;
}

//can ignore all non-mustache files at this point
if(ext !== '.mustache'){
//can ignore all non-supported files at this point
if(patternEngines.isFileExtensionSupported(ext) === false){
if (config.debug) {
console.log('==================== FOUND NON-MUSTACHE FILE');
}
return;
return currentPattern;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a super great idea. 💯

}

//see if this file has a state
Expand Down Expand Up @@ -143,6 +140,8 @@

//add currentPattern to patternlab.patterns array
addPattern(currentPattern, patternlab);

return currentPattern;
}


Expand Down Expand Up @@ -300,7 +299,7 @@
return renderPattern(template, data, partials);
},
process_pattern_iterative: function(file, patternlab){
processPatternIterative(file, patternlab);
return processPatternIterative(file, patternlab);
},
process_pattern_recursive: function(file, patternlab, additionalData){
processPatternRecursive(file, patternlab, additionalData);
Expand Down
66 changes: 66 additions & 0 deletions builder/pattern_engines/engine_handlebars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* handlebars pattern engine for patternlab-node - v0.15.1 - 2015
*
* Geoffrey Pursell, Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
*
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
*
*/

(function () {
"use strict";

var Handlebars = require('handlebars');

var engine_handlebars = {
engine: Handlebars,
engineName: 'handlebars',
engineFileExtension: '.hbs',

// regexes, stored here so they're only compiled once
// GTP warning: unchanged copypasta from mustache engine
findPartialsRE: /{{>\s*((?:\d+-[\w-]+\/)+(\d+-[\w-]+(\.\w+)?)|[A-Za-z0-9-]+)(\:[\w-]+)?(\(\s*\w+\s*:\s*(?:'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")\))?\s*}}/g,
findPartialsWithStyleModifiersRE: /{{>([ ])?([\w\-\.\/~]+)(?!\()(\:[A-Za-z0-9-_|]+)+(?:(| )\(.*)?([ ])?}}/g,
findPartialsWithPatternParametersRE: /{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-_|]+)?(?:(| )\(.*)+([ ])?}}/g,
findListItemsRE: /({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g,
getPartialKeyRE: /{{>([ ])?([\w\-\.\/~]+)(:[A-z-_|]+)?(?:\:[A-Za-z0-9-_]+)?(?:(| )\(.*)?([ ])?}}/g,

// render it
renderPattern: function renderPattern(template, data, partials) {
if (partials) {
Handlebars.registerPartial(partials);
}
var compiled = Handlebars.compile(template);
return compiled(data);
},

// find and return any {{> template-name }} within pattern
findPartials: function findPartials(pattern) {
var matches = pattern.template.match(this.findPartialsRE);
return matches;
},
findPartialsWithStyleModifiers: function(pattern) {
var matches = pattern.template.match(this.findPartialsWithStyleModifiersRE);
return matches;
},
// returns any patterns that match {{> value(foo:"bar") }} or {{>
// value:mod(foo:"bar") }} within the pattern
findPartialsWithPatternParameters: function(pattern) {
var matches = pattern.template.match(this.findPartialsWithPatternParametersRE);
return matches;
},
findListItems: function(pattern) {
var matches = pattern.template.match(this.findListItemsRE);
return matches;
},
// given a pattern, and a partial string, tease out the "pattern key" and
// return it.
getPartialKey: function(pattern, partialString) {
var partialKey = partialString.replace(this.getPartialKeyRE, '$2');
return partialKey;
}
};

module.exports = engine_handlebars;
})();
126 changes: 90 additions & 36 deletions builder/pattern_engines/pattern_engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,97 @@
'use strict';

var path = require('path');
var diveSync = require('diveSync');
var engineMatcher = /^engine_(.*)\.js/;
var enginesDirectory = __dirname;

// list of supported pattern engines
var supportedPatternEngineNames = [
'mustache',
'handlebars'
];
var PatternEngines; // the main export object
var engineNameForExtension; // generated mapping of extension to engine name

// mapping of file extensions to engine names, for lookup use
var engineNameForExtension = {};

// Object/hash of all loaded pattern engines, empty at first.
// My intention here is to make this return an object that can be used to
// obtain any loaded PatternEngine by addressing them like this:
// free "private" functions, for internal setup only

function findSupportedPatternEngineNames() {
var foundPatternEngineNames = [];

// find
diveSync(enginesDirectory, {
recursive: false,
filter: function (filePath, dir) {
var baseName = path.basename(filePath),
engineMatch = baseName.match(engineMatcher);

if (dir || engineMatch !== null) { return true; }
return false;
}
}, function (err, filePath) {
if (err) { throw err; }
var baseName = path.basename(filePath),
engineMatch = baseName.match(engineMatcher),
foundEngineName = engineMatch[1];

foundPatternEngineNames.push(foundEngineName);
});

return foundPatternEngineNames;
}

// try to load all supported engines
function loadAllEngines(enginesObject) {
console.log('\nLoading engines...');

enginesObject.supportedPatternEngineNames.forEach(function (engineName) {
var notLoaded = false;

try {
enginesObject[engineName] = require('./engine_' + engineName);
} catch (err) {
// Handle errors loading each pattern engine. This will usually be
// because the engine's renderer hasn't been installed by the end user
// -- we don't include any of them (except mustache) by default as
// depedencies in package.json.
notLoaded = (err.code === 'MODULE_NOT_FOUND');
} finally {
console.log('-', engineName, 'engine:',
notLoaded ? 'renderer not installed; engine disabled' : 'good to go');
}
});
console.log('...done loading engines.\n');
}

// produce a mapping between file extension and engine name for each of the
// loaded engines
function createFileExtensionToEngineNameMap(enginesObject) {
var mapping = {};

Object.keys(enginesObject).forEach(function (engineName) {
var extensionForEngine = enginesObject[engineName].engineFileExtension;
mapping[extensionForEngine] = engineName;
});

return mapping;
}


//
// PatternEngines: the main export of this module
//
// var PatternEngines = require('./pattern_engines/pattern_engines');
// var Mustache = PatternEngines['mustache'];
// It's an Object/hash of all loaded pattern engines, empty at first. My
// intention here is to make this return an object that can be used to obtain
// any loaded PatternEngine by addressing them like this:
//
var PatternEngines = Object.create({
supportedPatternEngineNames: supportedPatternEngineNames,
// var PatternEngines = require('./pattern_engines/pattern_engines');
// var Mustache = PatternEngines['mustache'];
//
// Object.create lets us create an object with a specified prototype. We want
// this here because we would like the object's "own properties" to include
// only the engine names so we can easily iterate over them; all the handy
// methods and properites below should therefore be on its prototype.

PatternEngines = Object.create({
// build the list of supported pattern engines based on what plugins we have
// in the pattern_engines directory
supportedPatternEngineNames: findSupportedPatternEngineNames(),

getEngineNameForPattern: function (pattern) {
// avoid circular dependency by putting this in here. TODO: is this slow?
Expand Down Expand Up @@ -93,30 +165,12 @@
}
});

// try to load all supported engines
(function loadAllEngines() {
supportedPatternEngineNames.forEach(function (engineName) {
try {
PatternEngines[engineName] = require('./engine_' + engineName);
} catch (err) {
console.log(err, 'pattern engine "' + engineName + '" not loaded. Did you install its dependency with npm?');
}
});
})();

// produce a mapping between file extension and engine name for each of the
// loaded engines
engineNameForExtension = (function () {
var mapping = {};

Object.keys(PatternEngines).forEach(function (engineName) {
var extensionForEngine = PatternEngines[engineName].engineFileExtension;
mapping[extensionForEngine] = engineName;
});

return mapping;
})();
// load up the engines we found
loadAllEngines(PatternEngines);

// mapping of file extensions to engine names, for lookup use
engineNameForExtension = createFileExtensionToEngineNameMap(PatternEngines);

module.exports = PatternEngines;
})();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"grunt-contrib-copy": "^0.8.2",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-contrib-watch": "^0.6.1",
"handlebars": "^4.0.5",
"html-entities": "^1.2.0",
"matchdep": "^1.0.0",
"mustache": "^2.2.0"
Expand Down
Loading