-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
192 lines (156 loc) · 4.97 KB
/
main.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
window.escapeText = function(text) {
return text
.replace(/&/g,"&")
.replace(/</g,"<")
.replace(/>/g,">")
.replace(/"/g,""")
.replace(/'/g,"'");
};
window.escapeChar = function(text) {
if (text.length == 1) {
return window.escapeText(text);
} else {
return text;
}
};
window.parseRules = function(rules) {
if (Array.isArray(rules)) {
rules = {"start": rules};
}
for (var i = 0;i < Object.keys(rules).length;i++) {
var thisState = Object.keys(rules)[i];
var thisRules = rules[thisState];
for (var j = 0;j < thisRules.length;j++) {
if (thisRules[j].defaultToken !== undefined) {
continue;
}
if (thisRules[j].regex === undefined || thisRules[j].token === undefined) {
rules[thisState].splice(j,1);
j--;
continue;
}
if ((thisRules[j].token === null || thisRules[j].token === undefined) && thisRules[j].tokenList !== undefined) {
rules[thisState][j].token = thisRules[j].tokenList;
}
if ((thisRules[j].token === null || thisRules[j].token === undefined) && thisRules[j].tokenArray !== undefined) {
rules[thisState][j].token = thisRules[j].tokenArray;
}
if (thisRules[j].next === null && thisRules[j].nextState !== undefined) {
rules[thisState][j].next = thisRules[j].nextState;
}
if (typeof thisRules[j].regex == "string") {
rules[thisState][j].regex = (new RegExp(thisRules[j].regex)).valueOf();
}
var regex = new RegExp(rules[thisState][j].regex);
regex.global = false;
regex.ignoreCase = rules[thisState][j].caseSensitive === undefined ? regex.ignoreCase : !(rules[thisState][j].caseSensitive);
regex.multiline = false;
regex.dotAll = false;
regex.unicode = false;
regex.sticky = false;
rules[thisState][j].regex = regex;
}
}
return rules;
};
window.addClasses = function(match,token,prefix) {
if (typeof match == "string") {
match = [match];
}
if (typeof token == "string") {
var tokens = token.split(".");
for (var i = 0;i < tokens.length;i++) {
tokens[i] = prefix + tokens[i];
}
return "<span class=\"" + window.escapeText(tokens.join(" ")) + "\">" + window.escapeText(match[0]) + "</span>";
} else if (Array.isArray(token)) {
var text = "";
for (var j = 1;j <= token.length;j++) {
text += window.addClasses(match[j],token[j - 1],prefix);
}
return text;
} else if (typeof token == "function") {
return window.addClasses(match,token(match),prefix);
}
};
window.compress = function(html) {
var matcher =
/(<span class="([^"]+)">)((?:[^<>"'&]|<|>|"|'|&)+)<\/span>((?:<span class="\2">(?:(?:[^<>"'&]|<|>|"|'|&)+)<\/span>)+)/g;
var nonglobal =
/(<span class="([^"]+)">)((?:[^<>"'&]|<|>|"|'|&)+)<\/span>((?:<span class="\2">(?:(?:[^<>"'&]|<|>|"|'|&)+)<\/span>)+)/;
var splitter = /<\/span><span class="[^"]+">/g;
return html.replace(matcher,function(m) {
var match = (typeof m == "string" ? m : m[0]).match(nonglobal.valueOf());
var start = match[1];
var classes = match[2];
var chars = match[4];
chars = chars.substr(start.length,chars.length - start.length - "</span>".length);
chars = chars.split(splitter);
return start + match[3] + chars.join("") + "</span>";
});
};
window.changeSpaces = function(html) {
var matcher = / {2,}/g;
return html.replace(matcher,function(m) {
if (typeof m == "object") {
m = m[0];
}
var txt = "‌";
for (var i = 0;i < m.length;i++) {
txt += " ‌";
}
return txt;
});
};
window.highlightLine = function(line,allRules,state,prefix) {
rules = allRules[state];
if (line === "") {
return {
html: "",
state: state
};
}
var span = line.substr(0,1);
var other = line.substr(1);
var matching = true;
for (var i = 0;i < rules.length;i++) {
if (!matching) {
break;
}
if (rules[i].regex !== undefined && rules[i].token !== undefined) {
var match = line.match(rules[i].regex);
if (match !== null && match.index === 0) {
span = addClasses(match,rules[i].token,prefix);
if (rules[i].next !== undefined) {
state = rules[i].next;
}
other = line.substr(match[0].length);
matching = false;
}
} else if (rules[i].defaultToken !== undefined) {
span = addClasses(span,rules[i].defaultToken,prefix);
if (rules[i].next !== undefined) {
state = rules[i].next;
}
matching = false;
}
}
other = window.highlightLine(other,allRules,state,prefix);
return {
html: window.escapeChar(span) + other.html,
state: other.state
};
};
window.highlight = function(text,rules,prefix) {
prefix = prefix || "zsnout_";
text = text.split("\n");
var html = [];
var state = "start";
rules = window.parseRules(rules);
for (var i = 0;i < text.length;i++) {
var line = window.highlightLine(text[i],rules,state,prefix);
html.push(line.html);
state = line.state;
}
return window.changeSpaces(window.compress(html.join("<br>")));
};