.. -*- mode: rst -*- ========== Quickstart ========== Pygments comes with a wide range of lexers for modern languages which are all accessible through the pygments.lexers package. A lexer enables Pygments to parse the source code into tokens which are passed to a formatter. Currently formatters exist for HTML, LaTeX and ANSI sequences. Example ======= Here is a small example for highlighting Python code: .. sourcecode:: python from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter code = 'print "Hello World"' print highlight(code, PythonLexer(), HtmlFormatter()) which prints something like this: .. sourcecode:: html
print "Hello World"
A CSS stylesheet which contains all CSS classes possibly used in the output can be produced by: .. sourcecode:: python print HtmlFormatter().get_style_defs('.highlight') The argument is used as an additional CSS selector: the output may look like .. sourcecode:: css .highlight .k { color: #AA22FF; font-weight: bold } .highlight .s { color: #BB4444 } ... Options ======= The `highlight()` function supports a fourth argument called `outfile`, it must be a file object if given. The formatted output will then be written to this file instead of being returned as a string. Lexers and formatters both support options. They are given to them as keyword arguments either to the class or to the lookup method: .. sourcecode:: python from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter lexer = get_lexer_by_name("python", stripall=True) formatter = HtmlFormatter(linenos=True, cssclass="source") result = highlight(code, lexer, formatter) This makes the lexer strip all leading and trailing whitespace from the input (`stripall` option), lets the formatter output line numbers (`linenos` option), and sets the wrapping ``
``'s class to ``source`` (instead of ``highlight``). For an overview of builtin lexers and formatters and their options, visit the `lexer `_ and `formatters `_ lists. Lexer and formatter lookup ========================== If you want to lookup a built-in lexer by its alias or a filename, you can use one of the following methods: .. sourcecode:: pycon >>> from pygments.lexers import get_lexer_by_name, get_lexer_for_filename >>> get_lexer_by_name('python') >>> get_lexer_for_filename('spam.py') The same API is available for formatters: use `get_formatter_by_name` and `get_formatter_for_filename` from the `pygments.formatters` module for this purpose. Command line usage ================== You can use Pygments from the command line, using the `pygmentize` script:: $ pygmentize test.py will highlight the Python file test.py using ANSI escape sequences (a.k.a. terminal colors) and print the result to standard output. To output HTML, use the ``-f`` option:: $ pygmentize -f html -o test.html test.py to write an HTML-highlighted version of test.py to the file test.html. The stylesheet can be created with:: $ pygmentize -S default -f html > style.css More options and tricks and be found in the `command line referene `_.