Pygments formatters.
copyright: | Copyright 2006-2013 by the Pygments team, see AUTHORS. |
---|---|
license: | BSD, see LICENSE for details. |
Format tokens as RTF markup. This formatter automatically outputs full RTF documents with color information and other useful stuff. Perfect for Copy and Paste into Microsoft® Word® documents.
New in Pygments 0.6.
Additional options accepted:
Format tokens with ANSI color sequences, for output in a 256-color terminal or console. Like in TerminalFormatter color sequences are terminated at newlines, so that paging the output works correctly.
The formatter takes colors from a style defined by the style option and converts them to nearest ANSI 256-color escape sequences. Bold and underline attributes from the style are preserved (and displayed).
New in Pygments 0.9.
Options accepted:
Format tokens as LaTeX code. This needs the fancyvrb and color standard packages.
Without the full option, code is formatted as one Verbatim environment, like this:
\begin{Verbatim}[commandchars=\\{\}]
\PY{k}{def }\PY{n+nf}{foo}(\PY{n}{bar}):
\PY{k}{pass}
\end{Verbatim}
The special command used here (\PY) and all the other macros it needs are output by the get_style_defs method.
With the full option, a complete LaTeX document is output, including the command definitions in the preamble.
The get_style_defs() method of a LatexFormatter returns a string containing \def commands defining the macros needed inside the Verbatim environments.
Additional options accepted:
The LaTeX commands used to produce colored output are constructed using this prefix and some letters (default: 'PY'). New in Pygments 0.7.
New in Pygments 0.10: the default is now 'PY' instead of 'C'.
Return the command sequences needed to define the commands used to format text in the verbatim environment. arg is ignored.
Format tokens with BBcodes. These formatting codes are used by many bulletin boards, so you can highlight your sourcecode with pygments before posting it there.
This formatter has no support for background colors and borders, as there are no common BBcode tags for that.
Some board systems (e.g. phpBB) don’t support colors in their [code] tag, so you can’t use the highlighting together with that tag. Text in a [code] tag usually is shown with a monospace font (which this formatter can do with the monofont option) and no spaces (which you need for indentation) are removed.
Additional options accepted:
Output the text unchanged without any formatting.
Format tokens as a raw representation for storing token streams.
The format is tokentype<TAB>repr(tokenstring)\n. The output can later be converted to a token stream with the RawTokenLexer, described in the lexer list.
Only two options are accepted:
Format tokens as HTML 4 <span> tags within a <pre> tag, wrapped in a <div> tag. The <div>‘s CSS class can be set by the cssclass option.
If the linenos option is set to "table", the <pre> is additionally wrapped inside a <table> which has one row and two cells: one containing the line numbers and one containing the code. Example:
<div class="highlight" >
<table><tr>
<td class="linenos" title="click to toggle"
onclick="with (this.firstChild.style)
{ display = (display == '') ? 'none' : '' }">
<pre>1
2</pre>
</td>
<td class="code">
<pre><span class="Ke">def </span><span class="NaFu">foo</span>(bar):
<span class="Ke">pass</span>
</pre>
</td>
</tr></table></div>
(whitespace added to improve clarity).
Wrapping can be disabled using the nowrap option.
A list of lines can be specified using the hl_lines option to make these lines highlighted (as of Pygments 0.11).
With the full option, a complete HTML 4 document is output, including the style definitions inside a <style> tag, or in a separate file if the cssfile option is given.
When tagsfile is set to the path of a ctags index file, it is used to generate hyperlinks from names to their definition. You must enable anchorlines and run ctags with the -n option for this to work. The python-ctags module from PyPI must be installed to use this feature; otherwise a RuntimeError will be raised.
The get_style_defs(arg=’‘) method of a HtmlFormatter returns a string containing CSS rules for the CSS classes used by the formatter. The argument arg can be used to specify additional CSS selectors that are prepended to the classes. A call fmter.get_style_defs(‘td .code’) would result in the following CSS classes:
td .code .kw { font-weight: bold; color: #00FF00 }
td .code .cm { color: #999999 }
...
If you have Pygments 0.6 or higher, you can also pass a list or tuple to the get_style_defs() method to request multiple prefixes for the tokens:
formatter.get_style_defs(['div.syntax pre', 'pre.syntax'])
The output would then look like this:
div.syntax pre .kw,
pre.syntax .kw { font-weight: bold; color: #00FF00 }
div.syntax pre .cm,
pre.syntax .cm { color: #999999 }
...
Additional options accepted:
CSS class for the wrapping <div> tag (default: 'highlight'). If you set this option, the default selector for get_style_defs() will be this class.
New in Pygments 0.9: If you select the 'table' line numbers, the wrapping table will have a CSS class of this string plus 'table', the default is accordingly 'highlighttable'.
If set to 'table', output line numbers as a table with two cells, one containing the line numbers, the other the whole code. This is copy-and-paste-friendly, but may cause alignment problems with some browsers or fonts. If set to 'inline', the line numbers will be integrated in the <pre> tag that contains the code (that setting is new in Pygments 0.8).
For compatibility with Pygments 0.7 and earlier, every true value except 'inline' means the same as 'table' (in particular, that means also True).
The default value is False, which means no line numbers at all.
Note: with the default (“table”) line number mechanism, the line numbers and code can have different line heights in Internet Explorer unless you give the enclosing <pre> tags an explicit line-height CSS property (you get the default line spacing with line-height: 125%).
Subclassing the HTML formatter
New in Pygments 0.7.
The HTML formatter is now built in a way that allows easy subclassing, thus customizing the output HTML code. The format() method calls self._format_lines() which returns a generator that yields tuples of (1, line), where the 1 indicates that the line is a line of the formatted source code.
If the nowrap option is set, the generator is the iterated over and the resulting HTML is output.
Otherwise, format() calls self.wrap(), which wraps the generator with other generators. These may add some HTML code to the one generated by _format_lines(), either by modifying the lines generated by the latter, then yielding them again with (1, line), and/or by yielding other HTML code before or after the lines, with (0, html). The distinction between source lines and other code makes it possible to wrap the generator multiple times.
The default wrap() implementation adds a <div> and a <pre> tag.
A custom HtmlFormatter subclass could look like this:
class CodeHtmlFormatter(HtmlFormatter):
def wrap(self, source, outfile):
return self._wrap_code(source)
def _wrap_code(self, source):
yield 0, '<code>'
for i, t in source:
if i == 1:
# it's a line of formatted code
t += '<br>'
yield i, t
yield 0, '</code>'
This results in wrapping the formatted lines with a <code> tag, where the source lines are broken using <br> tags.
After calling wrap(), the format() method also adds the “line numbers” and/or “full document” wrappers if the respective options are set. Then, all HTML yielded by the wrapped generator is output.
The formatting process uses several nested generators; which of them are used is determined by the user’s options.
Each generator should take at least one argument, inner, and wrap the pieces of text generated by this.
Always yield 2-tuples: (code, text). If “code” is 1, the text is part of the original tokensource being highlighted, if it’s 0, the text is some piece of wrapping. This makes it possible to use several different wrappers that process the original source linewise, e.g. line number generators.
Return CSS style definitions for the classes produced by the current highlighting style. arg can be a string or list of selectors to insert before the token type classes.
Wrap the source, which is a generator yielding individual lines, in custom generators. See docstring for format. Can be overridden.
Format tokens as an SVG graphics file. This formatter is still experimental. Each line of code is a <text> element with explicit x and y coordinates containing <tspan> elements with the individual token styles.
By default, this formatter outputs a full SVG document including doctype declaration and the <svg> root element.
New in Pygments 0.9.
Additional options accepted:
Format tokensource, an iterable of (tokentype, tokenstring) tuples and write it into outfile.
For our implementation we put all lines in their own ‘line group’.
Create a JPEG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.
New in Pygments 1.0. (You could create JPEG images before by passing a suitable image_format option to the ImageFormatter.)
Format tokens with ANSI color sequences, for output in a text console. Color sequences are terminated at newlines, so that paging the output works correctly.
The get_style_defs() method doesn’t do anything special since there is no support for common styles.
Options accepted:
Create a GIF image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.
New in Pygments 1.0. (You could create GIF images before by passing a suitable image_format option to the ImageFormatter.)
Create a bitmap image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.
New in Pygments 1.0. (You could create bitmap images before by passing a suitable image_format option to the ImageFormatter.)
Create a PNG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.
New in Pygments 0.10.
Additional options accepted:
An image format to output to that is recognised by PIL, these include:
The extra spacing (in pixels) between each line of text.
Default: 2
The font name to be used as the base font from which others, such as bold and italic fonts will be generated. This really should be a monospace font to look sane.
Default: “Bitstream Vera Sans Mono”
The font size in points to be used.
Default: 14
The padding, in pixels to be used at each edge of the resulting image.
Default: 10
Whether line numbers should be shown: True/False
Default: True
The line number of the first line.
Default: 1
The step used when printing line numbers.
Default: 1
The background colour (in “#123456” format) of the line number bar, or None to use the style background color.
Default: “#eed”
The text color of the line numbers (in “#123456”-like format).
Default: “#886”
The number of columns of line numbers allowable in the line number margin.
Default: 2
Whether line numbers will be bold: True/False
Default: False
Whether line numbers will be italicized: True/False
Default: False
Whether a line will be drawn between the line number area and the source code area: True/False
Default: True
The horizontal padding (in pixels) between the line number margin, and the source code area.
Default: 6
Specify a list of lines to be highlighted. New in Pygments 1.2.
Default: empty list
Specify the color for highlighting lines. New in Pygments 1.2.
Default: highlight color of the selected style
Format tokensource, an iterable of (tokentype, tokenstring) tuples and write it into outfile.
This implementation calculates where it should draw each token on the pixmap, then calculates the required pixmap size and draws the items.
_mapping.py | bbcode.py | html.py | img.py | latex.py | other.py | rtf.py | svg.py | terminal.py | terminal256.py