colorize.js (1303B)
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 // Distributed under an MIT license: http://codemirror.net/LICENSE 3 4 (function(mod) { 5 if (typeof exports == "object" && typeof module == "object") // CommonJS 6 mod(require("../../lib/codemirror"), require("./runmode")); 7 else if (typeof define == "function" && define.amd) // AMD 8 define(["../../lib/codemirror", "./runmode"], mod); 9 else // Plain browser env 10 mod(CodeMirror); 11 })(function(CodeMirror) { 12 "use strict"; 13 14 var isBlock = /^(p|li|div|h\\d|pre|blockquote|td)$/; 15 16 function textContent(node, out) { 17 if (node.nodeType == 3) return out.push(node.nodeValue); 18 for (var ch = node.firstChild; ch; ch = ch.nextSibling) { 19 textContent(ch, out); 20 if (isBlock.test(node.nodeType)) out.push("\n"); 21 } 22 } 23 24 CodeMirror.colorize = function(collection, defaultMode) { 25 if (!collection) collection = document.body.getElementsByTagName("pre"); 26 27 for (var i = 0; i < collection.length; ++i) { 28 var node = collection[i]; 29 var mode = node.getAttribute("data-lang") || defaultMode; 30 if (!mode) continue; 31 32 var text = []; 33 textContent(node, text); 34 node.innerHTML = ""; 35 CodeMirror.runMode(text.join(""), mode, node); 36 37 node.className += " cm-s-default"; 38 } 39 }; 40 });