diff options
author | Michal Čihař <mcihar@suse.cz> | 2013-05-21 12:01:57 +0400 |
---|---|---|
committer | Michal Čihař <mcihar@suse.cz> | 2013-05-24 11:59:38 +0400 |
commit | e03449460ab1d343b4ac1d838fdf53354bb2d054 (patch) | |
tree | 29af77ee5c28694f32525228c4db442b9424d3fd /js/codemirror | |
parent | 5e8b060dcc3291870185735acdb9f6c29c9cda9b (diff) |
Highlight SQL using CodeMirror
Diffstat (limited to 'js/codemirror')
-rw-r--r-- | js/codemirror/addon/runmode/colorize.js | 29 | ||||
-rw-r--r-- | js/codemirror/addon/runmode/runmode.js | 56 |
2 files changed, 85 insertions, 0 deletions
diff --git a/js/codemirror/addon/runmode/colorize.js b/js/codemirror/addon/runmode/colorize.js new file mode 100644 index 0000000000..62286d21e4 --- /dev/null +++ b/js/codemirror/addon/runmode/colorize.js @@ -0,0 +1,29 @@ +CodeMirror.colorize = (function() { + + var isBlock = /^(p|li|div|h\\d|pre|blockquote|td)$/; + + function textContent(node, out) { + if (node.nodeType == 3) return out.push(node.nodeValue); + for (var ch = node.firstChild; ch; ch = ch.nextSibling) { + textContent(ch, out); + if (isBlock.test(node.nodeType)) out.push("\n"); + } + } + + return function(collection, defaultMode) { + if (!collection) collection = document.body.getElementsByTagName("pre"); + + for (var i = 0; i < collection.length; ++i) { + var node = collection[i]; + var mode = node.getAttribute("data-lang") || defaultMode; + if (!mode) continue; + + var text = []; + textContent(node, text); + node.innerHTML = ""; + CodeMirror.runMode(text.join(""), mode, node); + + node.className += " cm-s-default"; + } + }; +})(); diff --git a/js/codemirror/addon/runmode/runmode.js b/js/codemirror/addon/runmode/runmode.js new file mode 100644 index 0000000000..a7da6d718f --- /dev/null +++ b/js/codemirror/addon/runmode/runmode.js @@ -0,0 +1,56 @@ +CodeMirror.runMode = function(string, modespec, callback, options) { + var mode = CodeMirror.getMode(CodeMirror.defaults, modespec); + var ie = /MSIE \d/.test(navigator.userAgent); + var ie_lt9 = ie && (document.documentMode == null || document.documentMode < 9); + + if (callback.nodeType == 1) { + var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize; + var node = callback, col = 0; + node.innerHTML = ""; + callback = function(text, style) { + if (text == "\n") { + // Emitting LF or CRLF on IE8 or earlier results in an incorrect display. + // Emitting a carriage return makes everything ok. + node.appendChild(document.createTextNode(ie_lt9 ? '\r' : text)); + col = 0; + return; + } + var content = ""; + // replace tabs + for (var pos = 0;;) { + var idx = text.indexOf("\t", pos); + if (idx == -1) { + content += text.slice(pos); + col += text.length - pos; + break; + } else { + col += idx - pos; + content += text.slice(pos, idx); + var size = tabSize - col % tabSize; + col += size; + for (var i = 0; i < size; ++i) content += " "; + pos = idx + 1; + } + } + + if (style) { + var sp = node.appendChild(document.createElement("span")); + sp.className = "cm-" + style.replace(/ +/g, " cm-"); + sp.appendChild(document.createTextNode(content)); + } else { + node.appendChild(document.createTextNode(content)); + } + }; + } + + var lines = CodeMirror.splitLines(string), state = CodeMirror.startState(mode); + for (var i = 0, e = lines.length; i < e; ++i) { + if (i) callback("\n"); + var stream = new CodeMirror.StringStream(lines[i]); + while (!stream.eol()) { + var style = mode.token(stream, state); + callback(stream.current(), style, i, stream.start); + stream.start = stream.pos; + } + } +}; |