|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 Pygments |
|
4 ~~~~~~~~ |
|
5 |
|
6 Pygments is a syntax highlighting package written in Python. |
|
7 |
|
8 It is a generic syntax highlighter for general use in all kinds of software |
|
9 such as forum systems, wikis or other applications that need to prettify |
|
10 source code. Highlights are: |
|
11 |
|
12 * a wide range of common languages and markup formats is supported |
|
13 * special attention is paid to details, increasing quality by a fair amount |
|
14 * support for new languages and formats are added easily |
|
15 * a number of output formats, presently HTML, LaTeX, RTF, SVG and ANSI sequences |
|
16 * it is usable as a command-line tool and as a library |
|
17 * ... and it highlights even Brainfuck! |
|
18 |
|
19 The `Pygments tip`_ is installable with ``easy_install Pygments==dev``. |
|
20 |
|
21 .. _Pygments tip: |
|
22 http://dev.pocoo.org/hg/pygments-main/archive/tip.tar.gz#egg=Pygments-dev |
|
23 |
|
24 :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS. |
|
25 :license: BSD, see LICENSE for details. |
|
26 """ |
|
27 |
|
28 __version__ = '1.1' |
|
29 __docformat__ = 'restructuredtext' |
|
30 |
|
31 __all__ = ['lex', 'format', 'highlight'] |
|
32 |
|
33 |
|
34 import sys, os |
|
35 |
|
36 from pygments.util import StringIO, BytesIO |
|
37 |
|
38 |
|
39 def lex(code, lexer): |
|
40 """ |
|
41 Lex ``code`` with ``lexer`` and return an iterable of tokens. |
|
42 """ |
|
43 try: |
|
44 return lexer.get_tokens(code) |
|
45 except TypeError, err: |
|
46 if isinstance(err.args[0], str) and \ |
|
47 'unbound method get_tokens' in err.args[0]: |
|
48 raise TypeError('lex() argument must be a lexer instance, ' |
|
49 'not a class') |
|
50 raise |
|
51 |
|
52 |
|
53 def format(tokens, formatter, outfile=None): |
|
54 """ |
|
55 Format a tokenlist ``tokens`` with the formatter ``formatter``. |
|
56 |
|
57 If ``outfile`` is given and a valid file object (an object |
|
58 with a ``write`` method), the result will be written to it, otherwise |
|
59 it is returned as a string. |
|
60 """ |
|
61 try: |
|
62 if not outfile: |
|
63 #print formatter, 'using', formatter.encoding |
|
64 realoutfile = formatter.encoding and BytesIO() or StringIO() |
|
65 formatter.format(tokens, realoutfile) |
|
66 return realoutfile.getvalue() |
|
67 else: |
|
68 formatter.format(tokens, outfile) |
|
69 except TypeError, err: |
|
70 if isinstance(err.args[0], str) and \ |
|
71 'unbound method format' in err.args[0]: |
|
72 raise TypeError('format() argument must be a formatter instance, ' |
|
73 'not a class') |
|
74 raise |
|
75 |
|
76 |
|
77 def highlight(code, lexer, formatter, outfile=None): |
|
78 """ |
|
79 Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``. |
|
80 |
|
81 If ``outfile`` is given and a valid file object (an object |
|
82 with a ``write`` method), the result will be written to it, otherwise |
|
83 it is returned as a string. |
|
84 """ |
|
85 return format(lex(code, lexer), formatter, outfile) |
|
86 |
|
87 |
|
88 if __name__ == '__main__': |
|
89 from pygments.cmdline import main |
|
90 sys.exit(main(sys.argv)) |