-
Notifications
You must be signed in to change notification settings - Fork 0
/
bb2wiki.py
74 lines (44 loc) · 1.56 KB
/
bb2wiki.py
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
"Converts part of BBCode to wiki formatting, using regular expressions."
import re
import sys
def sub_b(in_str):
return re.sub(r'\[/?b\]', "'''", in_str)
def sub_i(in_str):
return re.sub(r'\[/?i\]', "''", in_str)
# numberd lists
def sub_nlist(in_str):
handle_nlist = lambda m: m.group(0)[8:-7].replace('[*]', '\n#') + '\n\n'
return re.sub(r'\[list=1\].*\[/list\]', handle_nlist, in_str)
# Converting URL's.
def sub_turl(in_str):
'[url=LINK]TITLE[/url] -> [LINK TITLE]'
def parse_url(m):
l=re.split(r'\[url=([^\]]*)]([^\[]*)\[/url\]', m.group(0))
return '['+ l[1] + ' ' + l[2] + ']'
in_str = re.sub(r'\[url=([^\]])*]([^\[])*\[/url\]', parse_url, in_str)
return in_str
def sub_url(in_str):
'[url]LINK[/url] -> [LINK]'
return re.sub(r'\[url\]([^\[])*\[/url\]', lambda m: '['+m.group(0)[5:-6]+']', in_str)
def sub_size(s):
def parse_size(m):
l = re.split(r'\[size=(\d*)\]([^\[]*)\[/size\]', m.group(0))
return '<span style="font-size:%spx">%s</span>' % (l[1], l[2])
return re.sub(r'\[size=\d*\]([^\[])*\[/size\]', parse_size, s)
def convert(in_str):
in_str = sub_b(in_str)
in_str = sub_i(in_str)
# \number doesn't work :(
in_str = re.sub(r'\[u\]', '<u>', in_str)
in_str = re.sub(r'\[/u\]', '</u>', in_str)
in_str = in_str.replace('\n', '<br>')
in_str = sub_nlist(in_str)
in_str = sub_url(in_str)
in_str = sub_turl(in_str)
in_str = in_str.replace('[center]', '<center>')
in_str = in_str.replace('[/center]', '</center>')
in_str = sub_size(in_str)
return in_str
if __name__ == "__main__":
in_page = sys.stdin.read()
print convert(in_page)