Last active
August 29, 2015 14:19
-
-
Save faceyspacey/ce19cd47706a16ee06ee to your computer and use it in GitHub Desktop.
match html to spacebars template algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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