Skip to content

Instantly share code, notes, and snippets.

@ninjascribble
Created February 19, 2013 21:50
Show Gist options
  • Save ninjascribble/4990415 to your computer and use it in GitHub Desktop.
Save ninjascribble/4990415 to your computer and use it in GitHub Desktop.
/**
* Replace any ocurrences of {{dimsum}} with generated text.
*/
dimsum.parse = function(root) {
var reg = /{{dimsum[:]?([pst0-9]*)}}/ig,
root = root || document.getElementsByTagName('body')[0],
command = '';
// If root is just a string, then return a string with replacement
if (typeof root == 'string') {
return root.replace(reg, function(m, sm) {
return make_dimsum(sm, 'text');
});
}
// If it's a TextNode, then make sure it matches and replace it with some markup
else if (root.nodeType == 3 && root.data.match(reg)) {
var div = document.createElement('div');
div.className = 'dimsum';
div.innerHTML = root.data.replace(reg, function(m, sm) {
return make_dimsum(sm, 'html');
});
root.parentNode.insertBefore(div, root);
root.parentNode.removeChild(root);
}
// If it's not a TextNode, then keep on walking the DOM
else {
for (var i = 0, len = root.childNodes.length; i < len; i++) {
dimsum.parse(root.childNodes[i]);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment