summaryrefslogtreecommitdiff
path: root/pygments/formatters
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2014-10-08 09:21:15 +0200
committerGeorg Brandl <georg@python.org>2014-10-08 09:21:15 +0200
commit444fb6fd9b3492040a36fcca672fee8175f8d603 (patch)
tree2bd411ca78decf276b95dc6b1788e594b2e35287 /pygments/formatters
parent491fec23ef01687906f5d71ee718522cd2917926 (diff)
parentc1bfe4eed3805d3556bffa3c6b9cc2d3f6976205 (diff)
downloadpygments-444fb6fd9b3492040a36fcca672fee8175f8d603.tar.gz
Merged in leodemoura/pygments-main (pull request #399)
Diffstat (limited to 'pygments/formatters')
-rw-r--r--pygments/formatters/__init__.py128
-rwxr-xr-xpygments/formatters/_mapping.py107
-rw-r--r--pygments/formatters/html.py8
-rw-r--r--pygments/formatters/img.py18
-rw-r--r--pygments/formatters/latex.py22
-rw-r--r--pygments/formatters/other.py5
-rw-r--r--pygments/formatters/rtf.py53
-rw-r--r--pygments/formatters/svg.py1
8 files changed, 182 insertions, 160 deletions
diff --git a/pygments/formatters/__init__.py b/pygments/formatters/__init__.py
index f0c5dc41..45ceaff6 100644
--- a/pygments/formatters/__init__.py
+++ b/pygments/formatters/__init__.py
@@ -8,63 +8,111 @@
:copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
-import os.path
-import fnmatch
+
import re
+import sys
+import types
+import fnmatch
+from os.path import basename
from pygments.formatters._mapping import FORMATTERS
from pygments.plugin import find_plugin_formatters
-from pygments.util import ClassNotFound
-
-ns = globals()
-for fcls in FORMATTERS:
- ns[fcls.__name__] = fcls
-del fcls
+from pygments.util import ClassNotFound, itervalues
__all__ = ['get_formatter_by_name', 'get_formatter_for_filename',
- 'get_all_formatters'] + [cls.__name__ for cls in FORMATTERS]
+ 'get_all_formatters'] + list(FORMATTERS)
+
+_formatter_cache = {} # classes by name
+_pattern_cache = {}
+
+
+def _fn_matches(fn, glob):
+ """Return whether the supplied file name fn matches pattern filename."""
+ if glob not in _pattern_cache:
+ pattern = _pattern_cache[glob] = re.compile(fnmatch.translate(glob))
+ return pattern.match(fn)
+ return _pattern_cache[glob].match(fn)
+
+
+def _load_formatters(module_name):
+ """Load a formatter (and all others in the module too)."""
+ mod = __import__(module_name, None, None, ['__all__'])
+ for formatter_name in mod.__all__:
+ cls = getattr(mod, formatter_name)
+ _formatter_cache[cls.name] = cls
+
+def get_all_formatters():
+ """Return a generator for all formatter classes."""
+ # NB: this returns formatter classes, not info like get_all_lexers().
+ for info in itervalues(FORMATTERS):
+ if info[1] not in _formatter_cache:
+ _load_formatters(info[0])
+ yield _formatter_cache[info[1]]
+ for _, formatter in find_plugin_formatters():
+ yield formatter
-_formatter_alias_cache = {}
-_formatter_filename_cache = []
-def _init_formatter_cache():
- if _formatter_alias_cache:
- return
- for cls in get_all_formatters():
- for alias in cls.aliases:
- _formatter_alias_cache[alias] = cls
- for fn in cls.filenames:
- _formatter_filename_cache.append((
- re.compile(fnmatch.translate(fn)), cls))
+def find_formatter_class(alias):
+ """Lookup a formatter by alias.
+ Returns None if not found.
+ """
+ for module_name, name, aliases, _, _ in itervalues(FORMATTERS):
+ if alias in aliases:
+ if name not in _formatter_cache:
+ _load_formatters(module_name)
+ return _formatter_cache[name]
+ for _, cls in find_plugin_formatters():
+ if alias in cls.aliases:
+ return cls
-def find_formatter_class(name):
- _init_formatter_cache()
- cls = _formatter_alias_cache.get(name, None)
- return cls
+def get_formatter_by_name(_alias, **options):
+ """Lookup and instantiate a formatter by alias.
-def get_formatter_by_name(name, **options):
- _init_formatter_cache()
- cls = _formatter_alias_cache.get(name, None)
- if not cls:
- raise ClassNotFound("No formatter found for name %r" % name)
+ Raises ClassNotFound if not found.
+ """
+ cls = find_formatter_class(_alias)
+ if cls is None:
+ raise ClassNotFound("No formatter found for name %r" % _alias)
return cls(**options)
def get_formatter_for_filename(fn, **options):
- _init_formatter_cache()
- fn = os.path.basename(fn)
- for pattern, cls in _formatter_filename_cache:
- if pattern.match(fn):
- return cls(**options)
+ """Lookup and instantiate a formatter by filename pattern.
+
+ Raises ClassNotFound if not found.
+ """
+ fn = basename(fn)
+ for modname, name, _, filenames, _ in itervalues(FORMATTERS):
+ for filename in filenames:
+ if _fn_matches(fn, filename):
+ if name not in _formatter_cache:
+ _load_formatters(modname)
+ return _formatter_cache[name](**options)
+ for cls in find_plugin_formatters():
+ for filename in cls.filenames:
+ if _fn_matches(fn, filename):
+ return cls(**options)
raise ClassNotFound("No formatter found for file name %r" % fn)
-def get_all_formatters():
- """Return a generator for all formatters."""
- for formatter in FORMATTERS:
- yield formatter
- for _, formatter in find_plugin_formatters():
- yield formatter
+class _automodule(types.ModuleType):
+ """Automatically import formatters."""
+
+ def __getattr__(self, name):
+ info = FORMATTERS.get(name)
+ if info:
+ _load_formatters(info[0])
+ cls = _formatter_cache[info[1]]
+ setattr(self, name, cls)
+ return cls
+ raise AttributeError(name)
+
+
+oldmod = sys.modules[__name__]
+newmod = _automodule(__name__)
+newmod.__dict__.update(oldmod.__dict__)
+sys.modules[__name__] = newmod
+del newmod.newmod, newmod.oldmod, newmod.sys, newmod.types
diff --git a/pygments/formatters/_mapping.py b/pygments/formatters/_mapping.py
index 8b3fc977..d4aeaeb0 100755
--- a/pygments/formatters/_mapping.py
+++ b/pygments/formatters/_mapping.py
@@ -14,45 +14,22 @@
"""
from __future__ import print_function
-try:
- import pygments
-except ImportError:
- # This block makes this mapping work like the lexer one -- not requiring
- # that Pygments already be on your PYTHONPATH.
- import os.path, sys
- sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
-
-# start
-from pygments.formatters.bbcode import BBCodeFormatter
-from pygments.formatters.html import HtmlFormatter
-from pygments.formatters.img import BmpImageFormatter
-from pygments.formatters.img import GifImageFormatter
-from pygments.formatters.img import ImageFormatter
-from pygments.formatters.img import JpgImageFormatter
-from pygments.formatters.latex import LatexFormatter
-from pygments.formatters.other import NullFormatter
-from pygments.formatters.other import RawTokenFormatter
-from pygments.formatters.other import TestcaseFormatter
-from pygments.formatters.rtf import RtfFormatter
-from pygments.formatters.svg import SvgFormatter
-from pygments.formatters.terminal import TerminalFormatter
-from pygments.formatters.terminal256 import Terminal256Formatter
FORMATTERS = {
- BBCodeFormatter: ('BBCode', ('bbcode', 'bb'), (), '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.'),
- BmpImageFormatter: ('img_bmp', ('bmp', 'bitmap'), ('*.bmp',), 'Create a bitmap image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
- GifImageFormatter: ('img_gif', ('gif',), ('*.gif',), 'Create a GIF image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
- HtmlFormatter: ('HTML', ('html',), ('*.html', '*.htm'), "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."),
- ImageFormatter: ('img', ('img', 'IMG', 'png'), ('*.png',), 'Create a PNG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
- JpgImageFormatter: ('img_jpg', ('jpg', 'jpeg'), ('*.jpg',), 'Create a JPEG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
- LatexFormatter: ('LaTeX', ('latex', 'tex'), ('*.tex',), 'Format tokens as LaTeX code. This needs the `fancyvrb` and `color` standard packages.'),
- NullFormatter: ('Text only', ('text', 'null'), ('*.txt',), 'Output the text unchanged without any formatting.'),
- RawTokenFormatter: ('Raw tokens', ('raw', 'tokens'), ('*.raw',), 'Format tokens as a raw representation for storing token streams.'),
- RtfFormatter: ('RTF', ('rtf',), ('*.rtf',), '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\xc2\xae Word\xc2\xae documents.'),
- SvgFormatter: ('SVG', ('svg',), ('*.svg',), '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.'),
- Terminal256Formatter: ('Terminal256', ('terminal256', 'console256', '256'), (), '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.'),
- TerminalFormatter: ('Terminal', ('terminal', 'console'), (), '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.'),
- TestcaseFormatter: ('Testcase', ('testcase',), (), 'Format tokens as appropriate for a new testcase.')
+ 'BBCodeFormatter': ('pygments.formatters.bbcode', 'BBCode', ('bbcode', 'bb'), (), '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.'),
+ 'BmpImageFormatter': ('pygments.formatters.img', 'img_bmp', ('bmp', 'bitmap'), ('*.bmp',), 'Create a bitmap image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
+ 'GifImageFormatter': ('pygments.formatters.img', 'img_gif', ('gif',), ('*.gif',), 'Create a GIF image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
+ 'HtmlFormatter': ('pygments.formatters.html', 'HTML', ('html',), ('*.html', '*.htm'), "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."),
+ 'ImageFormatter': ('pygments.formatters.img', 'img', ('img', 'IMG', 'png'), ('*.png',), 'Create a PNG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
+ 'JpgImageFormatter': ('pygments.formatters.img', 'img_jpg', ('jpg', 'jpeg'), ('*.jpg',), 'Create a JPEG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
+ 'LatexFormatter': ('pygments.formatters.latex', 'LaTeX', ('latex', 'tex'), ('*.tex',), 'Format tokens as LaTeX code. This needs the `fancyvrb` and `color` standard packages.'),
+ 'NullFormatter': ('pygments.formatters.other', 'Text only', ('text', 'null'), ('*.txt',), 'Output the text unchanged without any formatting.'),
+ 'RawTokenFormatter': ('pygments.formatters.other', 'Raw tokens', ('raw', 'tokens'), ('*.raw',), 'Format tokens as a raw representation for storing token streams.'),
+ 'RtfFormatter': ('pygments.formatters.rtf', 'RTF', ('rtf',), ('*.rtf',), '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\xc2\xae Word\xc2\xae documents.'),
+ 'SvgFormatter': ('pygments.formatters.svg', 'SVG', ('svg',), ('*.svg',), '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.'),
+ 'Terminal256Formatter': ('pygments.formatters.terminal256', 'Terminal256', ('terminal256', 'console256', '256'), (), '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.'),
+ 'TerminalFormatter': ('pygments.formatters.terminal', 'Terminal', ('terminal', 'console'), (), '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.'),
+ 'TestcaseFormatter': ('pygments.formatters.other', 'Testcase', ('testcase',), (), 'Format tokens as appropriate for a new testcase.')
}
if __name__ == '__main__':
@@ -65,39 +42,35 @@ if __name__ == '__main__':
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
from pygments.util import docstring_headline
- for filename in os.listdir('.'):
- if filename.endswith('.py') and not filename.startswith('_'):
- module_name = 'pygments.formatters.%s' % filename[:-3]
- print(module_name)
- module = __import__(module_name, None, None, [''])
- for formatter_name in module.__all__:
- imports.append((module_name, formatter_name))
- formatter = getattr(module, formatter_name)
- found_formatters.append(
- '%s: %r' % (formatter_name,
- (formatter.name,
- tuple(formatter.aliases),
- tuple(formatter.filenames),
- docstring_headline(formatter))))
- # sort them, that should make the diff files for svn smaller
+ for root, dirs, files in os.walk('.'):
+ for filename in files:
+ if filename.endswith('.py') and not filename.startswith('_'):
+ module_name = 'pygments.formatters%s.%s' % (
+ root[1:].replace('/', '.'), filename[:-3])
+ print(module_name)
+ module = __import__(module_name, None, None, [''])
+ for formatter_name in module.__all__:
+ formatter = getattr(module, formatter_name)
+ found_formatters.append(
+ '%r: %r' % (formatter_name,
+ (module_name,
+ formatter.name,
+ tuple(formatter.aliases),
+ tuple(formatter.filenames),
+ docstring_headline(formatter))))
+ # sort them to make the diff minimal
found_formatters.sort()
- imports.sort()
# extract useful sourcecode from this file
- f = open(__file__)
- try:
- content = f.read()
- finally:
- f.close()
- header = content[:content.find('# start')]
+ with open(__file__) as fp:
+ content = fp.read()
+ header = content[:content.find('FORMATTERS = {')]
footer = content[content.find("if __name__ == '__main__':"):]
# write new file
- f = open(__file__, 'w')
- f.write(header)
- f.write('# start\n')
- f.write('\n'.join(['from %s import %s' % imp for imp in imports]))
- f.write('\n\n')
- f.write('FORMATTERS = {\n %s\n}\n\n' % ',\n '.join(found_formatters))
- f.write(footer)
- f.close()
+ with open(__file__, 'w') as fp:
+ fp.write(header)
+ fp.write('FORMATTERS = {\n %s\n}\n\n' % ',\n '.join(found_formatters))
+ fp.write(footer)
+
+ print ('=== %d formatters processed.' % len(found_formatters))
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
index 970e595b..2c6de4ca 100644
--- a/pygments/formatters/html.py
+++ b/pygments/formatters/html.py
@@ -628,24 +628,24 @@ class HtmlFormatter(Formatter):
style = 'background-color: #ffffc0; padding: 0 5px 0 5px'
else:
style = 'background-color: #f0f0f0; padding: 0 5px 0 5px'
- yield 1, '<span style="%s">%*s</span> ' % (
+ yield 1, '<span style="%s">%*s </span>' % (
style, mw, (num%st and ' ' or num)) + line
num += 1
else:
for t, line in lines:
yield 1, ('<span style="background-color: #f0f0f0; '
- 'padding: 0 5px 0 5px">%*s</span> ' % (
+ 'padding: 0 5px 0 5px">%*s </span>' % (
mw, (num%st and ' ' or num)) + line)
num += 1
elif sp:
for t, line in lines:
- yield 1, '<span class="lineno%s">%*s</span> ' % (
+ yield 1, '<span class="lineno%s">%*s </span>' % (
num%sp == 0 and ' special' or '', mw,
(num%st and ' ' or num)) + line
num += 1
else:
for t, line in lines:
- yield 1, '<span class="lineno">%*s</span> ' % (
+ yield 1, '<span class="lineno">%*s </span>' % (
mw, (num%st and ' ' or num)) + line
num += 1
diff --git a/pygments/formatters/img.py b/pygments/formatters/img.py
index 8e2b5f9e..db5bee3b 100644
--- a/pygments/formatters/img.py
+++ b/pygments/formatters/img.py
@@ -295,6 +295,7 @@ class ImageFormatter(Formatter):
raise PilNotAvailable(
'Python Imaging Library is required for this formatter')
Formatter.__init__(self, **options)
+ self.encoding = 'latin1' # let pygments.format() do the right thing
# Read the style
self.styles = dict(self.style)
if self.style.background_color is None:
@@ -315,20 +316,20 @@ class ImageFormatter(Formatter):
self.line_number_fg = options.get('line_number_fg', '#886')
self.line_number_bg = options.get('line_number_bg', '#eed')
self.line_number_chars = get_int_opt(options,
- 'line_number_chars', 2)
+ 'line_number_chars', 2)
self.line_number_bold = get_bool_opt(options,
- 'line_number_bold', False)
+ 'line_number_bold', False)
self.line_number_italic = get_bool_opt(options,
- 'line_number_italic', False)
+ 'line_number_italic', False)
self.line_number_pad = get_int_opt(options, 'line_number_pad', 6)
self.line_numbers = get_bool_opt(options, 'line_numbers', True)
self.line_number_separator = get_bool_opt(options,
- 'line_number_separator', True)
+ 'line_number_separator', True)
self.line_number_step = get_int_opt(options, 'line_number_step', 1)
self.line_number_start = get_int_opt(options, 'line_number_start', 1)
if self.line_numbers:
self.line_number_width = (self.fontw * self.line_number_chars +
- self.line_number_pad * 2)
+ self.line_number_pad * 2)
else:
self.line_number_width = 0
self.hl_lines = []
@@ -437,7 +438,7 @@ class ImageFormatter(Formatter):
# quite complex.
value = value.expandtabs(4)
lines = value.splitlines(True)
- #print lines
+ # print lines
for i, line in enumerate(lines):
temp = line.rstrip('\n')
if temp:
@@ -478,9 +479,8 @@ class ImageFormatter(Formatter):
draw = ImageDraw.Draw(im)
recth = im.size[-1]
rectw = self.image_pad + self.line_number_width - self.line_number_pad
- draw.rectangle([(0, 0),
- (rectw, recth)],
- fill=self.line_number_bg)
+ draw.rectangle([(0, 0), (rectw, recth)],
+ fill=self.line_number_bg)
draw.line([(rectw, 0), (rectw, recth)], fill=self.line_number_fg)
del draw
diff --git a/pygments/formatters/latex.py b/pygments/formatters/latex.py
index 352684b0..4b22215f 100644
--- a/pygments/formatters/latex.py
+++ b/pygments/formatters/latex.py
@@ -236,6 +236,13 @@ class LatexFormatter(Formatter):
set. (default: ``''``).
.. versionadded:: 2.0
+
+ `envname`
+ Allows you to pick an alternative environment name replacing Verbatim.
+ The alternate environment still has to support Verbatim's option syntax.
+ (default: ``'Verbatim'``).
+
+ .. versionadded:: 2.0
"""
name = 'LaTeX'
aliases = ['latex', 'tex']
@@ -254,16 +261,15 @@ class LatexFormatter(Formatter):
self.texcomments = get_bool_opt(options, 'texcomments', False)
self.mathescape = get_bool_opt(options, 'mathescape', False)
self.escapeinside = options.get('escapeinside', '')
-
if len(self.escapeinside) == 2:
self.left = self.escapeinside[0]
self.right = self.escapeinside[1]
else:
self.escapeinside = ''
+ self.envname = options.get('envname', u'Verbatim')
self._create_stylesheet()
-
def _create_stylesheet(self):
t2n = self.ttype2name = {Token: ''}
c2d = self.cmd2def = {}
@@ -271,7 +277,7 @@ class LatexFormatter(Formatter):
def rgbcolor(col):
if col:
- return ','.join(['%.2f' %(int(col[i] + col[i + 1], 16) / 255.0)
+ return ','.join(['%.2f' % (int(col[i] + col[i + 1], 16) / 255.0)
for i in (0, 2, 4)])
else:
return '1,1,1'
@@ -331,7 +337,7 @@ class LatexFormatter(Formatter):
realoutfile = outfile
outfile = StringIO()
- outfile.write(u'\\begin{Verbatim}[commandchars=\\\\\\{\\}')
+ outfile.write(u'\\begin{' + self.envname + u'}[commandchars=\\\\\\{\\}')
if self.linenos:
start, step = self.linenostart, self.linenostep
outfile.write(u',numbers=left' +
@@ -371,9 +377,9 @@ class LatexFormatter(Formatter):
text = value
value = ''
while len(text) > 0:
- a,sep1,text = text.partition(self.left)
+ a, sep1, text = text.partition(self.left)
if len(sep1) > 0:
- b,sep2,text = text.partition(self.right)
+ b, sep2, text = text.partition(self.right)
if len(sep2) > 0:
value += escape_tex(a, self.commandprefix) + b
else:
@@ -404,7 +410,7 @@ class LatexFormatter(Formatter):
else:
outfile.write(value)
- outfile.write(u'\\end{Verbatim}\n')
+ outfile.write(u'\\end{' + self.envname + u'}\n')
if self.full:
realoutfile.write(DOC_TEMPLATE %
@@ -436,6 +442,7 @@ class LatexEmbeddedLexer(Lexer):
def get_tokens_unprocessed(self, text):
buf = ''
+ idx = 0
for i, t, v in self.lang.get_tokens_unprocessed(text):
if t in Token.Comment or t in Token.String:
if buf:
@@ -467,4 +474,3 @@ class LatexEmbeddedLexer(Lexer):
yield index, Token.Error, sep1
index += len(sep1)
text = b
-
diff --git a/pygments/formatters/other.py b/pygments/formatters/other.py
index c8269b19..d8e5f4f7 100644
--- a/pygments/formatters/other.py
+++ b/pygments/formatters/other.py
@@ -62,9 +62,8 @@ class RawTokenFormatter(Formatter):
def __init__(self, **options):
Formatter.__init__(self, **options)
- if self.encoding:
- raise OptionError('the raw formatter does not support the '
- 'encoding option')
+ # We ignore self.encoding if it is set, since it gets set for lexer
+ # and formatter if given with -Oencoding on the command line.
self.encoding = 'ascii' # let pygments.format() do the right thing
self.compress = get_choice_opt(options, 'compress',
['', 'none', 'gz', 'bz2'], '')
diff --git a/pygments/formatters/rtf.py b/pygments/formatters/rtf.py
index cf65a927..abecd484 100644
--- a/pygments/formatters/rtf.py
+++ b/pygments/formatters/rtf.py
@@ -48,8 +48,6 @@ class RtfFormatter(Formatter):
aliases = ['rtf']
filenames = ['*.rtf']
- unicodeoutput = False
-
def __init__(self, **options):
"""
Additional options accepted:
@@ -67,14 +65,14 @@ class RtfFormatter(Formatter):
self.fontsize = get_int_opt(options, 'fontsize', 0)
def _escape(self, text):
- return text.replace('\\', '\\\\') \
- .replace('{', '\\{') \
- .replace('}', '\\}')
+ return text.replace(u'\\', u'\\\\') \
+ .replace(u'{', u'\\{') \
+ .replace(u'}', u'\\}')
def _escape_text(self, text):
# empty strings, should give a small performance improvment
if not text:
- return ''
+ return u''
# escape text
text = self._escape(text)
@@ -87,22 +85,21 @@ class RtfFormatter(Formatter):
buf.append(str(c))
elif (2**7) <= cn < (2**16):
# single unicode escape sequence
- buf.append(r'{\u%d}' % cn)
+ buf.append(u'{\\u%d}' % cn)
elif (2**16) <= cn:
# RTF limits unicode to 16 bits.
# Force surrogate pairs
- h,l = _surrogatepair(cn)
- buf.append(r'{\u%d}{\u%d}' % (h,l))
+ buf.append(u'{\\u%d}{\\u%d}' % _surrogatepair(cn))
- return ''.join(buf).replace('\n', '\\par\n')
+ return u''.join(buf).replace(u'\n', u'\\par\n')
def format_unencoded(self, tokensource, outfile):
# rtf 1.8 header
- outfile.write(r'{\rtf1\ansi\uc0\deff0'
- r'{\fonttbl{\f0\fmodern\fprq1\fcharset0%s;}}'
- r'{\colortbl;' % (self.fontface and
- ' ' + self._escape(self.fontface) or
- ''))
+ outfile.write(u'{\\rtf1\\ansi\\uc0\\deff0'
+ u'{\\fonttbl{\\f0\\fmodern\\fprq1\\fcharset0%s;}}'
+ u'{\\colortbl;' % (self.fontface and
+ u' ' + self._escape(self.fontface) or
+ u''))
# convert colors and save them in a mapping to access them later.
color_mapping = {}
@@ -111,15 +108,15 @@ class RtfFormatter(Formatter):
for color in style['color'], style['bgcolor'], style['border']:
if color and color not in color_mapping:
color_mapping[color] = offset
- outfile.write(r'\red%d\green%d\blue%d;' % (
+ outfile.write(u'\\red%d\\green%d\\blue%d;' % (
int(color[0:2], 16),
int(color[2:4], 16),
int(color[4:6], 16)
))
offset += 1
- outfile.write(r'}\f0 ')
+ outfile.write(u'}\\f0 ')
if self.fontsize:
- outfile.write(r'\fs%d' % (self.fontsize))
+ outfile.write(u'\\fs%d' % (self.fontsize))
# highlight stream
for ttype, value in tokensource:
@@ -128,23 +125,23 @@ class RtfFormatter(Formatter):
style = self.style.style_for_token(ttype)
buf = []
if style['bgcolor']:
- buf.append(r'\cb%d' % color_mapping[style['bgcolor']])
+ buf.append(u'\\cb%d' % color_mapping[style['bgcolor']])
if style['color']:
- buf.append(r'\cf%d' % color_mapping[style['color']])
+ buf.append(u'\\cf%d' % color_mapping[style['color']])
if style['bold']:
- buf.append(r'\b')
+ buf.append(u'\\b')
if style['italic']:
- buf.append(r'\i')
+ buf.append(u'\\i')
if style['underline']:
- buf.append(r'\ul')
+ buf.append(u'\\ul')
if style['border']:
- buf.append(r'\chbrdr\chcfpat%d' %
+ buf.append(u'\\chbrdr\\chcfpat%d' %
color_mapping[style['border']])
- start = ''.join(buf)
+ start = u''.join(buf)
if start:
- outfile.write('{%s ' % start)
+ outfile.write(u'{%s ' % start)
outfile.write(self._escape_text(value))
if start:
- outfile.write('}')
+ outfile.write(u'}')
- outfile.write('}')
+ outfile.write(u'}')
diff --git a/pygments/formatters/svg.py b/pygments/formatters/svg.py
index 07636943..4e534fa2 100644
--- a/pygments/formatters/svg.py
+++ b/pygments/formatters/svg.py
@@ -78,7 +78,6 @@ class SvgFormatter(Formatter):
filenames = ['*.svg']
def __init__(self, **options):
- # XXX outencoding
Formatter.__init__(self, **options)
self.nowrap = get_bool_opt(options, 'nowrap', False)
self.fontfamily = options.get('fontfamily', 'monospace')