summaryrefslogtreecommitdiff
path: root/docs/src/quickstart.txt
blob: 91e0953fe4232211668b24220fd46c5c7c9699b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
.. -*- 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 then are passed to a formatter. Currently
formatters exist for HTML, LaTeX, RTF 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

    <div class="highlight">
    <pre><span class="k">print</span> <span class="s">&quot;Hello World&quot;</span></pre>
    </div>

As you can see, Pygments uses CSS classes (by default, but you can change that)
instead of inline styles in order to avoid outputting redundant style information over
and over. A CSS stylesheet that contains all CSS classes possibly used in the output
can be produced by:

.. sourcecode:: python

    print HtmlFormatter().get_style_defs('.highlight')

The argument to `get_style_defs` is used as an additional CSS selector: the output
may look like this:

.. 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 ``<div>``'s class to ``source`` (instead of
``highlight``).

For an overview of builtin lexers and formatters and their options, visit the
`lexer <lexers.txt>`_ and `formatters <formatters.txt>`_ 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_for_mimetype)

    >>> get_lexer_by_name('python')
    <pygments.lexers.PythonLexer>

    >>> get_lexer_for_filename('spam.rb')
    <pygments.lexers.RubyLexer>

    >>> get_lexer_for_mimetype('text/x-perl')
    <pygments.lexers.PerlLexer>

All these functions accept keyword arguments; they will be passed to the lexer
as options.

A similar API is available for formatters: use `get_formatter_by_name()` and
`get_formatter_for_filename()` from the `pygments.formatters` module
for this purpose.


Guessing lexers
===============

If you don't know the content of the file, or you want to highlight a file
whose extension is ambiguous, such as ``.html`` (which could contain plain HTML
or some template tags), use these functions:

.. sourcecode:: pycon

    >>> from pygments.lexers import guess_lexer, guess_lexer_for_filename

    >>> guess_lexer('#!/usr/bin/python\nprint "Hello World!"')
    <pygments.lexers.PythonLexer>

    >>> guess_lexer_for_filename('test.py', 'print "Hello World!"')
    <pygments.lexers.PythonLexer>

`guess_lexer()` passes the given content to the lexer classes' `analyze_text()`
method and returns the one for which it returns the highest number.

All lexers have two different filename pattern lists: the primary and the
secondary one. The `get_lexer_for_filename()` function only uses the primary
list, whose entries are supposed to be unique among all lexers.
`guess_lexer_for_filename()`, however, will first loop through all lexers and
look at the primary and secondary filename patterns if the filename matches.
If only one lexer matches, it is returned, else the guessing mechanism of
`guess_lexer()` is used with the matching lexers.

As usual, keyword arguments to these functions are given to the created lexer
as options.    


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 <cmdline.txt>`_.