Skip to content

Parameter hunter to partial hunter #416

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

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d0bff73
removing patternlab.partials as renderPattern param
e2tha-e Jul 28, 2016
fbcc9ab
Merge branch 'dev' of github.com:pattern-lab/patternlab-node into rep…
e2tha-e Jul 28, 2016
a3bddeb
adding patternPartials, allData, and dataKeys to pattern object proto…
e2tha-e Jul 28, 2016
9740111
adding patternPartials, allData, and dataKeys to pattern object proto…
e2tha-e Jul 28, 2016
1a59987
adding allData to pattern object prototype
e2tha-e Jul 28, 2016
29a4ae0
fixing unit tests
e2tha-e Jul 29, 2016
89996a7
parameter-hunter-to-partial-hunte
e2tha-e Jul 29, 2016
8ee0bb3
stopping point
e2tha-e Jul 29, 2016
0b7d24c
partial_hunters_tests.js passes
e2tha-e Jul 30, 2016
325381c
partial_hunter_tests.js passes
e2tha-e Jul 30, 2016
b2953a7
working list_item_hunter.js and unit tests
e2tha-e Jul 30, 2016
8d47307
working engine_mustache_tests.js
e2tha-e Jul 30, 2016
89b0b9e
mutation instead of return
e2tha-e Jul 30, 2016
e4ce892
adding test/_patterns/01-test1 dir
e2tha-e Jul 30, 2016
2718453
pattern_assembler correctly processes list items
e2tha-e Jul 30, 2016
e63bbf8
more working unit tests
e2tha-e Jul 30, 2016
9e96be0
pattern_assembler.js to work with refactor
e2tha-e Jul 30, 2016
958746b
new test file
e2tha-e Jul 30, 2016
e36b9b3
tabs to spaces in pattern_assembler_tests.js
e2tha-e Jul 30, 2016
3ab0f83
working pattern_assembler_tests.js
e2tha-e Jul 30, 2016
18cee7a
cleaning up pattern_assembler.js
e2tha-e Jul 30, 2016
3214664
preprocessing partials
e2tha-e Jul 30, 2016
c905356
patternlab.js updates
e2tha-e Jul 31, 2016
d601a68
tighter partial_hunter logic
e2tha-e Jul 31, 2016
95851db
dataKeys array includes mustache tags with dotted-object-key notation
e2tha-e Jul 31, 2016
6c4767f
more efficient data parse and stronger getDataKeys
e2tha-e Jul 31, 2016
c85a44f
more efficient getDataKeys
e2tha-e Aug 1, 2016
8c10e16
code cleanup
e2tha-e Aug 1, 2016
f493ee7
more efficient data key escape
e2tha-e Aug 1, 2016
4127fa0
removing extraneous block
e2tha-e Aug 1, 2016
6861e73
pseudopattern_hunger.js partially working
e2tha-e Aug 2, 2016
2a5199e
working pseudopattern_hunger.js
e2tha-e Aug 2, 2016
d364119
working pseudopattern_hunter.js
e2tha-e Aug 2, 2016
525552e
working list_item_hunter_tests.js
e2tha-e Aug 2, 2016
d8c80b1
fixing viewwall
e2tha-e Aug 2, 2016
762d975
working unit tests
e2tha-e Aug 3, 2016
ddbd002
eslinted
e2tha-e Aug 3, 2016
2ae4274
findPartial -> getPartial in unit tests
e2tha-e Aug 3, 2016
0a14bfc
tabs to spaces in list_item_hunter_test.js
e2tha-e Aug 3, 2016
e6fff18
checks for engine
e2tha-e Aug 3, 2016
48b4e5f
fixed lineage hunt
e2tha-e Aug 19, 2016
3651f61
fixed links
e2tha-e Sep 22, 2016
32bc7b1
fixing level param type
e2tha-e Oct 4, 2016
523edec
typo fix
e2tha-e Oct 7, 2016
9b75e1c
better regex
e2tha-e Nov 1, 2016
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
117 changes: 56 additions & 61 deletions core/lib/list_item_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,58 @@ var list_item_hunter = function () {
var extend = require('util')._extend,
JSON5 = require('json5'),
pa = require('./pattern_assembler'),
smh = require('./style_modifier_hunter'),
plutils = require('./utilities'),
Pattern = require('./object_factory').Pattern;
plutils = require('./utilities');

var pattern_assembler = new pa(),
style_modifier_hunter = new smh(),
items = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty'];

function getEnd(liMatch) {
return liMatch.replace('#', '/');
}

function getPatternBlock(pattern, liMatch, end) {
return pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch) + liMatch.length, pattern.extendedTemplate.indexOf(end));
}

