##// END OF EJS Templates
Backport PR #6653: Fix IPython.utils.ansispan() to ignore stray [0m...
Min RK -
Show More
@@ -0,0 +1,23 b''
1 casper.notebook_test(function () {
2 var input = [
3 "\033[0m[\033[0minfo\033[0m] \033[0mtext\033[0m",
4 "\033[0m[\033[33mwarn\033[0m] \033[0m\tmore text\033[0m",
5 "\033[0m[\033[33mwarn\033[0m] \033[0m https://some/url/to/a/file.ext\033[0m",
6 "\033[0m[\033[31merror\033[0m] \033[0m\033[0m",
7 "\033[0m[\033[31merror\033[0m] \033[0m\teven more text\033[0m",
8 "\033[0m[\033[31merror\033[0m] \033[0m\t\tand more more text\033[0m"].join("\n");
9
10 var output = [
11 "[info] text",
12 "[<span class=\"ansiyellow\">warn</span>] \tmore text",
13 "[<span class=\"ansiyellow\">warn</span>] https://some/url/to/a/file.ext",
14 "[<span class=\"ansired\">error</span>] ",
15 "[<span class=\"ansired\">error</span>] \teven more text",
16 "[<span class=\"ansired\">error</span>] \t\tand more more text"].join("\n");
17
18 var result = this.evaluate(function (input) {
19 return IPython.utils.fixConsole(input);
20 }, input);
21
22 this.test.assertEquals(result, output, "IPython.utils.fixConsole() handles [0m correctly");
23 });
@@ -290,27 +290,36 b' IPython.utils = (function (IPython) {'
290 function ansispan(str) {
290 function ansispan(str) {
291 // ansispan function adapted from github.com/mmalecki/ansispan (MIT License)
291 // ansispan function adapted from github.com/mmalecki/ansispan (MIT License)
292 // regular ansi escapes (using the table above)
292 // regular ansi escapes (using the table above)
293 var is_open = false
293 return str.replace(/\033\[(0?[01]|22|39)?([;\d]+)?m/g, function(match, prefix, pattern) {
294 return str.replace(/\033\[(0?[01]|22|39)?([;\d]+)?m/g, function(match, prefix, pattern) {
294 if (!pattern) {
295 if (!pattern) {
295 // [(01|22|39|)m close spans
296 // [(01|22|39|)m close spans
296 return "</span>";
297 if (is_open) {
297 }
298 is_open = false;
298 // consume sequence of color escapes
299 return "</span>";
299 var numbers = pattern.match(/\d+/g);
300 } else {
300 var attrs = {};
301 return "";
301 while (numbers.length > 0) {
302 }
302 _process_numbers(attrs, numbers);
303 } else {
303 }
304 is_open = true;
304
305
305 var span = "<span ";
306 // consume sequence of color escapes
306 for (var attr in attrs) {
307 var numbers = pattern.match(/\d+/g);
307 var value = attrs[attr];
308 var attrs = {};
308 span = span + " " + attr + '="' + attrs[attr] + '"';
309 while (numbers.length > 0) {
310 _process_numbers(attrs, numbers);
311 }
312
313 var span = "<span ";
314 for (var attr in attrs) {
315 var value = attrs[attr];
316 span = span + " " + attr + '="' + attrs[attr] + '"';
317 }
318 return span + ">";
309 }
319 }
310 return span + ">";
311 });
320 });
312 };
321 };
313
322
314 // Transform ANSI color escape codes into HTML <span> tags with css
323 // Transform ANSI color escape codes into HTML <span> tags with css
315 // classes listed in the above ansi_colormap object. The actual color used
324 // classes listed in the above ansi_colormap object. The actual color used
316 // are set in the css file.
325 // are set in the css file.
@@ -1,13 +1,12 b''
1 /* CSS font colors for translated ANSI colors. */
1 /* CSS font colors for translated ANSI colors. */
2
2
3
4 .ansibold {font-weight: bold;}
3 .ansibold {font-weight: bold;}
5
4
6 /* use dark versions for foreground, to improve visibility */
5 /* use dark versions for foreground, to improve visibility */
7 .ansiblack {color: black;}
6 .ansiblack {color: black;}
8 .ansired {color: darkred;}
7 .ansired {color: darkred;}
9 .ansigreen {color: darkgreen;}
8 .ansigreen {color: darkgreen;}
10 .ansiyellow {color: brown;}
9 .ansiyellow {color: #c4a000;}
11 .ansiblue {color: darkblue;}
10 .ansiblue {color: darkblue;}
12 .ansipurple {color: darkviolet;}
11 .ansipurple {color: darkviolet;}
13 .ansicyan {color: steelblue;}
12 .ansicyan {color: steelblue;}
@@ -22,4 +21,3 b''
22 .ansibgpurple {background-color: magenta;}
21 .ansibgpurple {background-color: magenta;}
23 .ansibgcyan {background-color: cyan;}
22 .ansibgcyan {background-color: cyan;}
24 .ansibggray {background-color: gray;}
23 .ansibggray {background-color: gray;}
25
@@ -31,7 +31,7 b' div.traceback-wrapper{text-align:left;max-width:800px;margin:auto}'
31 .ansiblack{color:#000}
31 .ansiblack{color:#000}
32 .ansired{color:#8b0000}
32 .ansired{color:#8b0000}
33 .ansigreen{color:#006400}
33 .ansigreen{color:#006400}
34 .ansiyellow{color:#a52a2a}
34 .ansiyellow{color:#c4a000}
35 .ansiblue{color:#00008b}
35 .ansiblue{color:#00008b}
36 .ansipurple{color:#9400d3}
36 .ansipurple{color:#9400d3}
37 .ansicyan{color:#4682b4}
37 .ansicyan{color:#4682b4}
@@ -1336,7 +1336,7 b' input.engine_num_input{width:60px}'
1336 .ansiblack{color:#000}
1336 .ansiblack{color:#000}
1337 .ansired{color:#8b0000}
1337 .ansired{color:#8b0000}
1338 .ansigreen{color:#006400}
1338 .ansigreen{color:#006400}
1339 .ansiyellow{color:#a52a2a}
1339 .ansiyellow{color:#c4a000}
1340 .ansiblue{color:#00008b}
1340 .ansiblue{color:#00008b}
1341 .ansipurple{color:#9400d3}
1341 .ansipurple{color:#9400d3}
1342 .ansicyan{color:#4682b4}
1342 .ansicyan{color:#4682b4}
General Comments 0
You need to be logged in to leave comments. Login now