forked from hkarl/mw2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection.py
136 lines (101 loc) · 3.29 KB
/
section.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
132
133
134
135
136
"""A small helper module to deal with sections in the control page.
"""
import os
import re
import wikiconnector
def linesFromBulletlist(t):
"""Assume t is a mediawiki bullet list produced by readlines,
one item per line.
Return a list of the items, without the bullet syntax.
"""
regexp = " *(\*|#)+ *"
r = [re.sub(regexp, '', x, count=1)
for x in t
if re.match(regexp, x)]
print 'lFB 1: ', t, '\n', r
# get the content of the link:
match = [re.search('\[\[ *(.*?) *\]\]', x) for x in r]
r = [(m.group(1) if m else x.strip())
for (x, m)
in zip(r, match)]
# remove any possible readable name suffices
match = [re.search('(.*?)\|(.*)', x) for x in r]
r = [(m.group(1) if m else x.strip())
for (x, m)
in zip(r, match)]
print 'lFB final: ', r
return r
#------------------------------------------
# helper functions to deal with sections
def getSection(text, section):
"""Assume text is a mediawiki formatted text.
Assume it has L1 headings.
Obtain the L1 heading with section as title
"""
# print "getSection: ", text, section
m = re.search('= *' + section + ' *=([^=]*)',
text, re.S)
if m:
blocktext = m.group(1).strip()
return blocktext
else:
return ""
def getSectionLines(text, section):
blocktext = getSection(text, section)
if blocktext:
return blocktext.split('\n')
return []
def writeSectionContent(text, section,
filename):
"""grab content of a desired section
and write it into a file.
"""
content = getSection(text, section)
with open(filename, 'w') as fp:
fp.write(content)
return content
def downloadSectionFiles(text, section, dirname, downloadFlag, embeddedElemetsFlag):
"""download all files mentioned
in the given section as a bullet list.
download them to dirname.
return list of successfull file names
"""
filenames = linesFromBulletlist(
getSectionLines(text, section))
print "filenames: ", filenames
r = []
for f in filenames:
tmp = f.strip()
print "trying to download: ", tmp, '\n'
if tmp:
if downloadFlag:
try:
wikiconnector.download(
target=tmp,
output=dirname,
embedded_elements=embeddedElemetsFlag)
r.append(f)
except:
pass
else:
# no download, but does the file already exist locally?
fname = os.path.join(dirname, tmp + '.md')
print "tmp: >>>", tmp, "<<<", dirname, fname
try:
fp = open(fname, 'r')
r.append(tmp)
fp.close()
except:
pass
print "reutrning: ", r
return r
def getBullets(text, section):
return linesFromBulletlist(
getSectionLines(text, section))
def getProperties(text, section):
tmp = getBullets(text, section)
tmp = [x.split(':') for x in tmp]
tmp = filter(lambda x: len(x) == 2, tmp)
properties = [(k.strip(), v.strip())
for (k, v) in tmp]
return properties