~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/support/pygments/formatters/bbcode.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
"""
3
 
    pygments.formatters.bbcode
4
 
    ~~~~~~~~~~~~~~~~~~~~~~~~~~
5
 
 
6
 
    BBcode formatter.
7
 
 
8
 
    :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
 
    :license: BSD, see LICENSE for details.
10
 
"""
11
 
 
12
 
 
13
 
from pygments.formatter import Formatter
14
 
from pygments.util import get_bool_opt
15
 
 
16
 
__all__ = ['BBCodeFormatter']
17
 
 
18
 
 
19
 
class BBCodeFormatter(Formatter):
20
 
    """
21
 
    Format tokens with BBcodes. These formatting codes are used by many
22
 
    bulletin boards, so you can highlight your sourcecode with pygments before
23
 
    posting it there.
24
 
 
25
 
    This formatter has no support for background colors and borders, as there
26
 
    are no common BBcode tags for that.
27
 
 
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.
33
 
 
34
 
    Additional options accepted:
35
 
 
36
 
    `style`
37
 
        The style to use, can be a string or a Style subclass (default:
38
 
        ``'default'``).
39
 
 
40
 
    `codetag`
41
 
        If set to true, put the output into ``[code]`` tags (default:
42
 
        ``false``)
43
 
 
44
 
    `monofont`
45
 
        If set to true, add a tag to show the code with a monospace font
46
 
        (default: ``false``).
47
 
    """
48
 
    name = 'BBCode'
49
 
    aliases = ['bbcode', 'bb']
50
 
    filenames = []
51
 
 
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)
56
 
 
57
 
        self.styles = {}
58
 
        self._make_styles()
59
 
 
60
 
    def _make_styles(self):
61
 
        for ttype, ndef in self.style:
62
 
            start = end = ''
63
 
            if ndef['color']:
64
 
                start += '[color=#%s]' % ndef['color']
65
 
                end = '[/color]' + end
66
 
            if ndef['bold']:
67
 
                start += '[b]'
68
 
                end = '[/b]' + end
69
 
            if ndef['italic']:
70
 
                start += '[i]'
71
 
                end = '[/i]' + end
72
 
            if ndef['underline']:
73
 
                start += '[u]'
74
 
                end = '[/u]' + end
75
 
            # there are no common BBcodes for background-color and border
76
 
 
77
 
            self.styles[ttype] = start, end
78
 
 
79
 
    def format_unencoded(self, tokensource, outfile):
80
 
        if self._code:
81
 
            outfile.write('[code]')
82
 
        if self._mono:
83
 
            outfile.write('[font=monospace]')
84
 
 
85
 
        lastval = ''
86
 
        lasttype = None
87
 
 
88
 
        for ttype, value in tokensource:
89
 
            while ttype not in self.styles:
90
 
                ttype = ttype.parent
91
 
            if ttype == lasttype:
92
 
                lastval += value
93
 
            else:
94
 
                if lastval:
95
 
                    start, end = self.styles[lasttype]
96
 
                    outfile.write(''.join((start, lastval, end)))
97
 
                lastval = value
98
 
                lasttype = ttype
99
 
 
100
 
        if lastval:
101
 
            start, end = self.styles[lasttype]
102
 
            outfile.write(''.join((start, lastval, end)))
103
 
 
104
 
        if self._mono:
105
 
            outfile.write('[/font]')
106
 
        if self._code:
107
 
            outfile.write('[/code]')
108
 
        if self._code or self._mono:
109
 
            outfile.write('\n')