Created
May 8, 2014 05:05
-
-
Save mx-moth/27be03dc8c3eb9ee0096 to your computer and use it in GitHub Desktop.
Reverse sax parser
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
"use strict"; | |
function processNodeList(list, target) { | |
list.forEach(function(node) { | |
processNode(node, target); | |
}); | |
} | |
function processNode(node, target) { | |
switch (node._type) { | |
case 'HTML': | |
if (Array.isArray(node.contents)) { | |
outputNodeList(node.contents); | |
} else { | |
target.fireEvent('html', node); | |
} | |
break; | |
case 'TAG': | |
target.fireEvent('tag', node); | |
break; | |
default: | |
target.fireEvent('unknownNode', node); | |
break; | |
} | |
} | |
function main() { | |
var list = getNodeList(); | |
var target = new EventListener(); | |
var output = []; | |
target.addEventListener('html', function(node) { | |
output.append(node.contents); | |
}); | |
target.addEventListener('tag', function(node) { | |
output.append('{% '); | |
output.append(node.tagName); | |
output.append(node.arguments.join(' ')); | |
output.append(' %}'); | |
}); | |
processNodeList(list, target); | |
console.log(output.join('')); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment