Skip to content

Instantly share code, notes, and snippets.

@faceyspacey
Last active August 29, 2015 14:19
Show Gist options
  • Save faceyspacey/ce19cd47706a16ee06ee to your computer and use it in GitHub Desktop.
Save faceyspacey/ce19cd47706a16ee06ee to your computer and use it in GitHub Desktop.
match html to spacebars template algorithm
function findMatchingSpacebarsTemplate(nodes, templates, lineIndex) {
var lineIndex = lineIndex || 0,
templateMatches = templatesWhoseFirstLineMatchesNode(node.shift(), templates, lineIndex),
isMultipleMatches = templateMatches.length > 1;
//nodes has first element removed, and templateMatches is reduced to those that match on the first line
if(isMultipleMatches) return findMatchingSpacebarsTemplate(nodes, templateMatches, lineIndex + 1);
else return templateMatches.pop(); //we have found the one matching template! yay!!
}
function templatesWhoseFirstLineMatchesNode(node, templates, lineIndex) {
return _.filter(templates, function(template) {
//obviously we will do more advanced parsing here, but the idea is it's not that complex
return template.lineToString(lineIndex).indexOf(node.openingTagToString()) > -1;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment