Back to index

moin  1.9.0~rc2
bbcode.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     pygments.formatters.bbcode
00004     ~~~~~~~~~~~~~~~~~~~~~~~~~~
00005 
00006     BBcode formatter.
00007 
00008     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00009     :license: BSD, see LICENSE for details.
00010 """
00011 
00012 
00013 from pygments.formatter import Formatter
00014 from pygments.util import get_bool_opt
00015 
00016 __all__ = ['BBCodeFormatter']
00017 
00018 
00019 class BBCodeFormatter(Formatter):
00020     """
00021     Format tokens with BBcodes. These formatting codes are used by many
00022     bulletin boards, so you can highlight your sourcecode with pygments before
00023     posting it there.
00024 
00025     This formatter has no support for background colors and borders, as there
00026     are no common BBcode tags for that.
00027 
00028     Some board systems (e.g. phpBB) don't support colors in their [code] tag,
00029     so you can't use the highlighting together with that tag.
00030     Text in a [code] tag usually is shown with a monospace font (which this
00031     formatter can do with the ``monofont`` option) and no spaces (which you
00032     need for indentation) are removed.
00033 
00034     Additional options accepted:
00035 
00036     `style`
00037         The style to use, can be a string or a Style subclass (default:
00038         ``'default'``).
00039 
00040     `codetag`
00041         If set to true, put the output into ``[code]`` tags (default:
00042         ``false``)
00043 
00044     `monofont`
00045         If set to true, add a tag to show the code with a monospace font
00046         (default: ``false``).
00047     """
00048     name = 'BBCode'
00049     aliases = ['bbcode', 'bb']
00050     filenames = []
00051 
00052     def __init__(self, **options):
00053         Formatter.__init__(self, **options)
00054         self._code = get_bool_opt(options, 'codetag', False)
00055         self._mono = get_bool_opt(options, 'monofont', False)
00056 
00057         self.styles = {}
00058         self._make_styles()
00059 
00060     def _make_styles(self):
00061         for ttype, ndef in self.style:
00062             start = end = ''
00063             if ndef['color']:
00064                 start += '[color=#%s]' % ndef['color']
00065                 end = '[/color]' + end
00066             if ndef['bold']:
00067                 start += '[b]'
00068                 end = '[/b]' + end
00069             if ndef['italic']:
00070                 start += '[i]'
00071                 end = '[/i]' + end
00072             if ndef['underline']:
00073                 start += '[u]'
00074                 end = '[/u]' + end
00075             # there are no common BBcodes for background-color and border
00076 
00077             self.styles[ttype] = start, end
00078 
00079     def format_unencoded(self, tokensource, outfile):
00080         if self._code:
00081             outfile.write('[code]')
00082         if self._mono:
00083             outfile.write('[font=monospace]')
00084 
00085         lastval = ''
00086         lasttype = None
00087 
00088         for ttype, value in tokensource:
00089             while ttype not in self.styles:
00090                 ttype = ttype.parent
00091             if ttype == lasttype:
00092                 lastval += value
00093             else:
00094                 if lastval:
00095                     start, end = self.styles[lasttype]
00096                     outfile.write(''.join((start, lastval, end)))
00097                 lastval = value
00098                 lasttype = ttype
00099 
00100         if lastval:
00101             start, end = self.styles[lasttype]
00102             outfile.write(''.join((start, lastval, end)))
00103 
00104         if self._mono:
00105             outfile.write('[/font]')
00106         if self._code:
00107             outfile.write('[/code]')
00108         if self._code or self._mono:
00109             outfile.write('\n')