Welcome, guest | Sign In | My Account | Store | Cart

After trying to write a BBCode support library, I decided that less in more. Though incomplete in its support of BBCode, it handles most cases with a minimal amount of code. Simply type "BBCode." followed by the operator. Call this with the text to be wrapped along with any arguments that operator may take. The virtual methods are case-sensitive, but the rendered names are returned in upper-case. If you are trying to write some forum-related software and want a lite BBCode implimentation, this recipe may serve your purposes well. The only aspect of this project that might be fixed at the user's discretion is support for numbered and bulleted lists (to be added to the _BBCode class as methods).

Python, 164 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
class _BBCode:

    def __getattr__(self, name):
        method = _Virtual(name)
        setattr(self, name, method)
        return method

class _Virtual:

    def __init__(self, name):
        self.__name = name.upper()

    def __call__(self, string, *args):
        return '[{0}{1}]{2}[/{0}]'.format(self.__name, ('=' + ','.join(map(str, args))) if args else '', string)

BBCode = _BBCode()

################################################################################

# EXAMPLE FOLLOWS

BBCode.b('A bold sentence')
'[B]A bold sentence[/B]'

>>> BBCode.i('Italics')
'[I]Italics[/I]'

>>> BBCode.u('underline')
'[U]underline[/U]'

>>> 'I want one ' + BBCode.s('million') + ' billion dollars'
'I want one [S]million[/S] billion dollars'

>>> BBCode.sub('sub') + 'marine'
'[SUB]sub[/SUB]marine'

>>> 'x' + BBCode.sup(2)
'x[SUP]2[/SUP]'

>>> BBCode.big('big')
'[BIG]big[/BIG]'

>>> BBCode.big(BBCode.big('bigger'))
'[BIG][BIG]bigger[/BIG][/BIG]'

>>> BBCode.small('smaller text')
'[SMALL]smaller text[/SMALL]'

>>> BBCode.color('Green text', 'green')
'[COLOR=green]Green text[/COLOR]'

>>> BBCode.bgcolor('A background for text', 'lightgreen')
'[BGCOLOR=lightgreen]A background for text[/BGCOLOR]'

>>> BBCode.border('bordered', 'red')
'[BORDER=red]bordered[/BORDER]'

>>> BBCode.border('bordered', 'blue', 3)
'[BORDER=blue,3]bordered[/BORDER]'

>>> BBCode.border('bordered', 'green', 1, 'dotted')
'[BORDER=green,1,dotted]bordered[/BORDER]'

>>> BBCode.center('centered')
'[CENTER]centered[/CENTER]'

>>> BBCode.right('right aligned')
'[RIGHT]right aligned[/RIGHT]'

>>> BBCode.font('A different font', 'verdana')
'[FONT=verdana]A different font[/FONT]'

>>> BBCode.quote('Party time', 'Joe')
'[QUOTE=Joe]Party time[/QUOTE]'

>>> BBCode.quote('Party time')
'[QUOTE]Party time[/QUOTE]'

>>> print(BBCode.html('''<html>
<head>
<title>Steel Headquarters</title>
</head>
<body>
<p class="intro">Welcome</p>
</body>
</html>'''))
[HTML]<html>
<head>
<title>Steel Headquarters</title>
</head>
<body>
<p class="intro">Welcome</p>
</body>
</html>[/HTML]

>>> BBCode.nocode('lets show someone how to make text bigger ' + BBCode.big('bigger text'))
'[NOCODE]lets show someone how to make text bigger [BIG]bigger text[/BIG][/NOCODE]'

>>> BBCode.code('''int x = y;
x++;''')
'[CODE]int x = y;\nx++;[/CODE]'

>>> BBCode.url('Google', 'http://www.google.com/')
'[URL=http://www.google.com/]Google[/URL]'

>>> BBCode.url('http://www.google.com/')
'[URL]http://www.google.com/[/URL]'

>>> BBCode.img('http://www.google.com/logos/july4th09.gif')
'[IMG]http://www.google.com/logos/july4th09.gif[/IMG]'

>>> BBCode.email('Email me', 'user@example.com')
'[EMAIL=user@example.com]Email me[/EMAIL]'

>>> BBCode.email('user@example.com')
'[EMAIL]user@example.com[/EMAIL]'

>>> BBCode.flash('http://www.youtube.com/v/z7SeHqxOVYc', 250, 210)
'[FLASH=250,210]http://www.youtube.com/v/z7SeHqxOVYc[/FLASH]'

>>> BBCode.list(''.join(map(lambda item: '[*]' + item, ('Item 1', 'Item 2', 'Item 3'))))
'[LIST][*]Item 1[*]Item 2[*]Item 3[/LIST]'

>>> BBCode.list(''.join(map(lambda item: '[*]' + item, ('Item 1', 'Item 2', 'Item 3'))), 1)
'[LIST=1][*]Item 1[*]Item 2[*]Item 3[/LIST]'

>>> 'You may want to hide something from accidental viewing:' + BBCode.spoiler('or the secret may be spoiled')
'You may want to hide something from accidental viewing:[SPOILER]or the secret may be spoiled[/SPOILER]'

>>>

################################################################################

# EXAMPLE PATCH

class _Patch(_BBCode):

    def __init__(self, **kwargs):
        for key in kwargs:
            setattr(self, key, kwargs[key])

    def bulleted(self, *items):
        return '[LIST]' + ''.join(map(lambda item: '[*]' + item, items)) + '[/LIST]'

    def numbered(self, *items):
        return '[LIST=1]' + ''.join(map(lambda item: '[*]' + item, items)) + '[/LIST]'

#############################################

>>> BBCode = _Patch(SELF='[me]', RULE='[hr]')

>>> BBCode.bulleted('Item 1', 'Item 2', 'Item 3')
'[LIST][*]Item 1[*]Item 2[*]Item 3[/LIST]'

>>> BBCode.numbered('Item 1', 'Item 2', 'Item 3')
'[LIST=1][*]Item 1[*]Item 2[*]Item 3[/LIST]'

>>> BBCode.SELF
'[me]'

>>> BBCode.RULE
'[hr]'

>>>

1 comment

Evan Fosmark 14 years, 9 months ago  # | flag

Pretty neat, Stephen. One thing, though. You titled this as BBCode Support and provided a great way of GENERATING BBCode, but no way of parsing it.

Created by Stephen Chappell on Sat, 4 Jul 2009 (MIT)
Python recipes (4591)
Stephen Chappell's recipes (233)

Required Modules

  • (none specified)

Other Information and Tasks