Back to index

moin  1.9.0~rc2
formatter.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     pygments.formatter
00004     ~~~~~~~~~~~~~~~~~~
00005 
00006     Base formatter class.
00007 
00008     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00009     :license: BSD, see LICENSE for details.
00010 """
00011 
00012 import codecs
00013 
00014 from pygments.util import get_bool_opt
00015 from pygments.styles import get_style_by_name
00016 
00017 __all__ = ['Formatter']
00018 
00019 
00020 def _lookup_style(style):
00021     if isinstance(style, basestring):
00022         return get_style_by_name(style)
00023     return style
00024 
00025 
00026 class Formatter(object):
00027     """
00028     Converts a token stream to text.
00029 
00030     Options accepted:
00031 
00032     ``style``
00033         The style to use, can be a string or a Style subclass
00034         (default: "default"). Not used by e.g. the
00035         TerminalFormatter.
00036     ``full``
00037         Tells the formatter to output a "full" document, i.e.
00038         a complete self-contained document. This doesn't have
00039         any effect for some formatters (default: false).
00040     ``title``
00041         If ``full`` is true, the title that should be used to
00042         caption the document (default: '').
00043     ``encoding``
00044         If given, must be an encoding name. This will be used to
00045         convert the Unicode token strings to byte strings in the
00046         output. If it is "" or None, Unicode strings will be written
00047         to the output file, which most file-like objects do not
00048         support (default: None).
00049     ``outencoding``
00050         Overrides ``encoding`` if given.
00051     """
00052 
00053     #: Name of the formatter
00054     name = None
00055 
00056     #: Shortcuts for the formatter
00057     aliases = []
00058 
00059     #: fn match rules
00060     filenames = []
00061 
00062     #: If True, this formatter outputs Unicode strings when no encoding
00063     #: option is given.
00064     unicodeoutput = True
00065 
00066     def __init__(self, **options):
00067         self.style = _lookup_style(options.get('style', 'default'))
00068         self.full  = get_bool_opt(options, 'full', False)
00069         self.title = options.get('title', '')
00070         self.encoding = options.get('encoding', None) or None
00071         self.encoding = options.get('outencoding', None) or self.encoding
00072         self.options = options
00073 
00074     def get_style_defs(self, arg=''):
00075         """
00076         Return the style definitions for the current style as a string.
00077 
00078         ``arg`` is an additional argument whose meaning depends on the
00079         formatter used. Note that ``arg`` can also be a list or tuple
00080         for some formatters like the html formatter.
00081         """
00082         return ''
00083 
00084     def format(self, tokensource, outfile):
00085         """
00086         Format ``tokensource``, an iterable of ``(tokentype, tokenstring)``
00087         tuples and write it into ``outfile``.
00088         """
00089         if self.encoding:
00090             # wrap the outfile in a StreamWriter
00091             outfile = codecs.lookup(self.encoding)[3](outfile)
00092         return self.format_unencoded(tokensource, outfile)