2be2b4e31292d8364b404f16ef4c654f9d89681a
[SubU] /
1 """
2     pygments.formatters.bbcode
3     ~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5     BBcode formatter.
6
7     :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
8     :license: BSD, see LICENSE for details.
9 """
10
11
12 from pip._vendor.pygments.formatter import Formatter
13 from pip._vendor.pygments.util import get_bool_opt
14
15 __all__ = ['BBCodeFormatter']
16
17
18 class 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.
23
24     This formatter has no support for background colors and borders, as there
25     are no common BBcode tags for that.
26
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.
32
33     Additional options accepted:
34
35     `style`
36         The style to use, can be a string or a Style subclass (default:
37         ``'default'``).
38
39     `codetag`
40         If set to true, put the output into ``[code]`` tags (default:
41         ``false``)
42
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 = []
50
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)
55
56         self.styles = {}
57         self._make_styles()
58
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
75
76             self.styles[ttype] = start, end
77
78     def format_unencoded(self, tokensource, outfile):
79         if self._code:
80             outfile.write('[code]')
81         if self._mono:
82             outfile.write('[font=monospace]')
83
84         lastval = ''
85         lasttype = None
86
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
98
99         if lastval:
100             start, end = self.styles[lasttype]
101             outfile.write(''.join((start, lastval, end)))
102
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')