bbcodeutils: BBCode parser and BBCode to HTML for Python

Yesterday I was looking for a Python module able to parse Bulletin Board Code (bbcode for friends) or able to transform bbcode to HTML.

After looking on the Internet I decided to create bbcodeutils a Python module to parse, generate and transform bbcode. I created in a way that I think is really simple to use.

Inside bbcodeutils you will find following classes:

  • bbcodeparser: parses bbcode and fixes invalid strings
  • bbcode2html: gets a parsed bbcode string and generates HTML from it
  • bbcodebuilder: good to generate BBCode programmatically

Here you can find the link to download bbcodeutils v1.0. You can use this module freely (see readme.txt for details).

Following there are some examples with the most common operations.


Before you try any example I’m assuming you will do something to load the bbcodeutils module, like:

from bbcodeutils import bbcodebuilder, bbcodeparser
Parsing bbcode strings

Parsing a bbcode string can be achieved using the bbcodeparser constructor, or calling parse method on a bbcodeparser object:

> bbcode = bbcodeparser('[b]bold string[/b]')


> bbcode = bbcodeparser()
> bbcode.parse ('[b]first string[/b]')

This is just to illustrate how to parse a string, but this won’t produce any output at all. What you are probably looking for is to fix an invalid bbcode string or to transform a bbcode string into valid HTML. Here we go…

Fix an invalid BBCode string

To fix an invalid bbcode string you just have to parse a string and stringify the parser object or to call the bbcode method.

> str(bbcode)
"This is [b]bold and [i]this bold and italics[/i][/b]."
> bbcode = bbcodeparser("This is [b]bold and [i]this bold and italics[/b].[/color]")
> bbcode.bbcode()
"This is [b]bold and [i]this bold and italics[/i][/b]."

Please note that the [/color] tag is removed and the closing [/i] tag is added properly.

Transform bbcode to HTML

The class bbcodeparser implements html method which uses bbcode2html internally. Calling this function will return valid HTML code.

> bbcode = bbcodeparser('[b]bold string[/b]')
> bbcode.html()
"<b>bold string</b>"
Create BBCode programmatically
> bbcode = bbcodebuilder()
> bbcode.b('string in bold')
"[b]string in bold[/b]"
> bbcode.url('')
> bbcode.url('Welcome to Codigo Manso', '')
"[url=]Welcome to Codigo Manso[/url]"
> bbcode.list('item 1', 'item 2')
   [*]item 1
   [*]item 2
Common operations in one line

Fix invalid BBCode in one line:

> str(bbcodeparser("[color=red]Fix this [b]string"))
"[color=red]Fix this [b]string[/b][/color]"

Transform BBCode to HTML in one line:

> str(bbcodeparser("[color=red]Fix this [b]string"))
'<span style="color: red;">Fix this <b>string</b></span>'

Thats it!

If you have any doubt feel free to leave a comment on this post

Enlaces de interés:

Trackback URL

, , ,

  1. Don Kreher
    21/11/2010 at 11:37 am Permalink


    I have found your very nice BBcode to HTML.

    I am looking for an ofline HTML to BBcode converter
    for the Mac. Do you have one?

    A LaTeX to BBcode converter would be even better.

    I also would like to find an offline viewer of BBcode for the Mac. Do you know of one?


  2. Pau Sánchez
    24/11/2010 at 4:37 pm Permalink

    I’m sorry Don, I have no idea about any HTML or LaTeX to BBCode converter 🙁

  3. rodgerson51
    04/06/2011 at 8:32 pm Permalink

    Inside bbcodeutils you will find following classes:Before you try any example I

  4. Greg Humphreys
    25/07/2011 at 6:17 am Permalink

    You should use “basestring” instead of “str” when checking if something is a string or dictionary so that it will be easier to use this with unicode input.