-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight.js
64 lines (58 loc) · 1.96 KB
/
highlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// TODO:
// - add an options page / popup to set theme for editing
~function (doc, $, pollTimeout, editor, textarea, selectExtension, extensions, undefined) {
textarea = $('.file-contents');
selectExtension = $('.js-language-picker');
// FIXME: doesn't CodeMirror accept extensions for modes?
extensions = {
"js": "javascript"
, "css": "css"
, "html": "htmlmixed"
, "txt": "text"
, "c": "clike"
, "clj": "clojure"
, "coffee": "coffeescript"
, "diff": "diff"
, "md": "gfm"
, "go": "go"
, "groovy": "groovy"
, "hs": "haskell"
, "lua": "lua"
, "sql": "mysql"
, "pl": "perl"
, "php": "php"
, "py": "python"
, "r": "r"
, "rb": "ruby"
, "scm": "scheme"
, "sh": "shell"
, "st": "smalltalk"
, "tpl": "smarty"
, "v": "verilog"
, "xml": "xml"
, "xq": "xquery"
, "yml": "yaml"
};
function extToMode (ext) {
return extensions[ext.slice(1)] || "text";
}
function fireEvent (element, event) {
var e = doc.createEvent("HTMLEvents");
e.initEvent(event, true, true);
return !element.dispatchEvent(e);
}
// FIXME: is there really no other option other than polling?
setTimeout( function pollLanguageChange (value) {
if (value !== selectExtension.value) fireEvent(selectExtension, "change");
setTimeout( pollLanguageChange, pollTimeout, selectExtension.value );
}, pollTimeout, selectExtension.value);
// defaults
CodeMirror.defaults.theme = "ambiance";
CodeMirror.defaults.lineNumbers = true;
selectExtension.addEventListener("change", function () {
editor.setOption("mode", extToMode(selectExtension.value));
}, false);
textarea && (editor = CodeMirror.fromTextArea(textarea, {
mode: extToMode(selectExtension.value)
}));
}(document, function (selector) { return document.querySelector(selector); }, 100);