-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.js
More file actions
111 lines (111 loc) · 2.76 KB
/
highlight.js
File metadata and controls
111 lines (111 loc) · 2.76 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
console.clear();
function id(ref){
return document.getElementById(ref);
}
Array.prototype.has = function(item){
return this.indexOf(item) > -1;
};
Object.defineProperty(Array.prototype,'last',{
get:function(){
return this[this.length-1];
},
set:function(val){
this[this.length-1] = val;
}
});
Object.defineProperty(String.prototype,'last',{
get:function(){
return this[this.length-1];
}
});
function ctype(text){
if(white.has(text)) return 'white';
if(group.has(text)) return 'group';
if(char.has(text)) return 'char';
if(op.has(text)) return 'op';
return 'char';
}
function stype(text){
if(group.has(text[0])) return 'group';
if(white.has(text[0])) return 'white';
if(res.has(text)) return 'res';
if(con.has(text)) return 'con';
if(!isNaN(+text)) return 'num';
if(quot.has(text[0]) && quot.has(text.last)) return 'str';
if(op.has(text[0])) return 'op';
return 'var';
}
function parse(raw){
raw = desc(raw);
console.log(raw);
var org = [''];
var state = false;
for(var s in raw){
if(ctype(raw[s]) != ctype(raw[s-1]) && !state){
org.push("");
if(quot.has(raw[s])) state = raw[s];
} else if(state){
if(raw[s] == state){
state = false; console.log(state + ' false');
}
} else if(quot.has(raw[s])){
state = raw[s];
console.log(state + ' set');
}
org.last += raw[s];
}
for(var t in org){
if(org[t].replace) org[t] = spanify(esc(org[t]),stype(org[t]));
}
return org.join('');
}
function desc(raw){
raw = raw.replace(/&/g,'&');
raw = raw.replace(/>/g,'>');
raw = raw.replace(/</g,'<');
return raw;
}
function esc(raw){
raw = raw.replace(/&/g,'&');
raw = raw.replace(/>/g,'>');
raw = raw.replace(/</g,'<');
return raw;
}
function spanify(text,cls){
return '<span class="' + cls + '"' + 'title="' + cls + '" >' + text + '</span>';
}
function codify(text){
text = desc(text);
var state = true;
var out = '';
for(var s in text){
if(text[s] == '`'){
if(state){
out += '<pre><code class="js">';
} else {
out += '</code></pre>';
}
state = !state;
} else {
out += text[s];
}
}
return out;
}
//char types
var white = [' ','\n','\t'];
var char = 'abcdefghijklmnopqrstuvwxyz0123456789$"\''.split('');
var op = '!%^&*-=+\\|:,<.>/?'.split('');
var group = '()[]{};'.split('');
var quot = ["'",'"'];
//string types
var res = /* Reserved words */ ['var','for','while','do','in','function','new','class','if','else','return'];
var con = /* Constants */ ['true','false','undefined','null','NaN','this'];
function render(){
id('lesson').innerHTML = codify(id('lesson').innerHTML);
var codes = document.getElementsByTagName('code');
for(var s in codes){
codes[s].innerHTML = parse(codes[s].innerHTML);
}
}
render();