Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/formatters/bbcode.py: 7%
55 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1"""
2 pygments.formatters.bbcode
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 BBcode formatter.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
12from pygments.formatter import Formatter
13from pygments.util import get_bool_opt
15__all__ = ['BBCodeFormatter']
18class BBCodeFormatter(Formatter):
19 """
20 Format tokens with BBcodes. These formatting codes are used by many
21 bulletin boards, so you can highlight your sourcecode with pygments before
22 posting it there.
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:
35 `style`
36 The style to use, can be a string or a Style subclass (default:
37 ``'default'``).
39 `codetag`
40 If set to true, put the output into ``[code]`` tags (default:
41 ``false``)
43 `monofont`
44 If set to true, add a tag to show the code with a monospace font
45 (default: ``false``).
46 """
47 name = 'BBCode'
48 aliases = ['bbcode', 'bb']
49 filenames = []
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)
56 self.styles = {}
57 self._make_styles()
59 def _make_styles(self):
60 for ttype, ndef in self.style:
61 start = end = ''
62 if ndef['color']:
63 start += '[color=#%s]' % ndef['color']
64 end = '[/color]' + end
65 if ndef['bold']:
66 start += '[b]'
67 end = '[/b]' + end
68 if ndef['italic']:
69 start += '[i]'
70 end = '[/i]' + end
71 if ndef['underline']:
72 start += '[u]'
73 end = '[/u]' + 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):
79 if self._code:
80 outfile.write('[code]')
81 if self._mono:
82 outfile.write('[font=monospace]')
84 lastval = ''
85 lasttype = None
87 for ttype, value in tokensource:
88 while ttype not in self.styles:
89 ttype = ttype.parent
90 if ttype == lasttype:
91 lastval += value
92 else:
93 if lastval:
94 start, end = self.styles[lasttype]
95 outfile.write(''.join((start, lastval, end)))
96 lastval = value
97 lasttype = ttype
99 if lastval:
100 start, end = self.styles[lasttype]
101 outfile.write(''.join((start, lastval, end)))
103 if self._mono:
104 outfile.write('[/font]')
105 if self._code:
106 outfile.write('[/code]')
107 if self._code or self._mono:
108 outfile.write('\n')