117 |
117 |
118 def _print_help(what, name): |
118 def _print_help(what, name): |
119 try: |
119 try: |
120 if what == 'lexer': |
120 if what == 'lexer': |
121 cls = find_lexer_class(name) |
121 cls = find_lexer_class(name) |
122 print "Help on the %s lexer:" % cls.name |
122 print("Help on the %s lexer:" % cls.name) |
123 print dedent(cls.__doc__) |
123 print(dedent(cls.__doc__)) |
124 elif what == 'formatter': |
124 elif what == 'formatter': |
125 cls = find_formatter_class(name) |
125 cls = find_formatter_class(name) |
126 print "Help on the %s formatter:" % cls.name |
126 print("Help on the %s formatter:" % cls.name) |
127 print dedent(cls.__doc__) |
127 print(dedent(cls.__doc__)) |
128 elif what == 'filter': |
128 elif what == 'filter': |
129 cls = find_filter_class(name) |
129 cls = find_filter_class(name) |
130 print "Help on the %s filter:" % name |
130 print("Help on the %s filter:" % name) |
131 print dedent(cls.__doc__) |
131 print(dedent(cls.__doc__)) |
132 except AttributeError: |
132 except AttributeError: |
133 print >>sys.stderr, "%s not found!" % what |
133 print("%s not found!" % what, file=sys.stderr) |
134 |
134 |
135 |
135 |
136 def _print_list(what): |
136 def _print_list(what): |
137 if what == 'lexer': |
137 if what == 'lexer': |
138 print |
138 print() |
139 print "Lexers:" |
139 print("Lexers:") |
140 print "~~~~~~~" |
140 print("~~~~~~~") |
141 |
141 |
142 info = [] |
142 info = [] |
143 for fullname, names, exts, _ in get_all_lexers(): |
143 for fullname, names, exts, _ in get_all_lexers(): |
144 tup = (', '.join(names)+':', fullname, |
144 tup = (', '.join(names)+':', fullname, |
145 exts and '(filenames ' + ', '.join(exts) + ')' or '') |
145 exts and '(filenames ' + ', '.join(exts) + ')' or '') |
146 info.append(tup) |
146 info.append(tup) |
147 info.sort() |
147 info.sort() |
148 for i in info: |
148 for i in info: |
149 print ('* %s\n %s %s') % i |
149 print(('* %s\n %s %s') % i) |
150 |
150 |
151 elif what == 'formatter': |
151 elif what == 'formatter': |
152 print |
152 print() |
153 print "Formatters:" |
153 print("Formatters:") |
154 print "~~~~~~~~~~~" |
154 print("~~~~~~~~~~~") |
155 |
155 |
156 info = [] |
156 info = [] |
157 for cls in get_all_formatters(): |
157 for cls in get_all_formatters(): |
158 doc = docstring_headline(cls) |
158 doc = docstring_headline(cls) |
159 tup = (', '.join(cls.aliases) + ':', doc, cls.filenames and |
159 tup = (', '.join(cls.aliases) + ':', doc, cls.filenames and |
160 '(filenames ' + ', '.join(cls.filenames) + ')' or '') |
160 '(filenames ' + ', '.join(cls.filenames) + ')' or '') |
161 info.append(tup) |
161 info.append(tup) |
162 info.sort() |
162 info.sort() |
163 for i in info: |
163 for i in info: |
164 print ('* %s\n %s %s') % i |
164 print(('* %s\n %s %s') % i) |
165 |
165 |
166 elif what == 'filter': |
166 elif what == 'filter': |
167 print |
167 print() |
168 print "Filters:" |
168 print("Filters:") |
169 print "~~~~~~~~" |
169 print("~~~~~~~~") |
170 |
170 |
171 for name in get_all_filters(): |
171 for name in get_all_filters(): |
172 cls = find_filter_class(name) |
172 cls = find_filter_class(name) |
173 print "* " + name + ':' |
173 print("* " + name + ':') |
174 print " %s" % docstring_headline(cls) |
174 print(" %s" % docstring_headline(cls)) |
175 |
175 |
176 elif what == 'style': |
176 elif what == 'style': |
177 print |
177 print() |
178 print "Styles:" |
178 print("Styles:") |
179 print "~~~~~~~" |
179 print("~~~~~~~") |
180 |
180 |
181 for name in get_all_styles(): |
181 for name in get_all_styles(): |
182 cls = get_style_by_name(name) |
182 cls = get_style_by_name(name) |
183 print "* " + name + ':' |
183 print("* " + name + ':') |
184 print " %s" % docstring_headline(cls) |
184 print(" %s" % docstring_headline(cls)) |
185 |
185 |
186 |
186 |
187 def main(args=sys.argv): |
187 def main(args=sys.argv): |
188 """ |
188 """ |
189 Main command line entry point. |
189 Main command line entry point. |
209 elif opt == '-F': |
209 elif opt == '-F': |
210 F_opts.append(arg) |
210 F_opts.append(arg) |
211 opts[opt] = arg |
211 opts[opt] = arg |
212 |
212 |
213 if not opts and not args: |
213 if not opts and not args: |
214 print usage |
214 print(usage) |
215 return 0 |
215 return 0 |
216 |
216 |
217 if opts.pop('-h', None) is not None: |
217 if opts.pop('-h', None) is not None: |
218 print usage |
218 print(usage) |
219 return 0 |
219 return 0 |
220 |
220 |
221 if opts.pop('-V', None) is not None: |
221 if opts.pop('-V', None) is not None: |
222 print 'Pygments version %s, (c) 2006-2008 by Georg Brandl.' % __version__ |
222 print('Pygments version %s, (c) 2006-2008 by Georg Brandl.' % __version__) |
223 return 0 |
223 return 0 |
224 |
224 |
225 # handle ``pygmentize -L`` |
225 # handle ``pygmentize -L`` |
226 L_opt = opts.pop('-L', None) |
226 L_opt = opts.pop('-L', None) |
227 if L_opt is not None: |
227 if L_opt is not None: |
228 if opts: |
228 if opts: |
229 print >>sys.stderr, usage |
229 print(usage, file=sys.stderr) |
230 return 2 |
230 return 2 |
231 |
231 |
232 # print version |
232 # print version |
233 main(['', '-V']) |
233 main(['', '-V']) |
234 if not args: |
234 if not args: |
239 |
239 |
240 # handle ``pygmentize -H`` |
240 # handle ``pygmentize -H`` |
241 H_opt = opts.pop('-H', None) |
241 H_opt = opts.pop('-H', None) |
242 if H_opt is not None: |
242 if H_opt is not None: |
243 if opts or len(args) != 2: |
243 if opts or len(args) != 2: |
244 print >>sys.stderr, usage |
244 print(usage, file=sys.stderr) |
245 return 2 |
245 return 2 |
246 |
246 |
247 what, name = args |
247 what, name = args |
248 if what not in ('lexer', 'formatter', 'filter'): |
248 if what not in ('lexer', 'formatter', 'filter'): |
249 print >>sys.stderr, usage |
249 print(usage, file=sys.stderr) |
250 return 2 |
250 return 2 |
251 |
251 |
252 _print_help(what, name) |
252 _print_help(what, name) |
253 return 0 |
253 return 0 |
254 |
254 |
269 # handle ``pygmentize -N`` |
269 # handle ``pygmentize -N`` |
270 infn = opts.pop('-N', None) |
270 infn = opts.pop('-N', None) |
271 if infn is not None: |
271 if infn is not None: |
272 try: |
272 try: |
273 lexer = get_lexer_for_filename(infn, **parsed_opts) |
273 lexer = get_lexer_for_filename(infn, **parsed_opts) |
274 except ClassNotFound, err: |
274 except ClassNotFound as err: |
275 lexer = TextLexer() |
275 lexer = TextLexer() |
276 except OptionError, err: |
276 except OptionError as err: |
277 print >>sys.stderr, 'Error:', err |
277 print('Error:', err, file=sys.stderr) |
278 return 1 |
278 return 1 |
279 |
279 |
280 print lexer.aliases[0] |
280 print(lexer.aliases[0]) |
281 return 0 |
281 return 0 |
282 |
282 |
283 # handle ``pygmentize -S`` |
283 # handle ``pygmentize -S`` |
284 S_opt = opts.pop('-S', None) |
284 S_opt = opts.pop('-S', None) |
285 a_opt = opts.pop('-a', None) |
285 a_opt = opts.pop('-a', None) |
286 if S_opt is not None: |
286 if S_opt is not None: |
287 f_opt = opts.pop('-f', None) |
287 f_opt = opts.pop('-f', None) |
288 if not f_opt: |
288 if not f_opt: |
289 print >>sys.stderr, usage |
289 print(usage, file=sys.stderr) |
290 return 2 |
290 return 2 |
291 if opts or args: |
291 if opts or args: |
292 print >>sys.stderr, usage |
292 print(usage, file=sys.stderr) |
293 return 2 |
293 return 2 |
294 |
294 |
295 try: |
295 try: |
296 parsed_opts['style'] = S_opt |
296 parsed_opts['style'] = S_opt |
297 fmter = get_formatter_by_name(f_opt, **parsed_opts) |
297 fmter = get_formatter_by_name(f_opt, **parsed_opts) |
298 except ClassNotFound, err: |
298 except ClassNotFound as err: |
299 print >>sys.stderr, err |
299 print(err, file=sys.stderr) |
300 return 1 |
300 return 1 |
301 |
301 |
302 arg = a_opt or '' |
302 arg = a_opt or '' |
303 try: |
303 try: |
304 print fmter.get_style_defs(arg) |
304 print(fmter.get_style_defs(arg)) |
305 except Exception, err: |
305 except Exception as err: |
306 print >>sys.stderr, 'Error:', err |
306 print('Error:', err, file=sys.stderr) |
307 return 1 |
307 return 1 |
308 return 0 |
308 return 0 |
309 |
309 |
310 # if no -S is given, -a is not allowed |
310 # if no -S is given, -a is not allowed |
311 if a_opt is not None: |
311 if a_opt is not None: |
312 print >>sys.stderr, usage |
312 print(usage, file=sys.stderr) |
313 return 2 |
313 return 2 |
314 |
314 |
315 # parse -F options |
315 # parse -F options |
316 F_opts = _parse_filters(F_opts) |
316 F_opts = _parse_filters(F_opts) |
317 opts.pop('-F', None) |
317 opts.pop('-F', None) |
320 outfn = opts.pop('-o', None) |
320 outfn = opts.pop('-o', None) |
321 fmter = opts.pop('-f', None) |
321 fmter = opts.pop('-f', None) |
322 if fmter: |
322 if fmter: |
323 try: |
323 try: |
324 fmter = get_formatter_by_name(fmter, **parsed_opts) |
324 fmter = get_formatter_by_name(fmter, **parsed_opts) |
325 except (OptionError, ClassNotFound), err: |
325 except (OptionError, ClassNotFound) as err: |
326 print >>sys.stderr, 'Error:', err |
326 print('Error:', err, file=sys.stderr) |
327 return 1 |
327 return 1 |
328 |
328 |
329 if outfn: |
329 if outfn: |
330 if not fmter: |
330 if not fmter: |
331 try: |
331 try: |
332 fmter = get_formatter_for_filename(outfn, **parsed_opts) |
332 fmter = get_formatter_for_filename(outfn, **parsed_opts) |
333 except (OptionError, ClassNotFound), err: |
333 except (OptionError, ClassNotFound) as err: |
334 print >>sys.stderr, 'Error:', err |
334 print('Error:', err, file=sys.stderr) |
335 return 1 |
335 return 1 |
336 try: |
336 try: |
337 outfile = open(outfn, 'wb') |
337 outfile = open(outfn, 'wb') |
338 except Exception, err: |
338 except Exception as err: |
339 print >>sys.stderr, 'Error: cannot open outfile:', err |
339 print('Error: cannot open outfile:', err, file=sys.stderr) |
340 return 1 |
340 return 1 |
341 else: |
341 else: |
342 if not fmter: |
342 if not fmter: |
343 fmter = TerminalFormatter(**parsed_opts) |
343 fmter = TerminalFormatter(**parsed_opts) |
344 outfile = sys.stdout |
344 outfile = sys.stdout |
346 # select lexer |
346 # select lexer |
347 lexer = opts.pop('-l', None) |
347 lexer = opts.pop('-l', None) |
348 if lexer: |
348 if lexer: |
349 try: |
349 try: |
350 lexer = get_lexer_by_name(lexer, **parsed_opts) |
350 lexer = get_lexer_by_name(lexer, **parsed_opts) |
351 except (OptionError, ClassNotFound), err: |
351 except (OptionError, ClassNotFound) as err: |
352 print >>sys.stderr, 'Error:', err |
352 print('Error:', err, file=sys.stderr) |
353 return 1 |
353 return 1 |
354 |
354 |
355 if args: |
355 if args: |
356 if len(args) > 1: |
356 if len(args) > 1: |
357 print >>sys.stderr, usage |
357 print(usage, file=sys.stderr) |
358 return 2 |
358 return 2 |
359 |
359 |
360 infn = args[0] |
360 infn = args[0] |
361 try: |
361 try: |
362 code = open(infn, 'rb').read() |
362 code = open(infn, 'rb').read() |
363 except Exception, err: |
363 except Exception as err: |
364 print >>sys.stderr, 'Error: cannot read infile:', err |
364 print('Error: cannot read infile:', err, file=sys.stderr) |
365 return 1 |
365 return 1 |
366 |
366 |
367 if not lexer: |
367 if not lexer: |
368 try: |
368 try: |
369 lexer = get_lexer_for_filename(infn, code, **parsed_opts) |
369 lexer = get_lexer_for_filename(infn, code, **parsed_opts) |
370 except ClassNotFound, err: |
370 except ClassNotFound as err: |
371 if '-g' in opts: |
371 if '-g' in opts: |
372 try: |
372 try: |
373 lexer = guess_lexer(code) |
373 lexer = guess_lexer(code) |
374 except ClassNotFound: |
374 except ClassNotFound: |
375 lexer = TextLexer() |
375 lexer = TextLexer() |
376 else: |
376 else: |
377 print >>sys.stderr, 'Error:', err |
377 print('Error:', err, file=sys.stderr) |
378 return 1 |
378 return 1 |
379 except OptionError, err: |
379 except OptionError as err: |
380 print >>sys.stderr, 'Error:', err |
380 print('Error:', err, file=sys.stderr) |
381 return 1 |
381 return 1 |
382 |
382 |
383 else: |
383 else: |
384 if '-g' in opts: |
384 if '-g' in opts: |
385 code = sys.stdin.read() |
385 code = sys.stdin.read() |
386 try: |
386 try: |
387 lexer = guess_lexer(code) |
387 lexer = guess_lexer(code) |
388 except ClassNotFound: |
388 except ClassNotFound: |
389 lexer = TextLexer() |
389 lexer = TextLexer() |
390 elif not lexer: |
390 elif not lexer: |
391 print >>sys.stderr, 'Error: no lexer name given and reading ' + \ |
391 print('Error: no lexer name given and reading ' + \ |
392 'from stdin (try using -g or -l <lexer>)' |
392 'from stdin (try using -g or -l <lexer>)', file=sys.stderr) |
393 return 2 |
393 return 2 |
394 else: |
394 else: |
395 code = sys.stdin.read() |
395 code = sys.stdin.read() |
396 |
396 |
397 # No encoding given? Use latin1 if output file given, |
397 # No encoding given? Use latin1 if output file given, |
413 try: |
413 try: |
414 # process filters |
414 # process filters |
415 for fname, fopts in F_opts: |
415 for fname, fopts in F_opts: |
416 lexer.add_filter(fname, **fopts) |
416 lexer.add_filter(fname, **fopts) |
417 highlight(code, lexer, fmter, outfile) |
417 highlight(code, lexer, fmter, outfile) |
418 except Exception, err: |
418 except Exception as err: |
419 import traceback |
419 import traceback |
420 info = traceback.format_exception(*sys.exc_info()) |
420 info = traceback.format_exception(*sys.exc_info()) |
421 msg = info[-1].strip() |
421 msg = info[-1].strip() |
422 if len(info) >= 3: |
422 if len(info) >= 3: |
423 # extract relevant file and position info |
423 # extract relevant file and position info |
424 msg += '\n (f%s)' % info[-2].split('\n')[0].strip()[1:] |
424 msg += '\n (f%s)' % info[-2].split('\n')[0].strip()[1:] |
425 print >>sys.stderr |
425 print(file=sys.stderr) |
426 print >>sys.stderr, '*** Error while highlighting:' |
426 print('*** Error while highlighting:', file=sys.stderr) |
427 print >>sys.stderr, msg |
427 print(msg, file=sys.stderr) |
428 return 1 |
428 return 1 |
429 |
429 |
430 return 0 |
430 return 0 |