Show More
@@ -106,37 +106,53 b' def ansi2html(text):' | |||||
106 |
|
106 | |||
107 |
|
107 | |||
108 | def single_ansi2latex(code): |
|
108 | def single_ansi2latex(code): | |
109 | """Converts single ansi markup to latex format |
|
109 | """Converts single ansi markup to latex format. | |
110 |
|
110 | |||
111 | Return latex code and number of open brackets. |
|
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: |
|
122 | components = code.split(';') | |
114 |
|
123 | if len(components) > 1: | ||
115 | #Make sure to get the color code (which is a part of the overall style) |
|
124 | style = components[0][-1] | |
116 | # i.e. 0;31 is valid |
|
125 | color = components[1][:-1] | |
117 | # 31 is also valid, and means the same thing |
|
126 | else: | |
118 | #coloransi.color_templates stores the longer of the two formats %d;%d |
|
127 | style = '0' | |
119 | #Get the short format so we can parse that too. Short format only exist |
|
128 | color = components[0][-3:-1] | |
120 | #if no other formating is applied (the other number must be a 0)! |
|
129 | ||
121 | style_code = getattr(coloransi.TermColors, color[0]) |
|
130 | # If the style is not normal (0), bold (1) or blinking (5) then treat it as normal | |
122 | color_code = style_code.split(';')[1] |
|
131 | if style not in '015': | |
123 | is_normal = style_code.split(';')[0] == '0' |
|
132 | style = '0' | |
124 |
|
133 | |||
125 | # regular weight |
|
134 | for name, tcode in coloransi.color_templates: | |
126 | if (code == style_code) or (is_normal and code == color_code): |
|
135 | tstyle, tcolor = tcode.split(';') | |
127 |
|
136 | if tstyle == style and tcolor == color: | ||
128 | return r'{\color{'+color[0].lower()+'}', 1 |
|
137 | break | |
129 | # bold |
|
138 | else: | |
130 | if code == style_code[:3]+str(1)+style_code[3:]: |
|
139 | return '', 0 | |
131 | return r'\textbf{\color{'+color[0].lower()+'}', 1 |
|
140 | ||
132 | return '', 0 |
|
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 | def ansi2latex(text): |
|
150 | def ansi2latex(text): | |
135 | """Converts ansi formated text to latex version |
|
151 | """Converts ansi formated text to latex version | |
136 |
|
152 | |||
137 | based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py |
|
153 | based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py | |
138 | """ |
|
154 | """ | |
139 |
color_pattern = re.compile('\x1b\\[([^m] |
|
155 | color_pattern = re.compile('\x1b\\[([^m]*)m') | |
140 | last_end = 0 |
|
156 | last_end = 0 | |
141 | openbrack = 0 |
|
157 | openbrack = 0 | |
142 | outstring = '' |
|
158 | outstring = '' | |
@@ -146,8 +162,9 b' def ansi2latex(text):' | |||||
146 | if openbrack: |
|
162 | if openbrack: | |
147 | outstring += '}'*openbrack |
|
163 | outstring += '}'*openbrack | |
148 | openbrack = 0 |
|
164 | openbrack = 0 | |
149 | if not (match.group() == coloransi.TermColors.Normal or openbrack): |
|
165 | code = match.group() | |
150 | texform, openbrack = single_ansi2latex(match.group()) |
|
166 | if not (code == coloransi.TermColors.Normal or openbrack): | |
|
167 | texform, openbrack = single_ansi2latex(code) | |||
151 | outstring += texform |
|
168 | outstring += texform | |
152 | last_end = match.end() |
|
169 | last_end = match.end() | |
153 |
|
170 | |||
@@ -156,3 +173,5 b' def ansi2latex(text):' | |||||
156 | if openbrack: |
|
173 | if openbrack: | |
157 | outstring += '}'*openbrack |
|
174 | outstring += '}'*openbrack | |
158 | return outstring.strip() |
|
175 | return outstring.strip() | |
|
176 | ||||
|
177 |
@@ -71,10 +71,13 b' class TestAnsi(TestsBase):' | |||||
71 | '%s' % (TermColors.Red) : r'{\color{red}}', |
|
71 | '%s' % (TermColors.Red) : r'{\color{red}}', | |
72 | 'hello%s' % TermColors.Blue: r'hello{\color{blue}}', |
|
72 | 'hello%s' % TermColors.Blue: r'hello{\color{blue}}', | |
73 | 'he%s%sllo' % (TermColors.Green, TermColors.Cyan) : r'he{\color{green}}{\color{cyan}llo}', |
|
73 | 'he%s%sllo' % (TermColors.Green, TermColors.Cyan) : r'he{\color{green}}{\color{cyan}llo}', | |
74 | '%shello' % TermColors.Yellow : r'{\color{yellow}hello}', |
|
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'{\color{white}h}{\color{white}e}{\color{white}l}{\color{white}l}{\color{white}o}{\color{white}}', |
|
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 | 'hel%slo' % TermColors.Green : r'hel{\color{green}lo}', |
|
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 | for inval, outval in correct_outputs.items(): |
|
82 | for inval, outval in correct_outputs.items(): | |
80 | self._try_ansi2latex(inval, outval) |
|
83 | self._try_ansi2latex(inval, outval) |
General Comments 0
You need to be logged in to leave comments.
Login now