Skip to content

Instantly share code, notes, and snippets.

@mx-moth
Created May 8, 2014 05:05
Show Gist options
  • Save mx-moth/27be03dc8c3eb9ee0096 to your computer and use it in GitHub Desktop.
Save mx-moth/27be03dc8c3eb9ee0096 to your computer and use it in GitHub Desktop.
Reverse sax parser
"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