forked from clemos/try-haxe
-
Notifications
You must be signed in to change notification settings - Fork 5
/
highlighter.js
56 lines (56 loc) · 2.48 KB
/
highlighter.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
(function (console) { "use strict";
var EReg = function(r,opt) {
opt = opt.split("u").join("");
this.r = new RegExp(r,opt);
};
EReg.prototype = {
replace: function(s,by) {
return s.replace(this.r,by);
}
};
var Highlighter = function() { };
Highlighter.main = function() {
var _g = 0;
var _g1 = window.document.getElementsByTagName("code");
while(_g < _g1.length) {
var el = _g1[_g];
++_g;
if(Highlighter.hasClass(el,"prettyprint")) {
if(!Highlighter.hasClass(el,"highlighted")) {
if(Highlighter.hasClass(el,"haxe") || Highlighter.hasClass(el,"js") || Highlighter.hasClass(el,"javascript")) {
el.innerHTML = Highlighter.syntaxHighlight(el.innerHTML);
el.className += " highlighted";
} else if(Highlighter.hasClass(el,"hxml")) {
el.innerHTML = Highlighter.syntaxHighlightHXML(el.innerHTML);
el.className += " highlighted";
}
}
}
}
};
Highlighter.hasClass = function(el,className) {
return el.className.indexOf(className) != -1;
};
Highlighter.syntaxHighlight = function(html) {
var kwds = ["abstract","trace","break","case","cast","class","continue","default","do","dynamic","else","enum","extends","extern","for","function","if","implements","import","in","inline","interface","macro","new","override","package","private","public","return","static","switch","throw","try","typedef","untyped","using","var","while"];
var kwds1 = new EReg("\\b(" + kwds.join("|") + ")\\b","g");
var vals = ["null","true","false","this"];
var vals1 = new EReg("\\b(" + vals.join("|") + ")\\b","g");
var types = new EReg("\\b([A-Z][a-zA-Z0-9]*)\\b","g");
html = kwds1.replace(html,"<span class='kwd'>$1</span>");
html = vals1.replace(html,"<span class='val'>$1</span>");
html = types.replace(html,"<span class='type'>$1</span>");
html = new EReg("(\"[^\"]*\")","g").replace(html,"<span class='str'>$1</span>");
html = new EReg("(//.+?)(\n|$)","g").replace(html,"<span class='cmt'>$1</span>$2");
html = new EReg("(/\\*\\*?(.|\n)+?\\*?\\*/)","g").replace(html,"<span class='cmt'>$1</span>");
return html;
};
Highlighter.syntaxHighlightHXML = function(html) {
html = new EReg("\\b(haxe)\\b","g").replace(html,"<span class='kwd'>$1</span>");
html = new EReg("(\"[^\"]*\")","g").replace(html,"<span class='str'>$1</span>");
html = new EReg("(--?.+?)(\\s)","g").replace(html,"<span class='val'>$1</span>$2");
html = new EReg("(#.+?)(\n|$)","g").replace(html,"<span class='cmt'>$1</span>$2");
return html;
};
Highlighter.main();
})(typeof console != "undefined" ? console : {log:function(){}});