|
| 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, all image |
| 16 | + formats that PIL supports, and ANSI sequences |
| 17 | + * it is usable as a command-line tool and as a library |
| 18 | + * ... and it highlights even Brainfuck! |
| 19 | +
|
| 20 | + The `Pygments tip`_ is installable with ``easy_install Pygments==dev``. |
| 21 | +
|
| 22 | + .. _Pygments tip: |
| 23 | + http://bitbucket.org/birkenfeld/pygments-main/get/tip.zip#egg=Pygments-dev |
| 24 | +
|
| 25 | + :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. |
| 26 | + :license: BSD, see LICENSE for details. |
| 27 | +""" |
| 28 | + |
| 29 | +__version__ = '1.6' |
| 30 | +__docformat__ = 'restructuredtext' |
| 31 | + |
| 32 | +__all__ = ['lex', 'format', 'highlight'] |
| 33 | + |
| 34 | + |
| 35 | +import sys |
| 36 | + |
| 37 | +from pygments.util import StringIO, BytesIO |
| 38 | + |
| 39 | + |
| 40 | +def lex(code, lexer): |
| 41 | + """ |
| 42 | + Lex ``code`` with ``lexer`` and return an iterable of tokens. |
| 43 | + """ |
| 44 | + try: |
| 45 | + return lexer.get_tokens(code) |
| 46 | + except TypeError, err: |
| 47 | + if isinstance(err.args[0], str) and \ |
| 48 | + 'unbound method get_tokens' in err.args[0]: |
| 49 | + raise TypeError('lex() argument must be a lexer instance, ' |
| 50 | + 'not a class') |
| 51 | + raise |
| 52 | + |
| 53 | + |
| 54 | +def format(tokens, formatter, outfile=None): |
| 55 | + """ |
| 56 | + Format a tokenlist ``tokens`` with the formatter ``formatter``. |
| 57 | +
|
| 58 | + If ``outfile`` is given and a valid file object (an object |
| 59 | + with a ``write`` method), the result will be written to it, otherwise |
| 60 | + it is returned as a string. |
| 61 | + """ |
| 62 | + try: |
| 63 | + if not outfile: |
| 64 | + #print formatter, 'using', formatter.encoding |
| 65 | + realoutfile = formatter.encoding and BytesIO() or StringIO() |
| 66 | + formatter.format(tokens, realoutfile) |
| 67 | + return realoutfile.getvalue() |
| 68 | + else: |
| 69 | + formatter.format(tokens, outfile) |
| 70 | + except TypeError, err: |
| 71 | + if isinstance(err.args[0], str) and \ |
| 72 | + 'unbound method format' in err.args[0]: |
| 73 | + raise TypeError('format() argument must be a formatter instance, ' |
| 74 | + 'not a class') |
| 75 | + raise |
| 76 | + |
| 77 | + |
| 78 | +def highlight(code, lexer, formatter, outfile=None): |
| 79 | + """ |
| 80 | + Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``. |
| 81 | +
|
| 82 | + If ``outfile`` is given and a valid file object (an object |
| 83 | + with a ``write`` method), the result will be written to it, otherwise |
| 84 | + it is returned as a string. |
| 85 | + """ |
| 86 | + return format(lex(code, lexer), formatter, outfile) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + from pygments.cmdline import main |
| 91 | + sys.exit(main(sys.argv)) |
0 commit comments