Show More
@@ -28,7 +28,7 b'' | |||
|
28 | 28 | scan Python source code and re-emit it with no changes to its original |
|
29 | 29 | formatting (which is the hard part). |
|
30 | 30 | |
|
31 |
$Id: PyColorize.py 2 |
|
|
31 | $Id: PyColorize.py 2205 2007-04-04 06:04:01Z fperez $""" | |
|
32 | 32 | |
|
33 | 33 | __all__ = ['ANSICodeColors','Parser'] |
|
34 | 34 | |
@@ -132,7 +132,6 b' class Parser:' | |||
|
132 | 132 | string 'str' and the parser will automatically return the output in a |
|
133 | 133 | string.""" |
|
134 | 134 | |
|
135 | self.raw = string.strip(string.expandtabs(raw)) | |
|
136 | 135 | string_output = 0 |
|
137 | 136 | if out == 'str' or self.out == 'str': |
|
138 | 137 | out_old = self.out |
@@ -140,22 +139,36 b' class Parser:' | |||
|
140 | 139 | string_output = 1 |
|
141 | 140 | elif out is not None: |
|
142 | 141 | self.out = out |
|
143 | # local shorthand | |
|
142 | ||
|
143 | # Fast return of the unmodified input for NoColor scheme | |
|
144 | if scheme == 'NoColor': | |
|
145 | error = False | |
|
146 | self.out.write(raw) | |
|
147 | if string_output: | |
|
148 | return raw,error | |
|
149 | else: | |
|
150 | return None,error | |
|
151 | ||
|
152 | # local shorthands | |
|
144 | 153 | colors = self.color_table[scheme].colors |
|
145 | 154 | self.colors = colors # put in object so __call__ sees it |
|
155 | ||
|
156 | # Remove trailing whitespace and normalize tabs | |
|
157 | self.raw = raw.expandtabs().rstrip() | |
|
146 | 158 | # store line offsets in self.lines |
|
147 | 159 | self.lines = [0, 0] |
|
148 | 160 | pos = 0 |
|
161 | raw_find = raw.find | |
|
162 | lines_append = self.lines.append | |
|
149 | 163 | while 1: |
|
150 |
pos = |
|
|
164 | pos = raw_find('\n', pos) + 1 | |
|
151 | 165 | if not pos: break |
|
152 |
|
|
|
153 |
|
|
|
166 | lines_append(pos) | |
|
167 | lines_append(len(self.raw)) | |
|
154 | 168 | |
|
155 | 169 | # parse the source and write it |
|
156 | 170 | self.pos = 0 |
|
157 | 171 | text = cStringIO.StringIO(self.raw) |
|
158 | #self.out.write('<pre><font face="Courier New">') | |
|
159 | 172 | |
|
160 | 173 | error = False |
|
161 | 174 | try: |
@@ -179,8 +192,9 b' class Parser:' | |||
|
179 | 192 | def __call__(self, toktype, toktext, (srow,scol), (erow,ecol), line): |
|
180 | 193 | """ Token handler, with syntax highlighting.""" |
|
181 | 194 | |
|
182 | # local shorthand | |
|
195 | # local shorthands | |
|
183 | 196 | colors = self.colors |
|
197 | owrite = self.out.write | |
|
184 | 198 | |
|
185 | 199 | # line separator, so this works across platforms |
|
186 | 200 | linesep = os.linesep |
@@ -192,12 +206,12 b' class Parser:' | |||
|
192 | 206 | |
|
193 | 207 | # handle newlines |
|
194 | 208 | if toktype in [token.NEWLINE, tokenize.NL]: |
|
195 |
|
|
|
209 | owrite(linesep) | |
|
196 | 210 | return |
|
197 | 211 | |
|
198 | 212 | # send the original whitespace, if needed |
|
199 | 213 | if newpos > oldpos: |
|
200 |
|
|
|
214 | owrite(self.raw[oldpos:newpos]) | |
|
201 | 215 | |
|
202 | 216 | # skip indenting tokens |
|
203 | 217 | if toktype in [token.INDENT, token.DEDENT]: |
@@ -220,7 +234,7 b' class Parser:' | |||
|
220 | 234 | (colors.normal,linesep,color)) |
|
221 | 235 | |
|
222 | 236 | # send text |
|
223 |
|
|
|
237 | owrite('%s%s%s' % (color,toktext,colors.normal)) | |
|
224 | 238 | |
|
225 | 239 | def main(): |
|
226 | 240 | """Colorize a python file using ANSI color escapes and print to stdout. |
@@ -1,3 +1,9 b'' | |||
|
1 | 2007-04-04 Fernando Perez <Fernando.Perez@colorado.edu> | |
|
2 | ||
|
3 | * IPython/PyColorize.py (Parser.format2): Fix identation of | |
|
4 | colorzied output and return early if color scheme is NoColor, to | |
|
5 | avoid unnecessary and expensive tokenization. Closes #131. | |
|
6 | ||
|
1 | 7 | 2007-04-03 Fernando Perez <Fernando.Perez@colorado.edu> |
|
2 | 8 | |
|
3 | 9 | * IPython/Debugger.py: disable the use of pydb version 1.17. It |
General Comments 0
You need to be logged in to leave comments.
Login now