Formatter for HTML output.
copyright: | Copyright 2006-2013 by the Pygments team, see AUTHORS. |
---|---|
license: | BSD, see LICENSE for details. |
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.