/
index.ts
247 lines (222 loc) · 9.16 KB
/
index.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const enum ACTION {
NOP = -1,
EXIT = -2,
EXIT_ALL = -3,
}
export type Ttoken = [number, string, number];
// Tokenize
export function tok(code: string, language): Ttoken[] {
// the result
var tags = [];
var numTags = 0;
// each element is a pattern object from language
var patternStack = [];
// the current position within inputString
var pos = 0;
// the name of the current style, or null if there is no current style
var currentStyle = null;
var output = function(s, style) {
var length = s.length;
// this is more than just an optimization - we don't want to output empty <span></span> elements
if (length === 0) {
return;
}
if (! style) {
var stackLength = patternStack.length;
if (stackLength !== 0) {
var pattern = patternStack[stackLength - 1];
// check whether this is a state or an environment
if (! pattern[3]) {
// it's not a state - it's an environment; use the style for this environment
style = pattern[1];
}
}
}
if (currentStyle !== style) {
if (currentStyle) {
tags[numTags - 1].push(pos);
}
if (style) {
tags[numTags++] = [pos, style];
}
}
pos += length;
currentStyle = style;
};
var endOfLinePattern = /\r\n|\r|\n/g;
endOfLinePattern.lastIndex = 0;
var inputStringLength = code.length;
while (pos < inputStringLength) {
var start = pos;
var end;
var startOfNextLine;
var endOfLineMatch = endOfLinePattern.exec(code);
if (endOfLineMatch === null) {
end = inputStringLength;
startOfNextLine = inputStringLength;
}
else {
end = endOfLineMatch.index;
startOfNextLine = endOfLinePattern.lastIndex;
}
var line = code.substring(start, end);
var matchCache = [];
for (;;) {
var posWithinLine = pos - start;
var stateIndex;
var stackLength = patternStack.length;
if (stackLength === 0) {
stateIndex = 0;
}
else {
// get the next state
stateIndex = patternStack[stackLength - 1][2];
}
var state = language[stateIndex];
var numPatterns = state.length;
var mc = matchCache[stateIndex];
if (! mc) {
mc = matchCache[stateIndex] = [];
}
var bestMatch = null;
var bestPatternIndex = -1;
for (var i = 0; i < numPatterns; i++) {
var match;
if (i < mc.length && (mc[i] === null || posWithinLine <= mc[i].index)) {
match = mc[i];
}
else {
var regex = state[i][0];
regex.lastIndex = posWithinLine;
match = regex.exec(line);
mc[i] = match;
}
if (match !== null && (bestMatch === null || match.index < bestMatch.index)) {
bestMatch = match;
bestPatternIndex = i;
if (match.index === posWithinLine) {
break;
}
}
}
if (bestMatch === null) {
output(line.substring(posWithinLine), null);
break;
}
else {
// got a match
if (bestMatch.index > posWithinLine) {
output(line.substring(posWithinLine, bestMatch.index), null);
}
var pattern = state[bestPatternIndex];
var newStyle = pattern[1];
var matchedString;
if (newStyle instanceof Array) {
for (var subexpression = 0; subexpression < newStyle.length; subexpression++) {
matchedString = bestMatch[subexpression + 1];
output(matchedString, newStyle[subexpression]);
}
}
else {
matchedString = bestMatch[0];
output(matchedString, newStyle);
}
switch (pattern[2]) {
case ACTION.NOP:
// do nothing
break;
case ACTION.EXIT:
// exit
patternStack.pop();
break;
case ACTION.EXIT_ALL:
// exitall
patternStack.length = 0;
break;
default:
// this was the start of a delimited pattern or a state/environment
patternStack.push(pattern);
break;
}
}
}
// end of the line
if (currentStyle) {
tags[numTags - 1].push(pos);
currentStyle = null;
}
pos = startOfNextLine;
}
return tags;
}
// Convert to JsonML tree
export function jml(code: string, tokens: Ttoken[], classAttr = 'class', prefix = 'hl-', tag = 'span', position = false): any[] {
var pos = 0;
var list = [];
for(var token of tokens) {
var [start, style, end] = token;
if(pos < start) {
list.push(code.substring(pos, start));
}
// JsonML node
var attributes = {[classAttr]: prefix + style};
if(position) {
attributes['data-pos'] = `${start},${end}`;
}
list.push([tag, attributes, code.substring(start, end)]);
pos = end;
}
if(pos < code.length) {
list.push(code.substring(pos));
}
return list;
}
export function css(root = '.hl-desigual', prefix = 'hl-') {
prefix = '.' + prefix;
return {
[root]: {
color: 'black',
'font-style': 'normal',
'font-weight': 'normal',
[prefix + 'keyword']: {color: 'blue', 'font-weight': 'bold'},
[prefix + 'type']: {color: 'darkgreen'},
[prefix + 'usertype']: {color: 'teal'},
[prefix + 'string']: {color: 'red', 'font-family': 'monospace'},
[prefix + 'regexp']: {color: 'orange', 'font-family': 'monospace'},
[prefix + 'specialchar']: {color: 'pink', 'font-family': 'monospace'},
[prefix + 'comment']: {color: 'brown', 'font-style': 'italic'},
[prefix + 'number']: {color: 'purple'},
[prefix + 'preproc']: {color: 'darkblue', 'font-weight': 'bold'},
[prefix + 'symbol']: {color: 'darkred'},
[prefix + 'function']: {color: 'black', 'font-weight': 'bold'},
[prefix + 'cbracket']: {color: 'red'},
[prefix + 'todo']: {'font-weight': 'bold', 'background-color': 'cyan'},
[prefix + 'predef_var']: {color: 'darkblue'},
[prefix + 'predef_func']: {color: 'darkblue', 'font-weight': 'bold'},
[prefix + 'classname']: {color: 'teal'},
[prefix + 'url']: {color: 'blue', 'font-family': 'monospace', 'text-decoration': 'underline'},
[prefix + 'date']: {color: 'blue', 'font-weight': 'bold'},
[prefix + 'time']: {color: 'darkblue', 'font-weight': 'bold'},
[prefix + 'file']: {color: 'darkblue', 'font-weight': 'bold'},
[prefix + 'ip']: {color: 'darkgreen'},
[prefix + 'name']: {color: 'darkgreen'},
[prefix + 'variable']: {color: 'darkgreen'},
[prefix + 'italics']: {color: 'darkgreen', 'font-style': 'italic'},
[prefix + 'bold']: {color: 'darkgreen', 'font-weight': 'bold'},
[prefix + 'underline']: {color: 'darkgreen', 'font-decoration': 'underline'},
[prefix + 'fixed']: {color: 'green', 'font-family': 'monospace'},
[prefix + 'argument']: {color: 'darkgreen'},
[prefix + 'optionalargument']: {color: 'purple'},
[prefix + 'math']: {color: 'orange'},
[prefix + 'bibtex']: {color: 'blue'},
[prefix + 'newfile']: {color: 'darkgreen'},
[prefix + 'difflines']: {color: 'blue'},
[prefix + 'selector']: {color: 'purple'},
[prefix + 'property']: {color: 'blue'},
[prefix + 'value']: {color: 'darkgreen', 'font-style': 'italic'},
[prefix + 'section']: {color: 'black', 'font-weight': 'bold'},
[prefix + 'paren']: {color: 'red'},
[prefix + 'attribute']: {color: 'darkgreen'},
}
};
}