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