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