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