3 pygments.cmdline |
3 pygments.cmdline |
4 ~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Command line interface. |
6 Command line interface. |
7 |
7 |
8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 from __future__ import print_function |
12 from __future__ import print_function |
13 |
13 |
17 |
17 |
18 from pygments import __version__, highlight |
18 from pygments import __version__, highlight |
19 from pygments.util import ClassNotFound, OptionError, docstring_headline, \ |
19 from pygments.util import ClassNotFound, OptionError, docstring_headline, \ |
20 guess_decode, guess_decode_from_terminal, terminal_encoding |
20 guess_decode, guess_decode_from_terminal, terminal_encoding |
21 from pygments.lexers import get_all_lexers, get_lexer_by_name, guess_lexer, \ |
21 from pygments.lexers import get_all_lexers, get_lexer_by_name, guess_lexer, \ |
22 get_lexer_for_filename, find_lexer_class_for_filename |
22 load_lexer_from_file, get_lexer_for_filename, find_lexer_class_for_filename |
23 from pygments.lexers.special import TextLexer |
23 from pygments.lexers.special import TextLexer |
24 from pygments.formatters.latex import LatexEmbeddedLexer, LatexFormatter |
24 from pygments.formatters.latex import LatexEmbeddedLexer, LatexFormatter |
25 from pygments.formatters import get_all_formatters, get_formatter_by_name, \ |
25 from pygments.formatters import get_all_formatters, get_formatter_by_name, \ |
26 get_formatter_for_filename, find_formatter_class |
26 load_formatter_from_file, get_formatter_for_filename, find_formatter_class |
27 from pygments.formatters.terminal import TerminalFormatter |
27 from pygments.formatters.terminal import TerminalFormatter |
28 from pygments.filters import get_all_filters, find_filter_class |
28 from pygments.filters import get_all_filters, find_filter_class |
29 from pygments.styles import get_all_styles, get_style_by_name |
29 from pygments.styles import get_all_styles, get_style_by_name |
30 |
30 |
31 |
31 |
32 USAGE = """\ |
32 USAGE = """\ |
33 Usage: %s [-l <lexer> | -g] [-F <filter>[:<options>]] [-f <formatter>] |
33 Usage: %s [-l <lexer> | -g] [-F <filter>[:<options>]] [-f <formatter>] |
34 [-O <options>] [-P <option=value>] [-s] [-v] [-o <outfile>] [<infile>] |
34 [-O <options>] [-P <option=value>] [-s] [-v] [-x] [-o <outfile>] [<infile>] |
35 |
35 |
36 %s -S <style> -f <formatter> [-a <arg>] [-O <options>] [-P <option=value>] |
36 %s -S <style> -f <formatter> [-a <arg>] [-O <options>] [-P <option=value>] |
37 %s -L [<which> ...] |
37 %s -L [<which> ...] |
38 %s -N <filename> |
38 %s -N <filename> |
39 %s -H <type> <name> |
39 %s -H <type> <name> |
54 plain text if this fails (this can work for stdin). |
54 plain text if this fails (this can work for stdin). |
55 |
55 |
56 Likewise, <formatter> is a formatter name, and will be guessed from |
56 Likewise, <formatter> is a formatter name, and will be guessed from |
57 the extension of the output file name. If no output file is given, |
57 the extension of the output file name. If no output file is given, |
58 the terminal formatter will be used by default. |
58 the terminal formatter will be used by default. |
|
59 |
|
60 The additional option -x allows custom lexers and formatters to be |
|
61 loaded from a .py file relative to the current working directory. For |
|
62 example, ``-l ./customlexer.py -x``. By default, this option expects a |
|
63 file with a class named CustomLexer or CustomFormatter; you can also |
|
64 specify your own class name with a colon (``-l ./lexer.py:MyLexer``). |
|
65 Users should be very careful not to use this option with untrusted files, |
|
66 because it will import and run them. |
59 |
67 |
60 With the -O option, you can give the lexer and formatter a comma- |
68 With the -O option, you can give the lexer and formatter a comma- |
61 separated list of options, e.g. ``-O bg=light,python=cool``. |
69 separated list of options, e.g. ``-O bg=light,python=cool``. |
62 |
70 |
63 The -P option adds lexer and formatter options like the -O option, but |
71 The -P option adds lexer and formatter options like the -O option, but |
221 if opts.pop('-h', None) is not None: |
229 if opts.pop('-h', None) is not None: |
222 print(usage) |
230 print(usage) |
223 return 0 |
231 return 0 |
224 |
232 |
225 if opts.pop('-V', None) is not None: |
233 if opts.pop('-V', None) is not None: |
226 print('Pygments version %s, (c) 2006-2015 by Georg Brandl.' % __version__) |
234 print('Pygments version %s, (c) 2006-2017 by Georg Brandl.' % __version__) |
227 return 0 |
235 return 0 |
228 |
236 |
229 # handle ``pygmentize -L`` |
237 # handle ``pygmentize -L`` |
230 L_opt = opts.pop('-L', None) |
238 L_opt = opts.pop('-L', None) |
231 if L_opt is not None: |
239 if L_opt is not None: |
312 |
320 |
313 # parse -F options |
321 # parse -F options |
314 F_opts = _parse_filters(F_opts) |
322 F_opts = _parse_filters(F_opts) |
315 opts.pop('-F', None) |
323 opts.pop('-F', None) |
316 |
324 |
|
325 allow_custom_lexer_formatter = False |
|
326 # -x: allow custom (eXternal) lexers and formatters |
|
327 if opts.pop('-x', None) is not None: |
|
328 allow_custom_lexer_formatter = True |
|
329 |
317 # select lexer |
330 # select lexer |
318 lexer = None |
331 lexer = None |
319 |
332 |
320 # given by name? |
333 # given by name? |
321 lexername = opts.pop('-l', None) |
334 lexername = opts.pop('-l', None) |
322 if lexername: |
335 if lexername: |
323 try: |
336 # custom lexer, located relative to user's cwd |
324 lexer = get_lexer_by_name(lexername, **parsed_opts) |
337 if allow_custom_lexer_formatter and '.py' in lexername: |
325 except (OptionError, ClassNotFound) as err: |
338 try: |
326 print('Error:', err, file=sys.stderr) |
339 if ':' in lexername: |
327 return 1 |
340 filename, name = lexername.rsplit(':', 1) |
|
341 lexer = load_lexer_from_file(filename, name, |
|
342 **parsed_opts) |
|
343 else: |
|
344 lexer = load_lexer_from_file(lexername, **parsed_opts) |
|
345 except ClassNotFound as err: |
|
346 print('Error:', err, file=sys.stderr) |
|
347 return 1 |
|
348 else: |
|
349 try: |
|
350 lexer = get_lexer_by_name(lexername, **parsed_opts) |
|
351 except (OptionError, ClassNotFound) as err: |
|
352 print('Error:', err, file=sys.stderr) |
|
353 return 1 |
328 |
354 |
329 # read input code |
355 # read input code |
330 code = None |
356 code = None |
331 |
357 |
332 if args: |
358 if args: |
399 |
425 |
400 # select formatter |
426 # select formatter |
401 outfn = opts.pop('-o', None) |
427 outfn = opts.pop('-o', None) |
402 fmter = opts.pop('-f', None) |
428 fmter = opts.pop('-f', None) |
403 if fmter: |
429 if fmter: |
404 try: |
430 # custom formatter, located relative to user's cwd |
405 fmter = get_formatter_by_name(fmter, **parsed_opts) |
431 if allow_custom_lexer_formatter and '.py' in fmter: |
406 except (OptionError, ClassNotFound) as err: |
432 try: |
407 print('Error:', err, file=sys.stderr) |
433 if ':' in fmter: |
408 return 1 |
434 file, fmtername = fmter.rsplit(':', 1) |
|
435 fmter = load_formatter_from_file(file, fmtername, |
|
436 **parsed_opts) |
|
437 else: |
|
438 fmter = load_formatter_from_file(fmter, **parsed_opts) |
|
439 except ClassNotFound as err: |
|
440 print('Error:', err, file=sys.stderr) |
|
441 return 1 |
|
442 else: |
|
443 try: |
|
444 fmter = get_formatter_by_name(fmter, **parsed_opts) |
|
445 except (OptionError, ClassNotFound) as err: |
|
446 print('Error:', err, file=sys.stderr) |
|
447 return 1 |
409 |
448 |
410 if outfn: |
449 if outfn: |
411 if not fmter: |
450 if not fmter: |
412 try: |
451 try: |
413 fmter = get_formatter_for_filename(outfn, **parsed_opts) |
452 fmter = get_formatter_for_filename(outfn, **parsed_opts) |
493 Main command line entry point. |
532 Main command line entry point. |
494 """ |
533 """ |
495 usage = USAGE % ((args[0],) * 6) |
534 usage = USAGE % ((args[0],) * 6) |
496 |
535 |
497 try: |
536 try: |
498 popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:vhVHgs") |
537 popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:vhVHgsx") |
499 except getopt.GetoptError: |
538 except getopt.GetoptError: |
500 print(usage, file=sys.stderr) |
539 print(usage, file=sys.stderr) |
501 return 2 |
540 return 2 |
502 |
541 |
503 try: |
542 try: |