This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
prettyprint.js
175 lines (140 loc) · 4.69 KB
/
prettyprint.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
var keywords = "";
var keywordArray = new Array (
"abstract", "event", "new", "struct", "as", "explicit",
"null", "switch", "base", "extern", "object", "this",
"bool", "false", "operator", "throw", "break", "finally",
"out", "true", "byte", "fixed", "override", "try",
"case", "float", "params", "typeof", "catch", "for",
"private", "uint", "char", "foreach", "protected", "ulong",
"checked", "goto", "public", "unchecked", "class", "if",
"readonly", "unsafe", "const", "implicit", "ref", "ushort",
"continue", "in", "return", "using", "decimal", "int",
"sbyte", "virtual", "default", "interface", "sealed", "volatile",
"delegate", "internal", "short", "void", "do", "is",
"sizeof", "while", "double", "lock", "stackalloc", "else",
"long", "static", "enum", "namespace", "string"
);
function paintColors()
{
keywords = "(";
for(n = 0; n < keywordArray.length; n++)
keywords += "\\b" + keywordArray[n] + "\\b|";
keywords += "string)";
var elems = document.getElementsByTagName("xmp");
paintColorsForTags (elems);
elems = document.getElementsByTagName ("pre");
paintColorsForTags (elems);
}
function paintColorsForTags (elems)
{
for(n = elems.length - 1; n >= 0; n--) {
if (elems[n].className == "code-csharp")
format(elems [n], formatCs);
else if (elems[n].className == "code-xml")
format(elems [n], formatXml);
else if (elems[n].className == "code-mdb")
format(elems [n], formatMdb);
else if (elems[n].className == "code-gaim")
format(elems [n], formatGaim);
}
}
function format(node, func)
{
text = node.innerHTML;
div = document.createElement("div");
//var className = node.className;
var className = "formattedcode";
// remove trailing/leading lines
while(text.charAt(0) == "\n" || text.charAt(0) == "\r")
text = text.substr(1);
while(text.charAt(text.length) == "\n" ||
text.charAt(text.length) == "\r")
text = text.splice(0, -1);
div.innerHTML = func(text);
node.parentNode.replaceChild(div, node);
div.className = className;
}
function formatCs(text)
{
var re = / /g;
text = text.replace(re, " ");
re = /<(.*?)>/g;
text = text.replace(re, "<___span style='color:red'_!_$1___/span_!_>");
re = /</g;
text = text.replace(re, "<");
re = />/g;
text = text.replace(re, ">");
re = /___/g;
text = text.replace (re, "<");
re = /_!_/g;
text = text.replace (re, ">");
// cant get this one to work in the good syntax
re = new RegExp("\"((((?!\").)|\\\")*?)\"","g");
text = text.replace(re,"<span style='color:purple'>\"$1\"</span>");
re = /\/\/(((.(?!\"\<\/span\>))|"(((?!").)*)"\<\/span\>)*)(\r|\n|\r\n)/g;
text = text.replace(re,"<span style='color:green'>//$1</span><br/>");
re = new RegExp (keywords,"g");
text = text.replace(re,"<span style='color:blue'>$1</span>");
re = /\t/g;
text = text.replace(re," ");
re = /\n/g;
text = text.replace (re,"<br/>");
div = document.createElement("div");
div.innerHTML = text;
spans = div.getElementsByTagName ("span")
for(i = 0; i < spans.length; i++) {
if (spans [i].parentNode.nodeName == "SPAN") {
spans [i].style.color = "";
}
}
return div.innerHTML;
}
function formatXml (text)
{
var re = / /g;
text = text.replace (re, " ");
re = /\t/g;
text = text.replace (re, " ");
re = /\<\s*(\/?)\s*(.*?)\s*(\/?)\s*\>/g;
text = text.replace (re,"{blue:<$1}{maroon:$2}{blue:$3>}");
re = /{(\w*):(.*?)}/g;
text = text.replace (re,"<span style='color:$1'>$2</span>");
re = /"(.*?)"/g;
text = text.replace (re,"\"<span style='color:purple'>$1</span>\"");
re = /\r\n|\r|\n/g;
text = text.replace (re, "<br/>");
return text;
}
function formatMdb (text)
{
var re = / /g;
text = text.replace (re, " ");
re = /\t/g;
text = text.replace (re, " ");
re = /^\$(.*)/g;
text = text.replace (re, "{blue:\$}{maroon:$1}");
re = /\(mdb\)(.*)/g
text = text.replace (re,"{blue:(mdb)}{maroon:$1}");
re = /{(\w*):(.*?)}/g;
text = text.replace (re,"<span style='color:$1'>$2</span>");
re = /\r\n|\r|\n/g;
text = text.replace (re, "<br/>");
return text;
}
function formatGaim (text)
{
var re = / /g;
text = text.replace (re, " ");
re = /\t/g;
text = text.replace (re, " ");
re = /\(([0-9]+:[0-9]+:[0-9]+)\)?(.*?):\s*(.*?)/g
text = text.replace (re,"{blue:(}{maroon:$1}{blue:)}{red:$2}:$3");
re = /{(\w*):(.*?)}/g;
text = text.replace (re,"<span style='color:$1'>$2</span>");
re = /\r\n|\r|\n/g;
text = text.replace (re, "<br/>");
return text;
}
window.onload = function () {
paintColors();
}