function preprocessListItemPartials(pattern) {
//find any listitem blocks
var matches = pattern.findListItems();

if (matches !== null) {
matches.forEach(function (liMatch) {
var end = getEnd(liMatch);
var patternBlock = getPatternBlock(pattern, liMatch, end);
var partials = pattern.engine.findPartials(patternBlock);

//escape listitem blocks with partials
if (partials) {
var liMatchEscaped = '\u0002' + liMatch.slice(2);
var endEscaped = '\u0002' + end.slice(2);
var find = liMatch + patternBlock + end;
var replace = liMatchEscaped + patternBlock + endEscaped;

pattern.extendedTemplate = pattern.extendedTemplate.replace(find, replace);
}
});
}
}

function postprocessListItemPartials(pattern) {
//find any listitem blocks
var matches = pattern.extendedTemplate.match(/\u0002(.|\s)*?\}\}/g);

if (matches !== null) {
matches.forEach(function (liMatch) {
var replace = '{{' + liMatch.slice(1);

pattern.extendedTemplate = pattern.extendedTemplate.replace(liMatch, replace);
});
}
}

function processListItemPartials(pattern, patternlab) {
preprocessListItemPartials(pattern);

//find any listitem blocks
var matches = pattern.findListItems();

Expand All @@ -26,8 +69,8 @@ var list_item_hunter = function () {

//find the boundaries of the block
var loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
var end = liMatch.replace('#', '/');
var patternBlock = pattern.template.substring(pattern.template.indexOf(liMatch) + liMatch.length, pattern.template.indexOf(end)).trim();
var end = getEnd(liMatch);
var patternBlock = getPatternBlock(pattern, liMatch, end).trim();

//build arrays that repeat the block, however large we need to
var repeatedBlockTemplate = [];
Expand Down Expand Up @@ -59,59 +102,11 @@ var list_item_hunter = function () {

//combine listItem data with pattern data with global data
var itemData = listData['' + items.indexOf(loopNumberString)]; //this is a property like "2"
var globalData;
var localData;
try {
globalData = JSON5.parse(JSON5.stringify(patternlab.data));
localData = JSON5.parse(JSON5.stringify(pattern.jsonFileData));
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.relPath);
console.log(err);
}

var allData = plutils.mergeData(globalData, localData);
allData = plutils.mergeData(allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup
var allData = plutils.mergeData(pattern.allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup
allData.link = extend({}, patternlab.data.link);

//check for partials within the repeated block
var foundPartials = Pattern.createEmpty({'template': thisBlockTemplate}).findPartials();

if (foundPartials && foundPartials.length > 0) {

for (var j = 0; j < foundPartials.length; j++) {

//get the partial
var partialName = foundPartials[j].match(/([\w\-\.\/~]+)/g)[0];
var partialPattern = pattern_assembler.findPartial(partialName, patternlab);

//create a copy of the partial so as to not pollute it after the get_pattern_by_key call.
var cleanPartialPattern;
try {
cleanPartialPattern = JSON5.parse(JSON5.stringify(partialPattern));
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.relPath);
console.log(err);
}

//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #356
cleanPartialPattern.extendedTemplate = cleanPartialPattern.template;

//if partial has style modifier data, replace the styleModifier value
if (foundPartials[j].indexOf(':') > -1) {
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartials[j], patternlab);
}

//replace its reference within the block with the extended template
thisBlockTemplate = thisBlockTemplate.replace(foundPartials[j], cleanPartialPattern.extendedTemplate);
}

//render with data
thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);

} else {
//just render with mergedData
thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
}
//just render with mergedData
thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData);

//add the rendered HTML to our string
repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML;
Expand All @@ -121,17 +116,17 @@ var list_item_hunter = function () {
var repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length);
pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml);

//update the extendedTemplate in the partials object in case this pattern is consumed later
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate;

});
}

postprocessListItemPartials(pattern);
}

return {
process_list_item_partials: function (pattern, patternlab) {
processListItemPartials(pattern, patternlab);
}
},
items: items
};
};

Expand Down
7 changes: 5 additions & 2 deletions core/lib/object_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ var Pattern = function (relPath, data) {
this.lineageRIndex = [];
this.isPseudoPattern = false;
this.engine = patternEngines.getEngineForPattern(this);
this.patternPartials = null;
this.allData = null;
this.dataKeys = [];
};

// Pattern methods
Expand All @@ -71,9 +74,9 @@ Pattern.prototype = {
return null;
},

registerPartial: function () {
registerPartial: function (patternlab) {
if (this.engine && typeof this.engine.registerPartial === 'function') {
this.engine.registerPartial(this);
this.engine.registerPartial(this, patternlab);
}
},

Expand Down
Loading