]>
Commit | Line | Data |
---|---|---|
1 | CodeMirror.colorize = (function() { | |
2 | ||
3 | var isBlock = /^(p|li|div|h\\d|pre|blockquote|td)$/; | |
4 | ||
5 | function textContent(node, out) { | |
6 | if (node.nodeType == 3) return out.push(node.nodeValue); | |
7 | for (var ch = node.firstChild; ch; ch = ch.nextSibling) { | |
8 | textContent(ch, out); | |
9 | if (isBlock.test(node.nodeType)) out.push("\n"); | |
10 | } | |
11 | } | |
12 | ||
13 | return function(collection, defaultMode) { | |
14 | if (!collection) collection = document.body.getElementsByTagName("pre"); | |
15 | ||
16 | for (var i = 0; i < collection.length; ++i) { | |
17 | var node = collection[i]; | |
18 | var mode = node.getAttribute("data-lang") || defaultMode; | |
19 | if (!mode) continue; | |
20 | ||
21 | var text = []; | |
22 | textContent(node, text); | |
23 | node.innerHTML = ""; | |
24 | CodeMirror.runMode(text.join(""), mode, node); | |
25 | ||
26 | node.className += " cm-s-default"; | |
27 | } | |
28 | }; | |
29 | })(); |