##// END OF EJS Templates
use much faster regexp for ansi coloring...
MinRK -
Show More
@@ -158,10 +158,32 b' IPython.utils = (function (IPython) {'
158 158
159 159 //Map from terminal commands to CSS classes
160 160 var ansi_colormap = {
161 "30":"ansiblack", "31":"ansired",
162 "32":"ansigreen", "33":"ansiyellow",
163 "34":"ansiblue", "35":"ansipurple","36":"ansicyan",
164 "37":"ansigrey", "01":"ansibold"
161 "01":"ansibold",
162 "30":"ansiblack",
163 "31":"ansired",
164 "32":"ansigreen",
165 "33":"ansiyellow",
166 "34":"ansiblue",
167 "35":"ansipurple",
168 "36":"ansicyan",
169 "37":"ansigrey"
170 };
171
172 function ansispan(str) {
173 // ansispan function adapted from github.com/mmalecki/ansispan (MIT License)
174 Object.keys(ansi_colormap).forEach(function(ansi) {
175 var span = '<span class="' + ansi_colormap[ansi] + '">';
176
177 //
178 // `\033[Xm` == `\033[0;Xm` sets foreground color to `X`.
179 //
180 str = str.replace(
181 new RegExp('\033\\[([01];)?' + ansi + 'm', 'g'), span
182 );
183 });
184
185 str = str.replace(/\033\[([01]|39|22)?m/g, '</span>');
186 return str;
165 187 };
166 188
167 189 // Transform ANSI color escape codes into HTML <span> tags with css
@@ -180,18 +202,8 b' IPython.utils = (function (IPython) {'
180 202 var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g;
181 203 txt = txt.replace(ignored_re, "");
182 204
183 while (re.test(txt)) {
184 var cmds = txt.match(re)[1].split(";");
185 closer = opened?"</span>":"";
186 opened = cmds.length > 1 || cmds[0] != 0;
187 var rep = [];
188 for (var i in cmds)
189 if (typeof(ansi_colormap[cmds[i]]) != "undefined")
190 rep.push(ansi_colormap[cmds[i]]);
191 opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":"";
192 txt = txt.replace(re, closer + opener);
193 }
194 if (opened) txt += "</span>";
205 // color ansi codes
206 txt = ansispan(txt);
195 207 return txt;
196 208 }
197 209
General Comments 0
You need to be logged in to leave comments. Login now