Skip to content
Closed
Changes from all commits
Commits
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
158 changes: 86 additions & 72 deletions builder/lineage_hunter.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,90 @@
/*
* patternlab-node - v1.1.1 - 2016
*
* 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 lineage_hunter = function(){

function findlineage(pattern, patternlab){

var pa = require('./pattern_assembler');
var pattern_assembler = new pa();

//find the {{> template-name }} within patterns
var matches = pattern_assembler.find_pattern_partials(pattern);
if(matches !== null){
matches.forEach(function(match, index, matches){
//strip out the template cruft
var foundPatternKey = match.replace("{{> ", "").replace(" }}", "").replace("{{>", "").replace("}}", "");

// remove any potential pattern parameters. this and the above are rather brutish but I didn't want to do a regex at the time
if(foundPatternKey.indexOf('(') > 0){
foundPatternKey = foundPatternKey.substring(0, foundPatternKey.indexOf('('));
}

//remove any potential stylemodifiers.
foundPatternKey = foundPatternKey.split(':')[0];

//get the ancestorPattern
var ancestorPattern = pattern_assembler.get_pattern_by_key(foundPatternKey, patternlab);

if (ancestorPattern && pattern.lineageIndex.indexOf(ancestorPattern.key) === -1){

//add it since it didnt exist
pattern.lineageIndex.push(ancestorPattern.key);
//create the more complex patternLineage object too
var l = {
"lineagePattern": ancestorPattern.key,
"lineagePath": "../../patterns/" + ancestorPattern.patternLink
};
pattern.lineage.push(JSON.stringify(l));

//also, add the lineageR entry if it doesn't exist
if (ancestorPattern.lineageRIndex.indexOf(pattern.key) === -1){
ancestorPattern.lineageRIndex.push(pattern.key);

//create the more complex patternLineage object in reverse
var lr = {
"lineagePattern": pattern.key,
"lineagePath": "../../patterns/" + pattern.patternLink
};
ancestorPattern.lineageR.push(JSON.stringify(lr));
}
}
});
}
}

return {
find_lineage: function(pattern, patternlab){
findlineage(pattern, patternlab);
}
/*
* patternlab-node - v1.1.1 - 2016
*
* Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
*
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
*
*/

"use strict";

var pa = require('./pattern_assembler');

function create_lineage_path(pattern, path) {
return {
"lineagePattern": pattern,
"lineagePath": "../../patterns/" + path
};
}

};
function get_pattern_name(str) {
var PATTERN_OPEN = /^\{\{\>( )?/,
PATTERN_CLOSE = /( )?\}\}$/,
foundPatternKey = str.replace(PATTERN_OPEN, "").replace(PATTERN_CLOSE, ""),
patternKey = foundPatternKey.indexOf('('),
hasPatternKey = patternKey > 0;

// remove any potential pattern parameters. this and the above are rather brutish but I didn't want to do a regex at the time
if(hasPatternKey){
foundPatternKey = foundPatternKey.substring(0, patternKey);
}

//remove any potential stylemodifiers.
foundPatternKey = foundPatternKey.split(':')[0];

return foundPatternKey;
}

function parse(match, index, matches, pattern, pattern_assembler, patternlab) {
var foundPatternKey = get_pattern_name(match);

//get the ancestorPattern
var ancestorPattern = pattern_assembler.get_pattern_by_key(foundPatternKey, patternlab),
ancestorPatternExist = pattern.lineageIndex.indexOf(ancestorPattern.key) >= 0;

if (!ancestorPattern || ancestorPatternExist) {
return;
}

//add it since it didnt exist
pattern.lineageIndex.push(ancestorPattern.key);

//create the more complex patternLineage object too
var l = create_lineage_path(ancestorPattern.key, ancestorPattern.patternLink);
pattern.lineage.push(JSON.stringify(l));

module.exports = lineage_hunter;
//also, add the lineageR entry if it doesn't exist
var lineageRExist = ancestorPattern.lineageRIndex.indexOf(pattern.key) >= 0;

}());
if (lineageRExist) {
return;
}

ancestorPattern.lineageRIndex.push(pattern.key);

//create the more complex patternLineage object in reverse
var lr = create_lineage_path(pattern.key, pattern.patternLink);
ancestorPattern.lineageR.push(JSON.stringify(lr));
}

function findlineage(pattern, patternlab){
var pattern_assembler = new pa();

var matches = pattern_assembler.find_pattern_partials(pattern);

if(!matches) {
return;
}

matches.forEach(function(match, index, matches) {
parse(match, index, matches, pattern, pattern_assembler, patternlab);
});
}

module.exports = function() {
return {
find_lineage: findlineage
};
}