register filter def bbcode value bbdata url url href url url href link

 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
@register.filter
def bbcode(value):
bbdata = [
(r'\[url\](.+?)\[/url\]', r'<a href="\1">\1</a>'),
(r'\[url=(.+?)\](.+?)\[/url\]', r'<a href="\1">\2</a>'),
(r'\[link\](.+?)\[/link\]', r'<a href="\1">\1</a>'),
(r'\[link=(.+?)\](.+?)\[/link\]', 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'\[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>'),
(r'\[br\]', r'<br />'),
]
for bbset in bbdata:
p = re.compile(bbset[0], re.DOTALL)
value = p.sub(bbset[1], value)
return value