2 pygments.formatters.bbcode
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~
7 :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
12 from pip._vendor.pygments.formatter import Formatter
13 from pip._vendor.pygments.util import get_bool_opt
15 __all__ = ['BBCodeFormatter']
18 class BBCodeFormatter(Formatter):
20 Format tokens with BBcodes. These formatting codes are used by many
21 bulletin boards, so you can highlight your sourcecode with pygments before
24 This formatter has no support for background colors and borders, as there
25 are no common BBcode tags for that.
27 Some board systems (e.g. phpBB) don't support colors in their [code] tag,
28 so you can't use the highlighting together with that tag.
29 Text in a [code] tag usually is shown with a monospace font (which this
30 formatter can do with the ``monofont`` option) and no spaces (which you
31 need for indentation) are removed.
33 Additional options accepted:
36 The style to use, can be a string or a Style subclass (default:
40 If set to true, put the output into ``[code]`` tags (default:
44 If set to true, add a tag to show the code with a monospace font
48 aliases = ['bbcode', 'bb']
51 def __init__(self, **options):
52 Formatter.__init__(self, **options)
53 self._code = get_bool_opt(options, 'codetag', False)
54 self._mono = get_bool_opt(options, 'monofont', False)
59 def _make_styles(self):
60 for ttype, ndef in self.style:
63 start += '[color=#%s]' % ndef['color']
64 end = '[/color]' + end
74 # there are no common BBcodes for background-color and border
76 self.styles[ttype] = start, end
78 def format_unencoded(self, tokensource, outfile):
80 outfile.write('[code]')
82 outfile.write('[font=monospace]')
87 for ttype, value in tokensource:
88 while ttype not in self.styles:
94 start, end = self.styles[lasttype]
95 outfile.write(''.join((start, lastval, end)))
100 start, end = self.styles[lasttype]
101 outfile.write(''.join((start, lastval, end)))
104 outfile.write('[/font]')
106 outfile.write('[/code]')
107 if self._code or self._mono: