diff options
author | Georg Brandl <georg@python.org> | 2014-03-04 14:17:10 +0100 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2014-03-04 14:17:10 +0100 |
commit | 18348a61d7e90b03a624fdc78fafdcb46b92307d (patch) | |
tree | e4fe1541ad9e2ada2de394eb2e020e2a0916ce94 /pygments/formatters | |
parent | cd9c0b70635f2a6c65ea97d042537478a0a95b7a (diff) | |
parent | 27895fe85076d2f1b44e7d30387b3f459fc60281 (diff) | |
download | pygments-18348a61d7e90b03a624fdc78fafdcb46b92307d.tar.gz |
merge with raichoo/pygments-main (pull request #210)
Diffstat (limited to 'pygments/formatters')
-rw-r--r-- | pygments/formatters/__init__.py | 8 | ||||
-rwxr-xr-x | pygments/formatters/_mapping.py | 10 | ||||
-rw-r--r-- | pygments/formatters/bbcode.py | 2 | ||||
-rw-r--r-- | pygments/formatters/html.py | 69 | ||||
-rw-r--r-- | pygments/formatters/img.py | 35 | ||||
-rw-r--r-- | pygments/formatters/latex.py | 28 | ||||
-rw-r--r-- | pygments/formatters/other.py | 11 | ||||
-rw-r--r-- | pygments/formatters/rtf.py | 4 | ||||
-rw-r--r-- | pygments/formatters/svg.py | 4 | ||||
-rw-r--r-- | pygments/formatters/terminal.py | 2 | ||||
-rw-r--r-- | pygments/formatters/terminal256.py | 4 |
11 files changed, 107 insertions, 70 deletions
diff --git a/pygments/formatters/__init__.py b/pygments/formatters/__init__.py index d842b96b..f0c5dc41 100644 --- a/pygments/formatters/__init__.py +++ b/pygments/formatters/__init__.py @@ -5,11 +5,12 @@ Pygments formatters. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ import os.path import fnmatch +import re from pygments.formatters._mapping import FORMATTERS from pygments.plugin import find_plugin_formatters @@ -34,7 +35,8 @@ def _init_formatter_cache(): for alias in cls.aliases: _formatter_alias_cache[alias] = cls for fn in cls.filenames: - _formatter_filename_cache.append((fn, cls)) + _formatter_filename_cache.append(( + re.compile(fnmatch.translate(fn)), cls)) def find_formatter_class(name): @@ -55,7 +57,7 @@ def get_formatter_for_filename(fn, **options): _init_formatter_cache() fn = os.path.basename(fn) for pattern, cls in _formatter_filename_cache: - if fnmatch.fnmatch(fn, pattern): + if pattern.match(fn): return cls(**options) raise ClassNotFound("No formatter found for file name %r" % fn) diff --git a/pygments/formatters/_mapping.py b/pygments/formatters/_mapping.py index a423ba50..79f592b3 100755 --- a/pygments/formatters/_mapping.py +++ b/pygments/formatters/_mapping.py @@ -3,16 +3,18 @@ pygments.formatters._mapping ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Formatter mapping defintions. This file is generated by itself. Everytime - you change something on a builtin formatter defintion, run this script from + Formatter mapping definitions. This file is generated by itself. Everytime + you change something on a builtin formatter definition, run this script from the formatters folder to update it. Do not alter the FORMATTERS dictionary by hand. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ +from __future__ import print_function + # start from pygments.formatters.bbcode import BBCodeFormatter from pygments.formatters.html import HtmlFormatter @@ -57,7 +59,7 @@ if __name__ == '__main__': for filename in os.listdir('.'): if filename.endswith('.py') and not filename.startswith('_'): module_name = 'pygments.formatters.%s' % filename[:-3] - print module_name + print(module_name) module = __import__(module_name, None, None, ['']) for formatter_name in module.__all__: imports.append((module_name, formatter_name)) diff --git a/pygments/formatters/bbcode.py b/pygments/formatters/bbcode.py index 15faff6c..21525f1c 100644 --- a/pygments/formatters/bbcode.py +++ b/pygments/formatters/bbcode.py @@ -5,7 +5,7 @@ BBcode formatter. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py index 06096930..3bc60e8a 100644 --- a/pygments/formatters/html.py +++ b/pygments/formatters/html.py @@ -5,18 +5,20 @@ Formatter for HTML output. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ +from __future__ import print_function + import os import sys import os.path -import StringIO from pygments.formatter import Formatter from pygments.token import Token, Text, STANDARD_TYPES -from pygments.util import get_bool_opt, get_int_opt, get_list_opt, bytes +from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \ + StringIO, string_types, iteritems try: import ctags @@ -218,29 +220,34 @@ class HtmlFormatter(Formatter): 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'``. + .. versionadded:: 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'``. `cssstyles` Inline CSS styles for the wrapping ``<div>`` tag (default: ``''``). `prestyles` - Inline CSS styles for the ``<pre>`` tag (default: ``''``). *New in - Pygments 0.11.* + Inline CSS styles for the ``<pre>`` tag (default: ``''``). + + .. versionadded:: 0.11 `cssfile` If the `full` option is true and this option is given, it must be the name of an external file. If the filename does not include an absolute path, the file's path will be assumed to be relative to the main output file's path, if the latter can be found. The stylesheet is then written - to this file instead of the HTML file. *New in Pygments 0.6.* + to this file instead of the HTML file. + + .. versionadded:: 0.6 `noclobber_cssfile` If `cssfile` is given and the specified file exists, the css file will not be overwritten. This allows the use of the `full` option in combination with a user specified css file. Default is ``False``. - *New in Pygments 1.1.* + + .. versionadded:: 1.1 `linenos` If set to ``'table'``, output line numbers as a table with two cells, @@ -263,7 +270,9 @@ class HtmlFormatter(Formatter): 125%``). `hl_lines` - Specify a list of lines to be highlighted. *New in Pygments 0.11.* + Specify a list of lines to be highlighted. + + .. versionadded:: 0.11 `linenostart` The line number for the first line (default: ``1``). @@ -279,24 +288,30 @@ class HtmlFormatter(Formatter): If set to ``True``, the formatter won't output the background color for the wrapping element (this automatically defaults to ``False`` when there is no wrapping element [eg: no argument for the - `get_syntax_defs` method given]) (default: ``False``). *New in - Pygments 0.6.* + `get_syntax_defs` method given]) (default: ``False``). + + .. versionadded:: 0.6 `lineseparator` This string is output between lines of code. It defaults to ``"\n"``, which is enough to break a line inside ``<pre>`` tags, but you can - e.g. set it to ``"<br>"`` to get HTML line breaks. *New in Pygments - 0.7.* + e.g. set it to ``"<br>"`` to get HTML line breaks. + + .. versionadded:: 0.7 `lineanchors` If set to a nonempty string, e.g. ``foo``, the formatter will wrap each output line in an anchor tag with a ``name`` of ``foo-linenumber``. - This allows easy linking to certain lines. *New in Pygments 0.9.* + This allows easy linking to certain lines. + + .. versionadded:: 0.9 `linespans` If set to a nonempty string, e.g. ``foo``, the formatter will wrap each output line in a span tag with an ``id`` of ``foo-linenumber``. - This allows easy access to lines via javascript. *New in Pygments 1.6.* + This allows easy access to lines via javascript. + + .. versionadded:: 1.6 `anchorlinenos` If set to `True`, will wrap line numbers in <a> tags. Used in @@ -306,18 +321,20 @@ class HtmlFormatter(Formatter): If set to the path of a ctags file, wrap names in anchor tags that link to their definitions. `lineanchors` should be used, and the tags file should specify line numbers (see the `-n` option to ctags). - *New in Pygments 1.6.* + + .. versionadded:: 1.6 `tagurlformat` A string formatting pattern used to generate links to ctags definitions. Available variables are `%(path)s`, `%(fname)s` and `%(fext)s`. Defaults to an empty string, resulting in just `#prefix-number` links. - *New in Pygments 1.6.* + + .. versionadded:: 1.6 **Subclassing the HTML formatter** - *New in Pygments 0.7.* + .. versionadded:: 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 @@ -453,7 +470,7 @@ class HtmlFormatter(Formatter): """ if arg is None: arg = ('cssclass' in self.options and '.'+self.cssclass or '') - if isinstance(arg, basestring): + if isinstance(arg, string_types): args = [arg] else: args = list(arg) @@ -467,7 +484,7 @@ class HtmlFormatter(Formatter): return ', '.join(tmp) styles = [(level, ttype, cls, style) - for cls, (style, ttype, level) in self.class2style.iteritems() + for cls, (style, ttype, level) in iteritems(self.class2style) if cls and style] styles.sort() lines = ['%s { %s } /* %s */' % (prefix(cls), style, repr(ttype)[6:]) @@ -505,8 +522,8 @@ class HtmlFormatter(Formatter): cssfilename = os.path.join(os.path.dirname(filename), self.cssfile) except AttributeError: - print >>sys.stderr, 'Note: Cannot determine output file name, ' \ - 'using current directory as base for the CSS file name' + print('Note: Cannot determine output file name, ' \ + 'using current directory as base for the CSS file name', file=sys.stderr) cssfilename = self.cssfile # write CSS file only if noclobber_cssfile isn't given as an option. try: @@ -515,7 +532,7 @@ class HtmlFormatter(Formatter): cf.write(CSSFILE_TEMPLATE % {'styledefs': self.get_style_defs('body')}) cf.close() - except IOError, err: + except IOError as err: err.strerror = 'Error writing CSS file: ' + err.strerror raise @@ -534,7 +551,7 @@ class HtmlFormatter(Formatter): yield 0, DOC_FOOTER def _wrap_tablelinenos(self, inner): - dummyoutfile = StringIO.StringIO() + dummyoutfile = StringIO() lncount = 0 for t, line in inner: if t: diff --git a/pygments/formatters/img.py b/pygments/formatters/img.py index 394c3b6a..8e2b5f9e 100644 --- a/pygments/formatters/img.py +++ b/pygments/formatters/img.py @@ -5,15 +5,15 @@ Formatter for Pixmap output. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ import sys from pygments.formatter import Formatter -from pygments.util import get_bool_opt, get_int_opt, \ - get_list_opt, get_choice_opt +from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \ + get_choice_opt, xrange # Import this carefully try: @@ -25,7 +25,10 @@ except ImportError: try: import _winreg except ImportError: - _winreg = None + try: + import winreg as _winreg + except ImportError: + _winreg = None __all__ = ['ImageFormatter', 'GifImageFormatter', 'JpgImageFormatter', 'BmpImageFormatter'] @@ -72,7 +75,10 @@ class FontManager(object): self._create_nix() def _get_nix_font_path(self, name, style): - from commands import getstatusoutput + try: + from commands import getstatusoutput + except ImportError: + from subprocess import getstatusoutput exit, out = getstatusoutput('fc-list "%s:style=%s" file' % (name, style)) if not exit: @@ -169,7 +175,7 @@ class ImageFormatter(Formatter): 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.* + .. versionadded:: 0.10 Additional options accepted: @@ -258,12 +264,16 @@ class ImageFormatter(Formatter): Default: 6 `hl_lines` - Specify a list of lines to be highlighted. *New in Pygments 1.2.* + Specify a list of lines to be highlighted. + + .. versionadded:: 1.2 Default: empty list `hl_color` - Specify the color for highlighting lines. *New in Pygments 1.2.* + Specify the color for highlighting lines. + + .. versionadded:: 1.2 Default: highlight color of the selected style """ @@ -513,8 +523,7 @@ class GifImageFormatter(ImageFormatter): 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`.) + .. versionadded:: 1.0 """ name = 'img_gif' @@ -528,8 +537,7 @@ class JpgImageFormatter(ImageFormatter): 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`.) + .. versionadded:: 1.0 """ name = 'img_jpg' @@ -543,8 +551,7 @@ class BmpImageFormatter(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`.) + .. versionadded:: 1.0 """ name = 'img_bmp' diff --git a/pygments/formatters/latex.py b/pygments/formatters/latex.py index 47fd1239..0f2397eb 100644 --- a/pygments/formatters/latex.py +++ b/pygments/formatters/latex.py @@ -5,13 +5,16 @@ Formatter for LaTeX fancyvrb output. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ +from __future__ import division + from pygments.formatter import Formatter from pygments.token import Token, STANDARD_TYPES -from pygments.util import get_bool_opt, get_int_opt, StringIO +from pygments.util import get_bool_opt, get_int_opt, StringIO, xrange, \ + iteritems __all__ = ['LatexFormatter'] @@ -152,7 +155,7 @@ class LatexFormatter(Formatter): .. sourcecode:: latex - \begin{Verbatim}[commandchars=\\{\}] + \begin{Verbatim}[commandchars=\\\{\}] \PY{k}{def }\PY{n+nf}{foo}(\PY{n}{bar}): \PY{k}{pass} \end{Verbatim} @@ -205,19 +208,24 @@ class LatexFormatter(Formatter): `commandprefix` 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'``. + .. versionadded:: 0.7 + .. versionchanged:: 0.10 + The default is now ``'PY'`` instead of ``'C'``. `texcomments` If set to ``True``, enables LaTeX comment lines. That is, LaTex markup in comment tokens is not escaped so that LaTeX can render it (default: - ``False``). *New in Pygments 1.2.* + ``False``). + + .. versionadded:: 1.2 `mathescape` If set to ``True``, enables LaTeX math mode escape in comments. That is, ``'$...$'`` inside a comment will trigger math mode (default: - ``False``). *New in Pygments 1.2.* + ``False``). + + .. versionadded:: 1.2 """ name = 'LaTeX' aliases = ['latex', 'tex'] @@ -291,7 +299,7 @@ class LatexFormatter(Formatter): """ cp = self.commandprefix styles = [] - for name, definition in self.cmd2def.iteritems(): + for name, definition in iteritems(self.cmd2def): styles.append(r'\expandafter\def\csname %s@tok@%s\endcsname{%s}' % (cp, name, definition)) return STYLE_TEMPLATE % {'cp': self.commandprefix, @@ -306,14 +314,14 @@ class LatexFormatter(Formatter): realoutfile = outfile outfile = StringIO() - outfile.write(ur'\begin{Verbatim}[commandchars=\\\{\}') + outfile.write(r'\begin{Verbatim}[commandchars=\\\{\}') if self.linenos: start, step = self.linenostart, self.linenostep outfile.write(u',numbers=left' + (start and u',firstnumber=%d' % start or u'') + (step and u',stepnumber=%d' % step or u'')) if self.mathescape or self.texcomments: - outfile.write(ur',codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}') + outfile.write(r',codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}') if self.verboptions: outfile.write(u',' + self.verboptions) outfile.write(u']\n') diff --git a/pygments/formatters/other.py b/pygments/formatters/other.py index 1029a7a7..7368a642 100644 --- a/pygments/formatters/other.py +++ b/pygments/formatters/other.py @@ -5,12 +5,12 @@ Other formatters: NullFormatter, RawTokenFormatter. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ from pygments.formatter import Formatter -from pygments.util import OptionError, get_choice_opt, b +from pygments.util import OptionError, get_choice_opt from pygments.token import Token from pygments.console import colorize @@ -40,7 +40,7 @@ class RawTokenFormatter(Formatter): 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 <lexers.txt>`_. + :doc:`lexer list <lexers>`. Only two options are accepted: @@ -50,7 +50,8 @@ class RawTokenFormatter(Formatter): `error_color` If set to a color name, highlight error tokens using that color. If set but with no value, defaults to ``'red'``. - *New in Pygments 0.11.* + + .. versionadded:: 0.11 """ name = 'Raw tokens' @@ -79,7 +80,7 @@ class RawTokenFormatter(Formatter): def format(self, tokensource, outfile): try: - outfile.write(b('')) + outfile.write(b'') except TypeError: raise TypeError('The raw tokens formatter needs a binary ' 'output file') diff --git a/pygments/formatters/rtf.py b/pygments/formatters/rtf.py index 3efda284..59d97742 100644 --- a/pygments/formatters/rtf.py +++ b/pygments/formatters/rtf.py @@ -5,7 +5,7 @@ A formatter that generates RTF files. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ @@ -21,7 +21,7 @@ class RtfFormatter(Formatter): documents with color information and other useful stuff. Perfect for Copy and Paste into Microsoft® Word® documents. - *New in Pygments 0.6.* + .. versionadded:: 0.6 Additional options accepted: diff --git a/pygments/formatters/svg.py b/pygments/formatters/svg.py index 271f22a7..07636943 100644 --- a/pygments/formatters/svg.py +++ b/pygments/formatters/svg.py @@ -5,7 +5,7 @@ Formatter for SVG output. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ @@ -35,7 +35,7 @@ class SvgFormatter(Formatter): By default, this formatter outputs a full SVG document including doctype declaration and the ``<svg>`` root element. - *New in Pygments 0.9.* + .. versionadded:: 0.9 Additional options accepted: diff --git a/pygments/formatters/terminal.py b/pygments/formatters/terminal.py index 94e078f2..539b0be9 100644 --- a/pygments/formatters/terminal.py +++ b/pygments/formatters/terminal.py @@ -5,7 +5,7 @@ Formatter for terminal output with ANSI sequences. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ diff --git a/pygments/formatters/terminal256.py b/pygments/formatters/terminal256.py index 772ed423..60b698c9 100644 --- a/pygments/formatters/terminal256.py +++ b/pygments/formatters/terminal256.py @@ -11,7 +11,7 @@ Formatter version 1. - :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ @@ -76,7 +76,7 @@ class Terminal256Formatter(Formatter): 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.* + .. versionadded:: 0.9 Options accepted: |