forked from stlemme/python-dokuwiki-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikiutils.py
48 lines (36 loc) · 977 Bytes
/
wikiutils.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
import re
rx_code = re.compile(r"^((.*)(<code>)(.*)|(.*)(</code>).*)+")
def strip_code_sections(doc):
active = False
output = []
for line in doc:
result = rx_code.findall(line)
if not len(result):
# remove comments
if line.lstrip().startswith('#'):
continue
parts = re.split('[ \t]+#', line, 1)
line = parts[0]
if active:
output.append(line.strip())
continue
# print result
for g in result:
# active |= len(g[2])
if len(g[2]):
if active:
print('Warning! <code> appears twice - ignored.')
# output.append('# invalid line:')
# output.append('# ' + line)
output.append(g[1])
active = True
if active:
output.append(g[3] + g[4])
# active &= not len(g[5])
if len(g[5]):
if not active:
print('Warning! </code> appears at an invalid place - ignored.')
# output.append('# invalid line:')
# output.append('# ' + line)
active = False
return output