-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload.py
executable file
·131 lines (102 loc) · 2.99 KB
/
upload.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
# coding=utf-8
#
# Author: ChangZhuo Chen (czchen) <czchen@gmail.com>
#
import sys
import tempfile
import xmlrpclib
from config import *
def remove_zim_header(zim):
l = zim.readline()
if not l.startswith('Content-Type:'):
return False
l = zim.readline()
if not l.startswith('Wiki-Format:'):
return False
l = zim.readline()
if not l.startswith('Creation-Date:'):
return False
return True
def convert_zim_to_wikidot(zim):
if not remove_zim_header(zim):
return
i = 0
content = zim.read()
# FIXME: is there any string buffer in python?
wikidot = ''
headline = False
link = False
while i < len(content):
if content[i] == '=':
count = 1
for x in range(i + 1, min(i + 6, len(content))):
if content[x] != '=':
break
count += 1
i += count
if count == 1:
wikidot += '='
continue
if headline:
if headline == 7 - count:
headline = False
else:
# FIXME: How to handle unbalance headline?
assert False, 'unbalance headline tag'
else:
wikidot += '+' * headline
headline = 7 - count
elif content[i] == '~':
if i + 1 < len(content) and content[i + 1] == '~':
wikidot += '--'
i += 2
else:
wikidot += content[i]
i += 1
elif content[i] == '[':
# FIXME: implement link
if i < len(content) and content[i + 1] == '[':
wikidot += '['
i += 2
link = True
else:
wikidot += content[i]
i += 1
elif content[i] == '|' and link:
wikidot += ' '
i += 1
elif content[i] == ']' and link:
if i + 1 < len(content) and content[i + 1] == ']':
link = False
wikidot += ']'
i += 2
else:
wikidot += content[i]
i += 1
elif content[i] == "'":
# FIXME: implement quoted
wikidot += content[i]
i += 1
elif content[i] == '^':
# FIXME: implement subscript
wikidot += content[i]
i += 1
elif content[i] == '}':
# FIXME: implement superscript
wikidot += content[i]
i += 1
else:
wikidot += content[i]
i += 1
return wikidot
def main():
doc_root = sys.argv[1]
filename = sys.argv[2]
all_config = get_all_config(doc_root)
# FIXME: Let user select one of all_config. Now assume 0 is selected
config = all_config[0]
with open(filename, mode='rt') as zim:
wikidot = convert_zim_to_wikidot(zim)
if __name__ == '__main__':
main()