##// END OF EJS Templates
tiny refactor to nbconvert ansi filter...
Paul Ivanov -
Show More
@@ -39,42 +39,43 b' def strip_ansi(source):'
39 39 return re.sub(r'\033\[(\d|;)+?m', '', source)
40 40
41 41
42 ansi_colormap = {
43 '30': 'ansiblack',
44 '31': 'ansired',
45 '32': 'ansigreen',
46 '33': 'ansiyellow',
47 '34': 'ansiblue',
48 '35': 'ansipurple',
49 '36': 'ansicyan',
50 '37': 'ansigrey',
51 '01': 'ansibold',
52 }
53
54 html_escapes = {
55 '<': '&lt;',
56 '>': '&gt;',
57 "'": '&apos;',
58 '"': '&quot;',
59 '`': '&#96;',
60 }
61 ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m')
62
42 63 def ansi2html(text):
43 64 """
44 Conver ansi colors to html colors.
65 Convert ansi colors to html colors.
45 66
46 67 Parameters
47 68 ----------
48 69 text : str
49 70 Text containing ansi colors to convert to html
50 71 """
51
52 ansi_colormap = {
53 '30': 'ansiblack',
54 '31': 'ansired',
55 '32': 'ansigreen',
56 '33': 'ansiyellow',
57 '34': 'ansiblue',
58 '35': 'ansipurple',
59 '36': 'ansicyan',
60 '37': 'ansigrey',
61 '01': 'ansibold',
62 }
63 72
64 73 # do ampersand first
65 74 text = text.replace('&', '&amp;')
66 html_escapes = {
67 '<': '&lt;',
68 '>': '&gt;',
69 "'": '&apos;',
70 '"': '&quot;',
71 '`': '&#96;',
72 }
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