-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntaxhighlighter.js
182 lines (160 loc) · 7.3 KB
/
syntaxhighlighter.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
var TOKEN_TYPE = {
OPEN_BRACKET: "open bracket",
STRING_DELIMITER: "string delimiter",
WHITE_SPACE: "white space",
OTHER: "other"
};
var PARSER_STATE = {
CONSUMING_TOKENS: {},
CONSUMING_STRING: {}
};
var Token = class Token {
constructor(node, type)
{
this.node = node;
this.type = type;
}
};
var Highlighter = class Highlighter {
constructor()
{
this.highlightedKeywords = {
"red": ["select","union","order", ">", "awk", "aspell", "cron", "gawk", "like", "pro", "rpm", "ssh", "similar", "snort", "top", "alias", "apt", "awk", "bzip2", "cat", "cd", "chmod", "chown", "cmp", "comm", "cp", "cpio", "date", "declare", "df", "echo", "enable", "env", "eval", "exec", "exit", "expect", "export", "find", "for", "free", "ftp", "gawk", "grep", "gzip", "ifconfig", "ifdown", "ifup", "kill", "less", "lft", "ln", "locate", "ls", "man", "mc", "mkdir", "more", "mv", "mysql", "neat", "netconfig", "netstat", "nslookup", "od", "passwd", "ping", "ps", "pwd", "read", "rm", "rsync", "screen", "sdiff", "sed", "shutdown", "slocate", "sort", "ssh", "su", "sudo", "tail", "tar", "top", "tr", "traceroute", "uname", "uniq", "unzip", "vi", "vim", "vmstat", "wc", "wget", "whatis", "whereis", "while", "whoami", "yum"],
"blue": ["null","none","from", "true", "abstract", "and", "as", "assert", "auto", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "def", "default", "del", "delete", "do", "double", "elif", "else", "enum", "except", "export", "extends", "extern", "false", "final", "finally", "float", "for", "from", "function", "global", "goto", "if", "implements", "import", "in", "inline", "instanceof", "int", "interface", "is", "lambda", "let", "long", "native", "new", "nonlocal", "not", "null", "or", "package", "pass", "private", "protected", "public", "raise", "register", "restrict", "return", "short", "signed", "sizeof", "static", "strictfp", "struct", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typedef", "typeof", "union", "unsigned", "var", "void", "volatile", "while", "with", "yield"],
"green": ["in", "by", "is", "up"]
};
this.regexes = [
"[a-z0-9]+://[a-zA-Z0-9-./]+[^\\s]*",
"\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+",
"\\d+\\.\\d+\\.\\d+\\.\\d+"
];
}
run(document)
{
var codeBlocks = document.getElementsByTagName("code");
for (var blocksIndex in codeBlocks)
{
var html = codeBlocks[blocksIndex].innerHTML;
if (html === undefined)
{
continue;
}
var tokens = this.tokenize(html);
var newInnerHtml = "";
var state = PARSER_STATE.CONSUMING_TOKENS;
var code = "";
for (var i = 0; i < tokens.length; i++)
{
var token = tokens[i];
switch (state)
{
case PARSER_STATE.CONSUMING_TOKENS:
switch (token.type)
{
case TOKEN_TYPE.STRING_DELIMITER:
state = PARSER_STATE.CONSUMING_STRING;
code += token.node;
break;
case TOKEN_TYPE.OTHER:
if (this.nextTokenIsOpenBracket(i, tokens))
{
newInnerHtml += "<span class='green'>" + token.node + "</span>";
}
else
{
newInnerHtml += this.highlight(token);
}
break;
default:
newInnerHtml += this.highlight(token);
break;
}
break;
case PARSER_STATE.CONSUMING_STRING:
code += token.node;
switch (token.type)
{
case TOKEN_TYPE.STRING_DELIMITER:
state = PARSER_STATE.CONSUMING_TOKENS;
newInnerHtml += "<span class='green'>" + code + "</span>";
code = '';
break;
default:
break;
}
}
}
codeBlocks[blocksIndex].innerHTML = newInnerHtml + code;
}
}
tokenize(html)
{
var currentWord = "";
var tokens = [];
for (var character in html)
{
var currentChar = html[character];
switch (currentChar)
{
case "(":
this.consume(currentWord, tokens);
currentWord = "";
tokens.push(new Token(currentChar, TOKEN_TYPE.OPEN_BRACKET));
break;
case " ":
this.consume(currentWord, tokens);
currentWord = "";
tokens.push(new Token(currentChar, TOKEN_TYPE.WHITE_SPACE));
break;
case '"':
case "'":
this.consume(currentWord, tokens);
currentWord = "";
tokens.push(new Token(currentChar, TOKEN_TYPE.STRING_DELIMITER));
break;
default:
currentWord += currentChar;
break;
}
}
this.consume(currentWord, tokens);
currentWord = "";
return tokens;
}
consume(currentWord, tokens)
{
if (currentWord.length > 0)
{
tokens.push(new Token(currentWord, TOKEN_TYPE.OTHER));
}
}
nextTokenIsOpenBracket(i, tokens)
{
return i + 1 < tokens.length && tokens[i + 1].type === TOKEN_TYPE.OPEN_BRACKET;
}
highlight(token)
{
for (var regex in this.regexes)
{
if (token.node.match(this.regexes[regex]))
{
return "<span class='blue'>" + token.node + "</span>";
}
}
for (var color in this.highlightedKeywords)
{
for (var keyword in this.highlightedKeywords[color])
{
if (token.node.toLowerCase() === this.highlightedKeywords[color][keyword])
{
return "<span class='" + color + "'>" + token.node + "</span>";
}
}
}
if (token.node ===' ')
{
return ' ';
}
return token.node;
}
};