Show More
@@ -158,12 +158,34 b' IPython.utils = (function (IPython) {' | |||||
158 |
|
158 | |||
159 | //Map from terminal commands to CSS classes |
|
159 | //Map from terminal commands to CSS classes | |
160 | var ansi_colormap = { |
|
160 | var ansi_colormap = { | |
161 |
" |
|
161 | "01":"ansibold", | |
162 | "32":"ansigreen", "33":"ansiyellow", |
|
162 | "30":"ansiblack", | |
163 | "34":"ansiblue", "35":"ansipurple","36":"ansicyan", |
|
163 | "31":"ansired", | |
164 |
"3 |
|
164 | "32":"ansigreen", | |
|
165 | "33":"ansiyellow", | |||
|
166 | "34":"ansiblue", | |||
|
167 | "35":"ansipurple", | |||
|
168 | "36":"ansicyan", | |||
|
169 | "37":"ansigrey" | |||
165 | }; |
|
170 | }; | |
166 |
|
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; | |||
|
187 | }; | |||
|
188 | ||||
167 | // Transform ANSI color escape codes into HTML <span> tags with css |
|
189 | // Transform ANSI color escape codes into HTML <span> tags with css | |
168 | // classes listed in the above ansi_colormap object. The actual color used |
|
190 | // classes listed in the above ansi_colormap object. The actual color used | |
169 | // are set in the css file. |
|
191 | // are set in the css file. | |
@@ -179,19 +201,9 b' IPython.utils = (function (IPython) {' | |||||
179 | // all ANSI codes that do not end with "m". |
|
201 | // all ANSI codes that do not end with "m". | |
180 | var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g; |
|
202 | var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g; | |
181 | txt = txt.replace(ignored_re, ""); |
|
203 | txt = txt.replace(ignored_re, ""); | |
182 |
|
204 | |||
183 | while (re.test(txt)) { |
|
205 | // color ansi codes | |
184 | var cmds = txt.match(re)[1].split(";"); |
|
206 | txt = ansispan(txt); | |
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>"; |
|
|||
195 | return txt; |
|
207 | return txt; | |
196 | } |
|
208 | } | |
197 |
|
209 |
General Comments 0
You need to be logged in to leave comments.
Login now