# -*- coding: utf-8 -*- """ Pygments ~~~~~~~ Pygments is a syntax highlighting package written in Python. It aims to be a generic syntax highlighter for general use in all kinds of software such as forum systems, wikis or other applications that need to prettify source code. Highlights are: * a wide range of common languages and markup formats is supported * special attention is paid to details, increasing quality by a fair amount * support for new languages and formats are added easily * a number of output formats, presently HTML, LaTeX and ANSI sequences * it is usable as a command-line tool and as a library * ... and it highlights even Brainfuck! :copyright: 2006 by Georg Brandl, Armin Ronacher, Lukas Meuser and others. :license: GNU LGPL, see LICENSE for more details. """ __version__ = '0.1' __docformat__ = 'restructuredtext' __license__ = 'GNU Lesser General Public License (LGPL)' __author__ = 'Georg Brandl ' __url__ = 'http://pygments.pocoo.org/' import sys, os # using StringIO because it can handle Unicode strings from StringIO import StringIO from pygments.util import OptionError from pygments.lexers import LEXERS, get_lexer_by_name, get_lexer_for_filename from pygments.formatters import FORMATTERS, get_formatter_by_name, \ get_formatter_for_filename, TerminalFormatter def lex(code, lexer): """ Lex ``code`` with ``lexer`` and return an iterable of tokens. """ return lexer.get_tokens(code) def format(tokens, formatter, outfile=None): """ Format a tokenlist ``tokens`` with the formatter ``formatter``. If ``outfile`` is given and a valid file object (an object with a ``write`` method), the result will be written to it, otherwise it is returned as a string. """ realoutfile = outfile or StringIO() formatter.format(tokens, realoutfile) if not outfile: return realoutfile.getvalue() def highlight(code, lexer, formatter, outfile=None): """ Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``. If ``outfile`` is given and a valid file object (an object with a ``write`` method), the result will be written to it, otherwise it is returned as a string. """ return format(lex(code, lexer), formatter, outfile) def cmdline_main(args): """ Make pygments usable as a command line utility. """ import getopt USAGE = """\ Usage: %s [-l ] [-f ] [-O ] [-o ] [] %s -S