##// END OF EJS Templates
Deal with ansi escape codes when nbconverting to latex...
Richard Everson -
Show More
@@ -106,37 +106,53 b' def ansi2html(text):'
106 106
107 107
108 108 def single_ansi2latex(code):
109 """Converts single ansi markup to latex format
109 """Converts single ansi markup to latex format.
110 110
111 111 Return latex code and number of open brackets.
112
113 Accepts codes like '\x1b[1;32m' (bold, red) and the short form '\x1b[32m' (red)
114
115 Colors are matched to those defined in coloransi, which defines colors
116 using the 0, 1 (bold) and 5 (blinking) styles. Styles 1 and 5 are
117 interpreted as bold. All other styles are mapped to 0. Note that in
118 coloransi, a style of 1 does not just mean bold; for example, Brown is
119 "0;33", but Yellow is "1;33". An empty string is returned for unrecognised
120 codes and the "reset" code '\x1b[m'.
112 121 """
113 for color in coloransi.color_templates:
114
115 #Make sure to get the color code (which is a part of the overall style)
116 # i.e. 0;31 is valid
117 # 31 is also valid, and means the same thing
118 #coloransi.color_templates stores the longer of the two formats %d;%d
119 #Get the short format so we can parse that too. Short format only exist
120 #if no other formating is applied (the other number must be a 0)!
121 style_code = getattr(coloransi.TermColors, color[0])
122 color_code = style_code.split(';')[1]
123 is_normal = style_code.split(';')[0] == '0'
124
125 # regular weight
126 if (code == style_code) or (is_normal and code == color_code):
127
128 return r'{\color{'+color[0].lower()+'}', 1
129 # bold
130 if code == style_code[:3]+str(1)+style_code[3:]:
131 return r'\textbf{\color{'+color[0].lower()+'}', 1
132 return '', 0
122 components = code.split(';')
123 if len(components) > 1:
124 style = components[0][-1]
125 color = components[1][:-1]
126 else:
127 style = '0'
128 color = components[0][-3:-1]
129
130 # If the style is not normal (0), bold (1) or blinking (5) then treat it as normal
131 if style not in '015':
132 style = '0'
133
134 for name, tcode in coloransi.color_templates:
135 tstyle, tcolor = tcode.split(';')
136 if tstyle == style and tcolor == color:
137 break
138 else:
139 return '', 0
140
141 if style == '5':
142 name = name[5:] # BlinkRed -> Red, etc
143 name = name.lower()
144
145 if style in '15':
146 return r'\textbf{\color{'+name+'}', 1
147 else:
148 return r'{\color{'+name+'}', 1
133 149
134 150 def ansi2latex(text):
135 151 """Converts ansi formated text to latex version
136 152
137 153 based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py
138 154 """
139 color_pattern = re.compile('\x1b\\[([^m]+)m')
155 color_pattern = re.compile('\x1b\\[([^m]*)m')
140 156 last_end = 0
141 157 openbrack = 0
142 158 outstring = ''
@@ -146,8 +162,9 b' def ansi2latex(text):'
146 162 if openbrack:
147 163 outstring += '}'*openbrack
148 164 openbrack = 0
149 if not (match.group() == coloransi.TermColors.Normal or openbrack):
150 texform, openbrack = single_ansi2latex(match.group())
165 code = match.group()
166 if not (code == coloransi.TermColors.Normal or openbrack):
167 texform, openbrack = single_ansi2latex(code)
151 168 outstring += texform
152 169 last_end = match.end()
153 170
@@ -156,3 +173,5 b' def ansi2latex(text):'
156 173 if openbrack:
157 174 outstring += '}'*openbrack
158 175 return outstring.strip()
176
177
@@ -71,10 +71,13 b' class TestAnsi(TestsBase):'
71 71 '%s' % (TermColors.Red) : r'{\color{red}}',
72 72 'hello%s' % TermColors.Blue: r'hello{\color{blue}}',
73 73 'he%s%sllo' % (TermColors.Green, TermColors.Cyan) : r'he{\color{green}}{\color{cyan}llo}',
74 '%shello' % TermColors.Yellow : r'{\color{yellow}hello}',
75 '{0}h{0}e{0}l{0}l{0}o{0}'.format(TermColors.White) : r'{\color{white}h}{\color{white}e}{\color{white}l}{\color{white}l}{\color{white}o}{\color{white}}',
74 '%shello' % TermColors.Yellow : r'\textbf{\color{yellow}hello}',
75 '{0}h{0}e{0}l{0}l{0}o{0}'.format(TermColors.White) : r'\textbf{\color{white}h}\textbf{\color{white}e}\textbf{\color{white}l}\textbf{\color{white}l}\textbf{\color{white}o}\textbf{\color{white}}',
76 76 'hel%slo' % TermColors.Green : r'hel{\color{green}lo}',
77 'hello' : 'hello'}
77 'hello' : 'hello',
78 u'hello\x1b[34mthere\x1b[mworld' : u'hello{\\color{blue}there}world',
79 u'hello\x1b[mthere': u'hellothere'
80 }
78 81
79 82 for inval, outval in correct_outputs.items():
80 83 self._try_ansi2latex(inval, outval)
General Comments 0
You need to be logged in to leave comments. Login now