| 1 |
.. -*- mode: rst -*- |
| 2 |
|
| 3 |
=========================== |
| 4 |
Introduction and Quickstart |
| 5 |
=========================== |
| 6 |
|
| 7 |
|
| 8 |
Welcome to Pygments! This document explains the basic concepts and terms and |
| 9 |
gives a few examples of how to use the library. |
| 10 |
|
| 11 |
|
| 12 |
Architecture |
| 13 |
============ |
| 14 |
|
| 15 |
There are four types of components that work together highlighting a piece of |
| 16 |
code: |
| 17 |
|
| 18 |
* A **lexer** splits the source into tokens, fragments of the source that |
| 19 |
have a token type that determines what the text represents semantically |
| 20 |
(e.g., keyword, string, or comment). There is a lexer for every language |
| 21 |
or markup format that Pygments supports. |
| 22 |
* The token stream can be piped through **filters**, which usually modify |
| 23 |
the token types or text fragments, e.g. uppercasing all keywords. |
| 24 |
* A **formatter** then takes the token stream and writes it to an output |
| 25 |
file, in a format such as HTML, LaTeX or RTF. |
| 26 |
* While writing the output, a **style** determines how to highlight all the |
| 27 |
different token types. It maps them to attributes like "red and bold". |
| 28 |
|
| 29 |
|
| 30 |
Example |
| 31 |
======= |
| 32 |
|
| 33 |
Here is a small example for highlighting Python code: |
| 34 |
|
| 35 |
.. sourcecode:: python |
| 36 |
|
| 37 |
from pygments import highlight |
| 38 |
from pygments.lexers import PythonLexer |
| 39 |
from pygments.formatters import HtmlFormatter |
| 40 |
|
| 41 |
code = 'print "Hello World"' |
| 42 |
print(highlight(code, PythonLexer(), HtmlFormatter())) |
| 43 |
|
| 44 |
which prints something like this: |
| 45 |
|
| 46 |
.. sourcecode:: html |
| 47 |
|
| 48 |
<div class="highlight"> |
| 49 |
<pre><span class="k">print</span> <span class="s">"Hello World"</span></pre> |
| 50 |
</div> |
| 51 |
|
| 52 |
As you can see, Pygments uses CSS classes (by default, but you can change that) |
| 53 |
instead of inline styles in order to avoid outputting redundant style information over |
| 54 |
and over. A CSS stylesheet that contains all CSS classes possibly used in the output |
| 55 |
can be produced by: |
| 56 |
|
| 57 |
.. sourcecode:: python |
| 58 |
|
| 59 |
print(HtmlFormatter().get_style_defs('.highlight')) |
| 60 |
|
| 61 |
The argument to :meth:`get_style_defs() <pygments.formatter.Formatter.get_style_defs()>` is used as an additional CSS selector: |
| 62 |
the output may look like this: |
| 63 |
|
| 64 |
.. sourcecode:: css |
| 65 |
|
| 66 |
.highlight .k { color: #AA22FF; font-weight: bold } |
| 67 |
.highlight .s { color: #BB4444 } |
| 68 |
... |
| 69 |
|
| 70 |
|
| 71 |
Options |
| 72 |
======= |
| 73 |
|
| 74 |
The :func:`highlight() <pygments.highlight>` function supports a fourth argument called *outfile*, it |
| 75 |
must be a file object if given. The formatted output will then be written to |
| 76 |
this file instead of being returned as a string. |
| 77 |
|
| 78 |
Lexers and formatters both support options. They are given to them as keyword |
| 79 |
arguments either to the class or to the lookup method: |
| 80 |
|
| 81 |
.. sourcecode:: python |
| 82 |
|
| 83 |
from pygments import highlight |
| 84 |
from pygments.lexers import get_lexer_by_name |
| 85 |
from pygments.formatters import HtmlFormatter |
| 86 |
|
| 87 |
lexer = get_lexer_by_name("python", stripall=True) |
| 88 |
formatter = HtmlFormatter(linenos=True, cssclass="source") |
| 89 |
result = highlight(code, lexer, formatter) |
| 90 |
|
| 91 |
This makes the lexer strip all leading and trailing whitespace from the input |
| 92 |
(`stripall` option), lets the formatter output line numbers (`linenos` option), |
| 93 |
and sets the wrapping ``<div>``'s class to ``source`` (instead of |
| 94 |
``highlight``). |
| 95 |
|
| 96 |
Important options include: |
| 97 |
|
| 98 |
`encoding` : for lexers and formatters |
| 99 |
Since Pygments uses Unicode strings internally, this determines which |
| 100 |
encoding will be used to convert to or from byte strings. |
| 101 |
`style` : for formatters |
| 102 |
The name of the style to use when writing the output. |
| 103 |
|
| 104 |
|
| 105 |
For an overview of builtin lexers and formatters and their options, visit the |
| 106 |
:doc:`lexer <lexers>` and :doc:`formatters <formatters>` lists. |
| 107 |
|
| 108 |
For a documentation on filters, see :doc:`this page <filters>`. |
| 109 |
|
| 110 |
|
| 111 |
Lexer and formatter lookup |
| 112 |
========================== |
| 113 |
|
| 114 |
If you want to lookup a built-in lexer by its alias or a filename, you can use |
| 115 |
one of the following methods: |
| 116 |
|
| 117 |
.. sourcecode:: pycon |
| 118 |
|
| 119 |
>>> from pygments.lexers import (get_lexer_by_name, |
| 120 |
... get_lexer_for_filename, get_lexer_for_mimetype) |
| 121 |
|
| 122 |
>>> get_lexer_by_name('python') |
| 123 |
<pygments.lexers.PythonLexer> |
| 124 |
|
| 125 |
>>> get_lexer_for_filename('spam.rb') |
| 126 |
<pygments.lexers.RubyLexer> |
| 127 |
|
| 128 |
>>> get_lexer_for_mimetype('text/x-perl') |
| 129 |
<pygments.lexers.PerlLexer> |
| 130 |
|
| 131 |
All these functions accept keyword arguments; they will be passed to the lexer |
| 132 |
as options. |
| 133 |
|
| 134 |
A similar API is available for formatters: use :func:`.get_formatter_by_name()` |
| 135 |
and :func:`.get_formatter_for_filename()` from the :mod:`pygments.formatters` |
| 136 |
module for this purpose. |
| 137 |
|
| 138 |
|
| 139 |
Guessing lexers |
| 140 |
=============== |
| 141 |
|
| 142 |
If you don't know the content of the file, or you want to highlight a file |
| 143 |
whose extension is ambiguous, such as ``.html`` (which could contain plain HTML |
| 144 |
or some template tags), use these functions: |
| 145 |
|
| 146 |
.. sourcecode:: pycon |
| 147 |
|
| 148 |
>>> from pygments.lexers import guess_lexer, guess_lexer_for_filename |
| 149 |
|
| 150 |
>>> guess_lexer('#!/usr/bin/python\nprint "Hello World!"') |
| 151 |
<pygments.lexers.PythonLexer> |
| 152 |
|
| 153 |
>>> guess_lexer_for_filename('test.py', 'print "Hello World!"') |
| 154 |
<pygments.lexers.PythonLexer> |
| 155 |
|
| 156 |
:func:`.guess_lexer() <pygments.lexers.guess_lexer>` passes the given content to the lexer classes' |
| 157 |
:meth:`analyse_text() <pygments.lexer.Lexer.analyse_text>` method and returns the one for which it returns the |
| 158 |
highest number. |
| 159 |
|
| 160 |
All lexers have two different filename pattern lists: the primary and the |
| 161 |
secondary one. The :func:`.get_lexer_for_filename()` function only uses the |
| 162 |
primary list, whose entries are supposed to be unique among all lexers. |
| 163 |
:func:`.guess_lexer_for_filename()`, however, will first loop through all lexers |
| 164 |
and look at the primary and secondary filename patterns if the filename matches. |
| 165 |
If only one lexer matches, it is returned, else the guessing mechanism of |
| 166 |
:func:`.guess_lexer()` is used with the matching lexers. |
| 167 |
|
| 168 |
As usual, keyword arguments to these functions are given to the created lexer |
| 169 |
as options. |
| 170 |
|
| 171 |
|
| 172 |
Command line usage |
| 173 |
================== |
| 174 |
|
| 175 |
You can use Pygments from the command line, using the :program:`pygmentize` |
| 176 |
script:: |
| 177 |
|
| 178 |
$ pygmentize test.py |
| 179 |
|
| 180 |
will highlight the Python file test.py using ANSI escape sequences |
| 181 |
(a.k.a. terminal colors) and print the result to standard output. |
| 182 |
|
| 183 |
To output HTML, use the ``-f`` option:: |
| 184 |
|
| 185 |
$ pygmentize -f html -o test.html test.py |
| 186 |
|
| 187 |
to write an HTML-highlighted version of test.py to the file test.html. |
| 188 |
Note that it will only be a snippet of HTML, if you want a full HTML document, |
| 189 |
use the "full" option:: |
| 190 |
|
| 191 |
$ pygmentize -f html -O full -o test.html test.py |
| 192 |
|
| 193 |
This will produce a full HTML document with included stylesheet. |
| 194 |
|
| 195 |
A style can be selected with ``-O style=<name>``. |
| 196 |
|
| 197 |
If you need a stylesheet for an existing HTML file using Pygments CSS classes, |
| 198 |
it can be created with:: |
| 199 |
|
| 200 |
$ pygmentize -S default -f html > style.css |
| 201 |
|
| 202 |
where ``default`` is the style name. |
| 203 |
|
| 204 |
More options and tricks can be found in the :doc:`command line reference |
| 205 |
<cmdline>`. |