Version 7 (modified by anonymous, 8 years ago) (diff)

Comment about [quote]

CookBook - Template filter BBCode

django version : 0.96

Many boards allow BBCode to format posts without allowing HTML. This is a save way to allow things like urls, emails, lists, etc.

This filter should be used in conjunction with the escape filter so you first escape the variable to strip out html and then filter it through bbcode.

Please note that this is a quickly written piece of code, and it isn't even fully tested. It is up to you to test it and improve it :) Feel free to edit it right here to make it even better.

import re
from django import template
register = template.Library()

def bbcode(value):

    bbdata = [
        (r'\[url\](.+?)\[/url\]', r'<a href="\1">\1</a>'),
        (r'\[url=(.+?)\](.+?)\[/url\]', r'<a href="\1">\2</a>'),
        (r'\[email\](.+?)\[/email\]', r'<a href="mailto:\1">\1</a>'),
        (r'\[email=(.+?)\](.+?)\[/email\]', r'<a href="mailto:\1">\2</a>'),
        (r'\[img\](.+?)\[/img\]', r'<img src="\1">'),
        (r'\[img=(.+?)\](.+?)\[/img\]', r'<img src="\1" alt="\2">'),
        (r'\[b\](.+?)\[/b\]', r'<b>\1</b>'),
        (r'\[i\](.+?)\[/i\]', r'<i>\1</i>'),
        (r'\[u\](.+?)\[/u\]', r'<u>\1</u>'),
        (r'\[quote\](.+?)\[/quote\]', r'<div style="margin-left: 1cm">\1</div>'),
        (r'\[center\](.+?)\[/center\]', r'<div align="center">\1</div>'),
        (r'\[code\](.+?)\[/code\]', r'<tt>\1</tt>'),
        (r'\[big\](.+?)\[/big\]', r'<big>\1</big>'),
        (r'\[small\](.+?)\[/small\]', r'<small>\1</small>'),

    for bbset in bbdata:
        p = re.compile(bbset[0], re.DOTALL)
        value = p.sub(bbset[1], value)

    #The following two code parts handle the more complex list statements
    temp = ''
    p = re.compile(r'\[list\](.+?)\[/list\]', re.DOTALL)
    m =
    if m:
        items = re.split(re.escape('[*]'),
        for i in items[1:]:
            temp = temp + '<li>' + i + '</li>'
        value = p.sub(r'<ul>'+temp+'</ul>', value)

    temp = ''
    p = re.compile(r'\[list=(.)\](.+?)\[/list\]', re.DOTALL)
    m =
    if m:
        items = re.split(re.escape('[*]'),
        for i in items[1:]:
            temp = temp + '<li>' + i + '</li>'
        value = p.sub(r'<ol type=\1>'+temp+'</ol>', value)

    return value

An alternative is an XHTML compliant and very well tested bbcode parser I wrote, now included in Ian Holsman's forum repository:

If the code is a block, how to parse it? -- Limodou

{% filter bbcode %}data to translate{% endfilter %} -- nesh

What I siad is:

def p(a):
    print a

The "[code]" is a paragraph, but not a single string line. How to parse it? And if the bbcode has newline, there seems no process for it. ---- Limodou

I noticed that there is no handling of quote where a user name is given. phpBB, for example, has [quote="A User's Name"]textquote. ---- Powerlord

Back to Top