Back to index

moin  1.9.0~rc2
__init__.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     Pygments
00004     ~~~~~~~~
00005 
00006     Pygments is a syntax highlighting package written in Python.
00007 
00008     It is a generic syntax highlighter for general use in all kinds of software
00009     such as forum systems, wikis or other applications that need to prettify
00010     source code. Highlights are:
00011 
00012     * a wide range of common languages and markup formats is supported
00013     * special attention is paid to details, increasing quality by a fair amount
00014     * support for new languages and formats are added easily
00015     * a number of output formats, presently HTML, LaTeX, RTF, SVG and ANSI sequences
00016     * it is usable as a command-line tool and as a library
00017     * ... and it highlights even Brainfuck!
00018 
00019     The `Pygments tip`_ is installable with ``easy_install Pygments==dev``.
00020 
00021     .. _Pygments tip:
00022        http://dev.pocoo.org/hg/pygments-main/archive/tip.tar.gz#egg=Pygments-dev
00023 
00024     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00025     :license: BSD, see LICENSE for details.
00026 """
00027 
00028 __version__ = '1.1.1'
00029 __docformat__ = 'restructuredtext'
00030 
00031 __all__ = ['lex', 'format', 'highlight']
00032 
00033 
00034 import sys, os
00035 
00036 from pygments.util import StringIO, BytesIO
00037 
00038 
00039 def lex(code, lexer):
00040     """
00041     Lex ``code`` with ``lexer`` and return an iterable of tokens.
00042     """
00043     try:
00044         return lexer.get_tokens(code)
00045     except TypeError, err:
00046         if isinstance(err.args[0], str) and \
00047            'unbound method get_tokens' in err.args[0]:
00048             raise TypeError('lex() argument must be a lexer instance, '
00049                             'not a class')
00050         raise
00051 
00052 
00053 def format(tokens, formatter, outfile=None):
00054     """
00055     Format a tokenlist ``tokens`` with the formatter ``formatter``.
00056 
00057     If ``outfile`` is given and a valid file object (an object
00058     with a ``write`` method), the result will be written to it, otherwise
00059     it is returned as a string.
00060     """
00061     try:
00062         if not outfile:
00063             #print formatter, 'using', formatter.encoding
00064             realoutfile = formatter.encoding and BytesIO() or StringIO()
00065             formatter.format(tokens, realoutfile)
00066             return realoutfile.getvalue()
00067         else:
00068             formatter.format(tokens, outfile)
00069     except TypeError, err:
00070         if isinstance(err.args[0], str) and \
00071            'unbound method format' in err.args[0]:
00072             raise TypeError('format() argument must be a formatter instance, '
00073                             'not a class')
00074         raise
00075 
00076 
00077 def highlight(code, lexer, formatter, outfile=None):
00078     """
00079     Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``.
00080 
00081     If ``outfile`` is given and a valid file object (an object
00082     with a ``write`` method), the result will be written to it, otherwise
00083     it is returned as a string.
00084     """
00085     return format(lex(code, lexer), formatter, outfile)
00086 
00087 
00088 if __name__ == '__main__':
00089     from pygments.cmdline import main
00090     sys.exit(main(sys.argv))