Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 35 lines (30 sloc) 1.693 kb
42cfa420 »
2010-11-11 add -I don't remeber- snippets
1 # -*- coding: cp949 -*-
2 import re
3
4 _BBcodeRexPatterns = [
5 [ re.compile("&"), r"&"],
6 [ re.compile("<"), r"<" ],
7 [ re.compile(">"), r">" ],
8 [ re.compile("'"), r'&apos;' ],
9 [ re.compile('"'), r'"' ],
10 [ re.compile('(\n|\r|\n)'), r'<br/>\1'],
11 [ re.compile('\[[Bb]\](.+?)\[/[Bb]\]'), r'<b>\1</b>' ],
12 [ re.compile('\[[Ii]\](.+?)\[/[Ii]\]'), r'<i>\1</i>' ],
13 [ re.compile('\[[Uu]\](.+?)\[/[Uu]\]'), r'<u>\1</u>' ],
14 [ re.compile('\[[Hh]([1-6])\](.+?)\[/[Hh]\1\]'), r'<h\1>\2</h\1>' ],
15 [ re.compile('(\s|^)(http|ftp)(://[^\s:]+)'), r'\1<a href="\2\3">\2\3</a>' ],
16 [ re.compile('(\s|^)(www\.[^\s/]+\.[a-zA-Z]{2,4}(/[^\s/]*)*)'), r'\1<a href="http://\2">\2</a>' ],
17 [ re.compile('(\s|^)(ftp\.[^\s/]+\.[a-zA-Z]{2,4}(/[^\s/]*)*)'), r'\1<a href="ftp://\2">\2</a>' ],
18 [ re.compile('(\s|^)([a-zA-Z0-9.]+@[a-zA-Z0-9.]+\.[a-zA-Z]{2,4})'), r'\1<a href="mailto:\2">\2</a>' ],
19 [ re.compile('\[[Uu][Rr][Ll]\](.+?)\[/[Uu][Rr][Ll]\]'), r'<a href="\1">\1</a>' ],
20 [ re.compile('\[[Uu][Rr][Ll]=([^\]]+)\](.+?)\[/[Uu][Rr][Ll]\]'), r'<a href="\1">\2</a>'],
21 [ re.compile('\[[Ii][Mm][Gg]\](.+?\.(png|gif|jpe?g|svg))\[/[Ii][Mm][Gg]\]'), r'<img src="\1"/>' ]]
22
23 def decodeBBcode(bbStr):
24 for fromPtrn, toPtrn in _BBcodeRexPatterns:
25 for p in fromPtrn.finditer(bbStr):
26 fromStr = p.group()
27 toStr = p.expand(toPtrn)
28 bbStr = bbStr.replace(fromStr,toStr)
29 return bbStr
30
31 if __name__ == '__main__':
32 bbStr = '[url=http://www.python.org]ÆÄÀ̽㸶À»[/url]\n[url=http://www.python.org]ÆÄÀ̽㸶À»2[/url]'
33 print decodeBBcode(bbStr)
34
Something went wrong with that request. Please try again.