Skip to content

Commit 62a9763

Browse files
committed
Inline AnsiUp
1 parent edfd887 commit 62a9763

File tree

3 files changed

+318
-2
lines changed

3 files changed

+318
-2
lines changed

dlang.org.ddoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ COMMON_SCRIPTS =
3636
_=
3737
COMMON_SCRIPTS_DLANG =
3838
$(SCRIPTLOAD $(STATIC js/codemirror-compressed.js))
39-
$(SCRIPTLOAD $(STATIC js/ansi_up.min.js))
4039
$(SCRIPTLOAD $(STATIC js/run.js))
4140
_=
4241

js/run.js

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,323 @@ var nl2br = function()
4747
return this.replace(/\n/g, "<br>");
4848
}
4949

50+
/* ansi_up.js
51+
* author : Dru Nelson
52+
* license : MIT
53+
* http://github.com/drudru/ansi_up
54+
*/
55+
function rgx(tmplObj) {
56+
var subst = [];
57+
for (var _i = 1; _i < arguments.length; _i++) {
58+
subst[_i - 1] = arguments[_i];
59+
}
60+
var regexText = tmplObj.raw[0];
61+
var wsrgx = /^\s+|\s+\n|\s+#[\s\S]+?\n/gm;
62+
var txt2 = regexText.replace(wsrgx, '');
63+
return new RegExp(txt2, 'm');
64+
}
65+
var AnsiUp = (function () {
66+
function AnsiUp() {
67+
this.VERSION = "2.0.2";
68+
this.ansi_colors = [
69+
[
70+
{ rgb: [0, 0, 0], class_name: "ansi-black" },
71+
{ rgb: [187, 0, 0], class_name: "ansi-red" },
72+
{ rgb: [0, 187, 0], class_name: "ansi-green" },
73+
{ rgb: [187, 187, 0], class_name: "ansi-yellow" },
74+
{ rgb: [0, 0, 187], class_name: "ansi-blue" },
75+
{ rgb: [187, 0, 187], class_name: "ansi-magenta" },
76+
{ rgb: [0, 187, 187], class_name: "ansi-cyan" },
77+
{ rgb: [255, 255, 255], class_name: "ansi-white" }
78+
],
79+
[
80+
{ rgb: [85, 85, 85], class_name: "ansi-bright-black" },
81+
{ rgb: [255, 85, 85], class_name: "ansi-bright-red" },
82+
{ rgb: [0, 255, 0], class_name: "ansi-bright-green" },
83+
{ rgb: [255, 255, 85], class_name: "ansi-bright-yellow" },
84+
{ rgb: [85, 85, 255], class_name: "ansi-bright-blue" },
85+
{ rgb: [255, 85, 255], class_name: "ansi-bright-magenta" },
86+
{ rgb: [85, 255, 255], class_name: "ansi-bright-cyan" },
87+
{ rgb: [255, 255, 255], class_name: "ansi-bright-white" }
88+
]
89+
];
90+
this.htmlFormatter = {
91+
transform: function (fragment, instance) {
92+
var txt = fragment.text;
93+
if (txt.length === 0)
94+
return txt;
95+
if (instance._escape_for_html)
96+
txt = instance.old_escape_for_html(txt);
97+
if (!fragment.bright && fragment.fg === null && fragment.bg === null)
98+
return txt;
99+
var styles = [];
100+
var classes = [];
101+
var fg = fragment.fg;
102+
var bg = fragment.bg;
103+
if (fg === null && fragment.bright)
104+
fg = instance.ansi_colors[1][7];
105+
if (!instance._use_classes) {
106+
if (fg)
107+
styles.push("color:rgb(" + fg.rgb.join(',') + ")");
108+
if (bg)
109+
styles.push("background-color:rgb(" + bg.rgb + ")");
110+
}
111+
else {
112+
if (fg) {
113+
if (fg.class_name !== 'truecolor') {
114+
classes.push(fg.class_name + "-fg");
115+
}
116+
else {
117+
styles.push("color:rgb(" + fg.rgb.join(',') + ")");
118+
}
119+
}
120+
if (bg) {
121+
if (bg.class_name !== 'truecolor') {
122+
classes.push(bg.class_name + "-bg");
123+
}
124+
else {
125+
styles.push("background-color:rgb(" + bg.rgb.join(',') + ")");
126+
}
127+
}
128+
}
129+
var class_string = '';
130+
var style_string = '';
131+
if (classes.length)
132+
class_string = " class=\"" + classes.join(' ') + "\"";
133+
if (styles.length)
134+
style_string = " style=\"" + styles.join(';') + "\"";
135+
return "<span" + class_string + style_string + ">" + txt + "</span>";
136+
},
137+
compose: function (segments, instance) {
138+
return segments.join("");
139+
}
140+
};
141+
this.textFormatter = {
142+
transform: function (fragment, instance) {
143+
return fragment.text;
144+
},
145+
compose: function (segments, instance) {
146+
return segments.join("");
147+
}
148+
};
149+
this.setup_256_palette();
150+
this._use_classes = false;
151+
this._escape_for_html = true;
152+
this.bright = false;
153+
this.fg = this.bg = null;
154+
this._buffer = '';
155+
}
156+
Object.defineProperty(AnsiUp.prototype, "use_classes", {
157+
get: function () {
158+
return this._use_classes;
159+
},
160+
set: function (arg) {
161+
this._use_classes = arg;
162+
},
163+
enumerable: true,
164+
configurable: true
165+
});
166+
Object.defineProperty(AnsiUp.prototype, "escape_for_html", {
167+
get: function () {
168+
return this._escape_for_html;
169+
},
170+
set: function (arg) {
171+
this._escape_for_html = arg;
172+
},
173+
enumerable: true,
174+
configurable: true
175+
});
176+
AnsiUp.prototype.setup_256_palette = function () {
177+
var _this = this;
178+
this.palette_256 = [];
179+
this.ansi_colors.forEach(function (palette) {
180+
palette.forEach(function (rec) {
181+
_this.palette_256.push(rec);
182+
});
183+
});
184+
var levels = [0, 95, 135, 175, 215, 255];
185+
for (var r = 0; r < 6; ++r) {
186+
for (var g = 0; g < 6; ++g) {
187+
for (var b = 0; b < 6; ++b) {
188+
var col = { rgb: [levels[r], levels[g], levels[b]], class_name: 'truecolor' };
189+
this.palette_256.push(col);
190+
}
191+
}
192+
}
193+
var grey_level = 8;
194+
for (var i = 0; i < 24; ++i, grey_level += 10) {
195+
var gry = { rgb: [grey_level, grey_level, grey_level], class_name: 'truecolor' };
196+
this.palette_256.push(gry);
197+
}
198+
};
199+
AnsiUp.prototype.old_escape_for_html = function (txt) {
200+
return txt.replace(/[&<>]/gm, function (str) {
201+
if (str === "&")
202+
return "&amp;";
203+
if (str === "<")
204+
return "&lt;";
205+
if (str === ">")
206+
return "&gt;";
207+
});
208+
};
209+
AnsiUp.prototype.old_linkify = function (txt) {
210+
return txt.replace(/(https?:\/\/[^\s]+)/gm, function (str) {
211+
return "<a href=\"" + str + "\">" + str + "</a>";
212+
});
213+
};
214+
AnsiUp.prototype.detect_incomplete_ansi = function (txt) {
215+
return !(/.*?[\x40-\x7e]/.test(txt));
216+
};
217+
AnsiUp.prototype.detect_incomplete_link = function (txt) {
218+
var found = false;
219+
for (var i = txt.length - 1; i > 0; i--) {
220+
if (/\s|\x1B/.test(txt[i])) {
221+
found = true;
222+
break;
223+
}
224+
}
225+
if (!found) {
226+
if (/(https?:\/\/[^\s]+)/.test(txt))
227+
return 0;
228+
else
229+
return -1;
230+
}
231+
var prefix = txt.substr(i + 1, 4);
232+
if (prefix.length === 0)
233+
return -1;
234+
if ("http".indexOf(prefix) === 0)
235+
return (i + 1);
236+
};
237+
AnsiUp.prototype.ansi_to = function (txt, formatter) {
238+
var pkt = this._buffer + txt;
239+
this._buffer = '';
240+
var raw_text_pkts = pkt.split(/\x1B\[/);
241+
if (raw_text_pkts.length === 1)
242+
raw_text_pkts.push('');
243+
this.handle_incomplete_sequences(raw_text_pkts);
244+
var first_chunk = this.with_state(raw_text_pkts.shift());
245+
var blocks = new Array(raw_text_pkts.length);
246+
for (var i = 0, len = raw_text_pkts.length; i < len; ++i) {
247+
blocks[i] = (formatter.transform(this.process_ansi(raw_text_pkts[i]), this));
248+
}
249+
if (first_chunk.text.length > 0)
250+
blocks.unshift(formatter.transform(first_chunk, this));
251+
return formatter.compose(blocks, this);
252+
};
253+
AnsiUp.prototype.ansi_to_html = function (txt) {
254+
return this.ansi_to(txt, this.htmlFormatter);
255+
};
256+
AnsiUp.prototype.ansi_to_text = function (txt) {
257+
return this.ansi_to(txt, this.textFormatter);
258+
};
259+
AnsiUp.prototype.with_state = function (text) {
260+
return { bright: this.bright, fg: this.fg, bg: this.bg, text: text };
261+
};
262+
AnsiUp.prototype.handle_incomplete_sequences = function (chunks) {
263+
var last_chunk = chunks[chunks.length - 1];
264+
if ((last_chunk.length > 0) && this.detect_incomplete_ansi(last_chunk)) {
265+
this._buffer = "\x1B[" + last_chunk;
266+
chunks.pop();
267+
chunks.push('');
268+
}
269+
else {
270+
if (last_chunk.slice(-1) === "\x1B") {
271+
this._buffer = "\x1B";
272+
console.log("raw", chunks);
273+
chunks.pop();
274+
chunks.push(last_chunk.substr(0, last_chunk.length - 1));
275+
console.log(chunks);
276+
console.log(last_chunk);
277+
}
278+
if (chunks.length === 2 &&
279+
chunks[1] === "" &&
280+
chunks[0].slice(-1) === "\x1B") {
281+
this._buffer = "\x1B";
282+
last_chunk = chunks.shift();
283+
chunks.unshift(last_chunk.substr(0, last_chunk.length - 1));
284+
}
285+
}
286+
};
287+
AnsiUp.prototype.process_ansi = function (block) {
288+
if (!this._sgr_regex) {
289+
this._sgr_regex = (_a = ["\n ^ # beginning of line\n ([!<-?]?) # a private-mode char (!, <, =, >, ?)\n ([d;]*) # any digits or semicolons\n ([ -/]? # an intermediate modifier\n [@-~]) # the command\n ([sS]*) # any text following this CSI sequence\n "], _a.raw = ["\n ^ # beginning of line\n ([!\\x3c-\\x3f]?) # a private-mode char (!, <, =, >, ?)\n ([\\d;]*) # any digits or semicolons\n ([\\x20-\\x2f]? # an intermediate modifier\n [\\x40-\\x7e]) # the command\n ([\\s\\S]*) # any text following this CSI sequence\n "], rgx(_a));
290+
}
291+
var matches = block.match(this._sgr_regex);
292+
if (!matches) {
293+
return this.with_state(block);
294+
}
295+
var orig_txt = matches[4];
296+
if (matches[1] !== '' || matches[3] !== 'm') {
297+
return this.with_state(orig_txt);
298+
}
299+
var sgr_cmds = matches[2].split(';');
300+
while (sgr_cmds.length > 0) {
301+
var sgr_cmd_str = sgr_cmds.shift();
302+
var num = parseInt(sgr_cmd_str, 10);
303+
if (isNaN(num) || num === 0) {
304+
this.fg = this.bg = null;
305+
this.bright = false;
306+
}
307+
else if (num === 1) {
308+
this.bright = true;
309+
}
310+
else if (num === 22) {
311+
this.bright = false;
312+
}
313+
else if (num === 39) {
314+
this.fg = null;
315+
}
316+
else if (num === 49) {
317+
this.bg = null;
318+
}
319+
else if ((num >= 30) && (num < 38)) {
320+
var bidx = this.bright ? 1 : 0;
321+
this.fg = this.ansi_colors[bidx][(num - 30)];
322+
}
323+
else if ((num >= 90) && (num < 98)) {
324+
this.fg = this.ansi_colors[1][(num - 90)];
325+
}
326+
else if ((num >= 40) && (num < 48)) {
327+
this.bg = this.ansi_colors[0][(num - 40)];
328+
}
329+
else if ((num >= 100) && (num < 108)) {
330+
this.bg = this.ansi_colors[1][(num - 100)];
331+
}
332+
else if (num === 38 || num === 48) {
333+
if (sgr_cmds.length > 0) {
334+
var is_foreground = (num === 38);
335+
var mode_cmd = sgr_cmds.shift();
336+
if (mode_cmd === '5' && sgr_cmds.length > 0) {
337+
var palette_index = parseInt(sgr_cmds.shift(), 10);
338+
if (palette_index >= 0 && palette_index <= 255) {
339+
if (is_foreground)
340+
this.fg = this.palette_256[palette_index];
341+
else
342+
this.bg = this.palette_256[palette_index];
343+
}
344+
}
345+
if (mode_cmd === '2' && sgr_cmds.length > 2) {
346+
var r = parseInt(sgr_cmds.shift(), 10);
347+
var g = parseInt(sgr_cmds.shift(), 10);
348+
var b = parseInt(sgr_cmds.shift(), 10);
349+
if ((r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255)) {
350+
var c = { rgb: [r, g, b], class_name: 'truecolor' };
351+
if (is_foreground)
352+
this.fg = c;
353+
else
354+
this.bg = c;
355+
}
356+
}
357+
}
358+
}
359+
}
360+
return this.with_state(orig_txt);
361+
var _a;
362+
};
363+
return AnsiUp;
364+
}());
365+
// end ansi_up
366+
50367
var ansi_up = new AnsiUp;
51368
ansi_up.use_classes = true;
52369

posix.mak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ IMAGES=favicon.ico $(ORGS_USING_D) $(addprefix images/, \
297297

298298
JAVASCRIPT=$(addsuffix .js, $(addprefix js/, \
299299
codemirror-compressed dlang ddox listanchors platform-downloads run \
300-
run_examples show_contributors jquery-1.7.2.min ansi_up.min))
300+
run_examples show_contributors jquery-1.7.2.min))
301301

302302
STYLES=$(addsuffix .css, $(addprefix css/, \
303303
style print codemirror ddox))

0 commit comments

Comments
 (0)