##// END OF EJS Templates
tiny refactor to nbconvert ansi filter...
Paul Ivanov -
Show More
@@ -39,16 +39,6 b' def strip_ansi(source):'
39 39 return re.sub(r'\033\[(\d|;)+?m', '', source)
40 40
41 41
42 def ansi2html(text):
43 """
44 Conver ansi colors to html colors.
45
46 Parameters
47 ----------
48 text : str
49 Text containing ansi colors to convert to html
50 """
51
52 42 ansi_colormap = {
53 43 '30': 'ansiblack',
54 44 '31': 'ansired',
@@ -61,8 +51,6 b' def ansi2html(text):'
61 51 '01': 'ansibold',
62 52 }
63 53
64 # do ampersand first
65 text = text.replace('&', '&')
66 54 html_escapes = {
67 55 '<': '&lt;',
68 56 '>': '&gt;',
@@ -70,11 +58,24 b' def ansi2html(text):'
70 58 '"': '&quot;',
71 59 '`': '&#96;',
72 60 }
61 ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m')
62
63 def ansi2html(text):
64 """
65 Convert ansi colors to html colors.
66
67 Parameters
68 ----------
69 text : str
70 Text containing ansi colors to convert to html
71 """
72
73 # do ampersand first
74 text = text.replace('&', '&amp;')
73 75
74 76 for c, escape in html_escapes.items():
75 77 text = text.replace(c, escape)
76 78
77 ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m')
78 79 m = ansi_re.search(text)
79 80 opened = False
80 81 cmds = []
@@ -90,7 +91,7 b' def ansi2html(text):'
90 91 classes = []
91 92 for cmd in cmds:
92 93 if cmd in ansi_colormap:
93 classes.append(ansi_colormap.get(cmd))
94 classes.append(ansi_colormap[cmd])
94 95
95 96 if classes:
96 97 opener = '<span class="%s">' % (' '.join(classes))
General Comments 0
You need to be logged in to leave comments. Login now