Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
bbcode.py109 linesDownload Raw Back to formatters
1"""2    pygments.formatters.bbcode3    ~~~~~~~~~~~~~~~~~~~~~~~~~~4 5    BBcode formatter.6 7    :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.8    :license: BSD, see LICENSE for details.9"""10 11 12from pygments.formatter import Formatter13from pygments.util import get_bool_opt14 15__all__ = ['BBCodeFormatter']16 17 18class BBCodeFormatter(Formatter):19    """20    Format tokens with BBcodes. These formatting codes are used by many21    bulletin boards, so you can highlight your sourcecode with pygments before22    posting it there.23 24    This formatter has no support for background colors and borders, as there25    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 this30    formatter can do with the ``monofont`` option) and no spaces (which you31    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 font45        (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=#{}]'.format(ndef['color'])64                end = '[/color]' + end65            if ndef['bold']:66                start += '[b]'67                end = '[/b]' + end68            if ndef['italic']:69                start += '[i]'70                end = '[/i]' + end71            if ndef['underline']:72                start += '[u]'73                end = '[/u]' + end74            # there are no common BBcodes for background-color and border75 76            self.styles[ttype] = start, end77 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 = None86 87        for ttype, value in tokensource:88            while ttype not in self.styles:89                ttype = ttype.parent90            if ttype == lasttype:91                lastval += value92            else:93                if lastval:94                    start, end = self.styles[lasttype]95                    outfile.write(''.join((start, lastval, end)))96                lastval = value97                lasttype = ttype98 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')109 
codekingpro/portable-devtools · Team Ai