1
# -*- coding: utf-8 -*-
3
pygments.formatters.bbcode
4
~~~~~~~~~~~~~~~~~~~~~~~~~~
8
:copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
:license: BSD, see LICENSE for details.
13
from pygments.formatter import Formatter
14
from pygments.util import get_bool_opt
16
__all__ = ['BBCodeFormatter']
19
class BBCodeFormatter(Formatter):
21
Format tokens with BBcodes. These formatting codes are used by many
22
bulletin boards, so you can highlight your sourcecode with pygments before
25
This formatter has no support for background colors and borders, as there
26
are no common BBcode tags for that.
28
Some board systems (e.g. phpBB) don't support colors in their [code] tag,
29
so you can't use the highlighting together with that tag.
30
Text in a [code] tag usually is shown with a monospace font (which this
31
formatter can do with the ``monofont`` option) and no spaces (which you
32
need for indentation) are removed.
34
Additional options accepted:
37
The style to use, can be a string or a Style subclass (default:
41
If set to true, put the output into ``[code]`` tags (default:
45
If set to true, add a tag to show the code with a monospace font
49
aliases = ['bbcode', 'bb']
52
def __init__(self, **options):
53
Formatter.__init__(self, **options)
54
self._code = get_bool_opt(options, 'codetag', False)
55
self._mono = get_bool_opt(options, 'monofont', False)
60
def _make_styles(self):
61
for ttype, ndef in self.style:
64
start += '[color=#%s]' % ndef['color']
65
end = '[/color]' + end
75
# there are no common BBcodes for background-color and border
77
self.styles[ttype] = start, end
79
def format_unencoded(self, tokensource, outfile):
81
outfile.write('[code]')
83
outfile.write('[font=monospace]')
88
for ttype, value in tokensource:
89
while ttype not in self.styles:
95
start, end = self.styles[lasttype]
96
outfile.write(''.join((start, lastval, end)))
101
start, end = self.styles[lasttype]
102
outfile.write(''.join((start, lastval, end)))
105
outfile.write('[/font]')
107
outfile.write('[/code]')
108
if self._code or self._mono: