/** * Super simple syntax highlighting plugin for CSSS code snippets * Usage: * @author Lea Verou */ (function(){ if(!document.body.insertAdjacentHTML) { return; } var self = window.Highlight = { languages: { javascript: { 'comment': /(\/\*.*?\*\/)|\/\/.*?(\r?\n|$)/g, // TODO multiline support 'string': /(('|").*?(\2))/g, // used to be: /'.*?'|".*?"/g, 'keyword': /\b(var|let|if|else|while|do|for|return|in|instanceof|function|new|with|typeof)\b/g, 'boolean': /\b(true|false)\b/g, 'number': /\b-?(0x)?\d*\.?\d+\b/g, 'regex': /\/.+?\/[gim]{0,3}/g } }, isInited: function(code) { return code.hasAttribute('data-highlighted'); }, init: function(code) { if(!code || self.isInited(code)) { return; // or should I rehighlight? } var lang = self.languages[code.getAttribute('lang')]; if(!lang) { return; } code.normalize(); for(var token in lang) { // Assumption: If there are other tags in the code, they don't cut a token in half var textNodes = getTextNodes(code, function(node) { var parent = node.parentNode; return !(/span/i.test(parent.nodeName) && /^token\s/.test(parent.className)); }); for(var i=0; i/g, '>'); var newText = text.replace(lang[token], function($0) { return '' + $0 + '' }); if(newText !== text) { replaceNodeWithHTML(oldNode, newText); } } } code.setAttribute('data-highlighted', 'true'); }, container: function(container) { if(!container) { return; } var codes = container.querySelectorAll('code[lang]'); for(var i=0; i