]>
Commit | Line | Data |
---|---|---|
dbd61667 | 1 | function Colourise(line, entity, execfn) { |
9e769c12 CP |
2 | var fg; |
3 | var bg; | |
4 | var underline = false; | |
5 | var bold = false; | |
6 | ||
7 | var out = []; | |
8 | var xline = line.split(""); | |
9 | var element = document.createElement("span"); | |
10 | ||
11 | entity.addClass("colourline"); | |
12 | ||
13 | function isNum(x) { | |
14 | return x >= '0' && x <= '9'; | |
15 | } | |
16 | ||
17 | function parseColours(xline, i) { | |
18 | if(!isNum(xline[i + 1])) { | |
19 | fg = undefined; | |
20 | bg = undefined; | |
21 | return i; | |
22 | } | |
23 | i++; | |
24 | if(isNum(xline[i + 1])) { | |
25 | fg = parseInt(xline[i] + xline[i + 1]); | |
26 | i++; | |
27 | } else { | |
28 | fg = parseInt(xline[i]); | |
29 | } | |
30 | if(xline[i + 1] != ",") | |
31 | return i; | |
32 | if(!isNum(xline[i + 2])) | |
33 | return i; | |
34 | i+=2; | |
35 | ||
36 | if(isNum(xline[i + 1])) { | |
37 | bg = parseInt(xline[i] + xline[i + 1]); | |
38 | i++; | |
39 | } else { | |
40 | bg = parseInt(xline[i]); | |
41 | } | |
42 | return i; | |
43 | } | |
44 | ||
45 | function emitEndToken() { | |
46 | if(out.length > 0) { | |
dbd61667 | 47 | urlificate(element, out.join(""), execfn); |
9e769c12 CP |
48 | entity.appendChild(element); |
49 | out = []; | |
50 | } | |
51 | element = document.createElement("span"); | |
52 | } | |
53 | function emitStartToken() { | |
54 | classes = [] | |
55 | if(fg != undefined) | |
56 | classes.push("Xc" + fg); | |
57 | if(bg != undefined) | |
58 | classes.push("Xbc" + bg); | |
59 | if(bold) | |
60 | classes.push("Xb"); | |
61 | if(underline) | |
62 | classes.push("Xu"); | |
63 | element.className = classes.join(" "); | |
64 | } | |
65 | ||
66 | for(i=0;i<xline.length;i++) { | |
67 | var lc = xline[i]; | |
68 | if(lc == "\x02") { | |
69 | emitEndToken(); | |
70 | ||
71 | bold = !bold; | |
72 | ||
73 | emitStartToken(); | |
74 | } else if(lc == "\x1F") { | |
75 | emitEndToken(); | |
76 | ||
77 | underline = !underline; | |
78 | ||
79 | emitStartToken(); | |
80 | } else if(lc == "\x0F") { | |
81 | emitEndToken(); | |
82 | ||
83 | fg = undefined; | |
84 | bg = undefined; | |
85 | underline = false; | |
86 | bold = false; | |
87 | } else if(lc == "\x03") { | |
88 | emitEndToken(); | |
89 | ||
90 | i = parseColours(xline, i); | |
91 | if(bg > 15) | |
92 | bg = undefined; | |
93 | if(fg > 15) | |
94 | fg = undefined; | |
95 | ||
96 | emitStartToken(); | |
97 | } else { | |
98 | out.push(lc); | |
99 | } | |
100 | } | |
101 | ||
102 | emitEndToken(); | |
c3efd9cc | 103 | } |