-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtractAndConvert.py
477 lines (405 loc) · 18.9 KB
/
ExtractAndConvert.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/env python
# -*- coding: utf-8 -*
#
# Program for converting LibrePlan Twiki pages to GitHub Wiki+markdown
# Because some content comes from separate MySQL table we use the webpage as root
# and not the Twiki/data files
# Call url https://wiki.libreplan.org/bin/view/LibrePlan/WebTopicList
import os,sys, re
import requests
from bs4 import BeautifulSoup
from pprint import pprint,pformat
import pypandoc
import codecs
import copy
# URL that lists all pages
#url = "http://wiki.libreplan.org/bin/view/LibrePlan/WebTopicList"
url = "http://wiki.libreplan-enterprise.com/bin/view/LibrePlan/WebTopicList"
# Root directory of old Twiki page
root_url="http://wiki.libreplan-enterprise.com/"
# Dir to start exporting to
export_root_dir="./libreplan.wiki/twiki"
# All urls we are interested in start with:
starts_with="/twiki/bin/view"
# new url location
new_url_root="https://github.com/LibrePlan/libreplan/wiki/twiki"
bugcounter=0
copyright_notice="<p>\n\nCopyright (c) by the contributing authors. All material on this collaboration platform is the property of the contributing authors.</body></html>"
print("Pandoc version: "+pypandoc.get_pandoc_version())
#print(pypandoc.get_pandoc_path())
#print(pypandoc.get_pandoc_formats())
#
r = requests.get(url)
data = r.text
data2=copy.copy(data)
# Copyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
mysearch = "<!-- /patternContent-->"
if mysearch in data2:
data2 = data2[0:data2.index(mysearch)]
# </body> </html>
data2 = data2 + copyright_notice
# let's try to shorten urls:
# data2 = re.sub(r'href="(.*?)/(.*?)/(.*?)(\?(.*) )"',
# r'href=\"\2/\3\"',
# data2)
# removing all http://wiki.libreplan-enterprise.com and relocate to the testrepo
# This url works on github: [ItEr61S03RFPerformanceCompanyView](ItEr61S03RFPerformanceCompanyView)
data2 = data2.replace("http://wiki.libreplan-enterprise.com","")
if "/twiki/kwoot/" in data2:
print 1
sys.exit(2)
# intermediarie change
data2 = data2.replace("/twiki/bin/edit", "/twiki/bin/view")
# /twiki/bin/view/
data2 = data2.replace(r"/twiki/bin/view/LibrePlan/", r"")
if "/twiki/kwoot/" in data2:
print 2
sys.exit(2)
soup = BeautifulSoup(data, "html.parser")
print type(soup)
#soup2=copy.copy(soup)
soup2=BeautifulSoup(data2, "html.parser")
# remove tages and do not keep children
invalid_tags = ["style","script","head"]
for tag in invalid_tags:
for match in soup.findAll(tag):
match.clear()
# print "intermediate"+str(len(pformat(soup2))) + ":" + pformat(soup2)
print str(len(pformat(soup2))) + ":" + pformat(soup2)
# remove tages but keep children
invalid_tags = ['div','span',"base"]
for tag in invalid_tags:
for match in soup2.findAll(tag):
match.replaceWithChildren()
# print "intermediate"+str(len(pformat(soup2))) + ":" + pformat(soup2)
print str(len(pformat(soup2))) + ":" + pformat(soup2)
basefilename="libreplan.wiki/twiki/LibrePlan/All_Twiki_Pages"
filename=basefilename+".pre-pandoc-concersion"
print "Filename: " + filename
# Save in libreplan.wiki/twiki
text_file = codecs.open(filename, "w", "utf-8")
text_file.write(soup2.prettify())
text_file.close()
# Convert page to markdown_github syntax
#filters = ['pandoc-citeproc']
#pdoc_args = ['--mathjax',
# '--smart']
filters=[]
pdoc_args=["--wrap=none"]
# extensions: simple_tables, multiline_tables, grid_tables, pipe_tables
# disable extension: raw_html, native_spans
#extensions = "-raw_html-native_spans+pipe_tables"
extensions = "-native_spans"
# pipe_tables geeft hele brede kolommen. Next!
# simple_tables idem
print "Start pandoc conversion..."
print str(len(pformat(soup2))) + ":" + pformat(soup2)
my_output = pypandoc.convert_text(soup2,
'markdown_github'+extensions,
format='html',
extra_args=pdoc_args,
filters=filters)
print "*" * 80
print str(len(pformat(my_output))) + ":" + pformat(my_output)
pprint(my_output)
print "*" * 80
# After pandoc conversion save intermediate file
filename=basefilename + ".raw-md"
print "Filename: " + filename
# Save in libreplan.wiki/twiki
text_file = codecs.open(filename, "w", "utf-8")
text_file.write(my_output)
text_file.close()
# File handling part
filename=basefilename + ".md"
print "Filename: " + filename
# Save in libreplan.wiki/twiki
text_file = codecs.open(filename, "w", "utf-8")
text_file.write(my_output)
text_file.close()
#output = pypandoc.convert_text(soup2, "markdown_github", format='html', outputfile=filename)
# assert output == ""
#sys.exit(1)
counter=0
for link in soup.find_all('a'):
counter+=1
print "#" * 120
link=link.get('href')
#pprint(link)
#print(link.get('href'))
# trial: only convert pages from "LibrePlan" space
#if link is not None and link.startswith(starts_with) and "/LibrePlan/" in link:
if link is not None and "/LibrePlan/" in link:
print link,
link_parts=link.split("/")
print ", in parts: ",pformat(link_parts),
page_to_convert_url= root_url + link # _parts[-1] # get last part after splitting to get page name
print "page to request: ",page_to_convert_url
# Directory handling part
# Check for directory
twiki_space=link_parts[-2]
if twiki_space=="LibrePlan":
destination_directory = export_root_dir + "/" + twiki_space
print "Directory: " + destination_directory,
if os.path.isdir(destination_directory):
print "Found..."
else:
print "Not found! Creating!"
os.makedirs(destination_directory)
# todo get history of page and build a loop around it
filename = destination_directory + "/" + link_parts[-1] + ".original-html"
print "Filename: " + filename
if os.path.exists(filename):
# Load file
text_file = codecs.open(filename, "r", "utf-8")
data2=text_file.read()
text_file.close()
#file = open(“testfile.text”, “r”)
#print file.read()
else:
# Actually retrieve wiki page
try:
r2 = requests.get(page_to_convert_url, allow_redirects=False) # to prevent redirection to https
except:
print "Unexpected error:", sys.exc_info()[0]
data2=r2.text
#print data2
#sys.exit(1)
# save original html for checking purposes
# File handling part
# Save in libreplan.wiki/twiki
text_file = codecs.open(filename, "w", "utf-8")
text_file.write(data2)
text_file.close()
# Try to remove everything after <!-- /patternContent--> being the footer of the page.
# Remove everything after start of footer
# if 'span id=\"topic-actions\"' in data2:
# data2=data2[0:data2.index('span id=\"topic-actions\"')]
# if 'http://twiki.org/?ref=twiki.org/topmenuskin' in data2:
# data2=data2[0:data2.index('http://twiki.org/?ref=twiki.org/topmenuskin')]
# Copyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
mysearch = "<!-- /patternContent-->"
if mysearch in data2:
data2 = data2[0:data2.index(mysearch)]
# </body> </html>
data2 = data2 + copyright_notice
# let's try to shorten urls:
# data2 = re.sub(r'href="(.*?)/(.*?)/(.*?)(\?(.*) )"',
# r'href=\"\2/\3\"',
# data2)
# removing all http://wiki.libreplan-enterprise.com and relocate to the testrepo
# This url works on github: [ItEr61S03RFPerformanceCompanyView](ItEr61S03RFPerformanceCompanyView)
data2 = data2.replace("http://wiki.libreplan-enterprise.com","")
if "/twiki/kwoot/" in data2:
print 1
sys.exit(2)
# intermediarie change
data2 = data2.replace("/twiki/bin/edit", "/twiki/bin/view")
# /twiki/bin/view/
data2 = data2.replace(r"/twiki/bin/view/LibrePlan/", r"")
if "/twiki/kwoot/" in data2:
print 2
sys.exit(2)
# Fix strange Twiki bug: <th> </td>
if "<th> </td>" in data2:
data2 = data2.replace("<th> </td>", "<th> </th>")
bugcounter+=1
#
# data2 = data2.replace("/bin/edit", "/kwoot/testwiki/wiki/twiki")
# data2 = data2.replace(r"/twiki/bin/view/Main/", r"")
# if "/twiki/kwoot/" in data2:
# print 3
# sys.exit(2)
# adding images: [[https://github.com/username/repository/blob/master/img/octocat.png|alt=octocat]]
# Change page using Beautifulsoup
soup2=BeautifulSoup(data2, "html.parser")
print "*" * 80
print str(len(pformat(soup2)))+":"+pformat(soup2)
# save original html for checking purposes
# File handling part
# filename = destination_directory + "/" + link_parts[-1] + ".pre-html-processing"
# print "Filename: " + filename
# # Save in libreplan.wiki/twiki
# text_file = codecs.open(filename, "w", "utf-8")
# text_file.write(soup2.prettify())
# text_file.close()
# Remove some attributes
REMOVE_ATTRIBUTES = [
'lang', 'language', 'onmouseover', 'onmouseout', 'script', 'style', 'font',
'dir', 'face', 'size', 'color', 'style', 'class', 'width', 'height', 'hspace',
'border', 'valign', 'align', 'background', 'bgcolor', 'text', 'link', 'vlink',
'alink', 'cellpadding', 'cellspacing']
for tag in soup2.recursiveChildGenerator():
if hasattr(tag, 'attrs'):
tag.attrs = {key:value for key,value in tag.attrs.iteritems()
if key not in REMOVE_ATTRIBUTES}
print str(len(pformat(soup2))) + ":" + pformat(soup2)
# for s in soup2.find_all("span"):
# print "==>"
# pprint(s)
# #s.decompose()
# s.replace_with('')
# remove tages and do not keep children
invalid_tags = ["style","script","head"]
for tag in invalid_tags:
for match in soup2.findAll(tag):
match.clear()
# print "intermediate"+str(len(pformat(soup2))) + ":" + pformat(soup2)
print str(len(pformat(soup2))) + ":" + pformat(soup2)
# remove tages but keep children
invalid_tags = ['div','span',"base"]
for tag in invalid_tags:
for match in soup2.findAll(tag):
match.replaceWithChildren()
# print "intermediate"+str(len(pformat(soup2))) + ":" + pformat(soup2)
print str(len(pformat(soup2))) + ":" + pformat(soup2)
# Remove stuff on top of page: <a name="PageTop"> </a>
# for match in soup2.find_all("a",{"name":"PageTop"}):
# match.decompose()
#
# for match in soup2.find_all("form",{"name":"tagmeshow"}):
# match.decompose()
#
# # <a href="/bin/view/TWiki/TagMeViewAllTags">
# for match in soup2.find_all("a",{"href":"/bin/view/TWiki/TagMeViewAllTags"}):
# match.decompose()
#
# for match in soup2.find_all("span"):
# pprint(match)
# match.decompose()
# Change urls
# tmp disable
# from_root="http://wiki.libreplan-enterprise.com/bin/view/"
# for a in soup2.find_all("a","href"):
# #print a
# # to point to md files
# #if from_root in a:
# #if a.get("href") is not None:
# a["href"] = a["href"]+".md"
# # remove create new tag url
# if "create new tag" in a:
# a.decompose()
# # remove view all tags url
# if "view all tags" in a:
# a.decompose()
# Change img src="http://wiki.libreplan-enterprise.com/twiki/pub/TWiki/SmiliesPlugin/smile.gif
# http://wiki.libreplan-enterprise.com/pub/
# for d in soup2.find_all("div","patternBottomBar"):
# pprint(d)
# if d["id"]=="patternBottomBar":
# d.decompose()
# no longer needed
# span id="topic-actions
# for s in soup2.find_all("span","topic-actions"):
# pprint(s)
# s.decompose()
# Also replace urls to relocate pages:
# From: http://wiki.libreplan.org/bin/view/LibrePlan/MinuteS20121009
# To: https://github.com/LibrePlan/libreplan/wiki/twiki
# from_root="http://wiki.libreplan.org/bin/view/"
# from_root = "/bin/view/"
# to_root="https://github.com/LibrePlan/libreplan/wiki/twiki/"
# for a in soup2.findAll("a"):
# #print a
# if from_root in a:
# a["href"] = a["href"].replace(from_root, to_root)
# print str(len(pformat(soup2))) + ":" + pformat(soup2)
# save intermediate html for checking purposes
# File handling part
filename=destination_directory+"/"+link_parts[-1] + ".pre-pandoc-concersion"
print "Filename: " + filename
# Save in libreplan.wiki/twiki
text_file = codecs.open(filename, "w", "utf-8")
text_file.write(soup2.prettify())
text_file.close()
# Convert page to markdown_github syntax
#filters = ['pandoc-citeproc']
#pdoc_args = ['--mathjax',
# '--smart']
filters=[]
pdoc_args=["--wrap=none"]
# extensions: simple_tables, multiline_tables, grid_tables, pipe_tables
# disable extension: raw_html, native_spans
#extensions = "-raw_html-native_spans+pipe_tables"
extensions = "-native_spans"
# pipe_tables geeft hele brede kolommen. Next!
# simple_tables idem
print "Start pandoc conversion..."
print str(len(pformat(soup2))) + ":" + pformat(soup2)
my_output = pypandoc.convert_text(soup2,
'markdown_github'+extensions,
format='html',
extra_args=pdoc_args,
filters=filters)
print "*" * 80
print str(len(pformat(my_output))) + ":" + pformat(my_output)
pprint(my_output)
print "*" * 80
# After pandoc conversion save intermediate file
filename=destination_directory+"/"+link_parts[-1] + ".raw-md"
print "Filename: " + filename
# Save in libreplan.wiki/twiki
text_file = codecs.open(filename, "w", "utf-8")
text_file.write(my_output)
text_file.close()
#output = pypandoc.convert_text(soup2, "markdown_github", format='html', outputfile=filename)
# assert output == ""
# replace strings in md output
# (/bin/view/ => (twiki/
#my_output = my_output.replace("(/bin/view/", "(twiki/")
# Next is not possible because also gifs in original
#output = output.replace(")", ".md)")
# change img urls to local
#my_output = my_output.replace("http://wiki.libreplan-enterprise.com/twiki/pub/TWiki/TWikiDocGraphics/", "twiki/TWiki/TWikiDocGraphics/")
#my_output = my_output.replace("http://wiki.libreplan-enterprise.com/twiki/pub/", "/twiki/")
# Also replace all urls with "pub" in them
#my_output = my_output.replace("/twiki/pub/TWiki/", "/twiki/TWiki/")
# /twiki/bin/view/
#my_output = my_output.replace("/twiki/bin/view/", "/twiki/")
# remove persistent span in top of page
#my_output = re.sub(r"<span id=\".*\">.*<\/span>", "", my_output)
# Remove complete footer part from edit button to end of file!
# Now doing this earlier in original html
# Change url's in md phase
# To create a reference link, use two sets of square brackets.
# [[my internal link|internal-ref]] will link to the internal reference internal-ref.
# examples
# from: [url](/twiki/bin/view/Main/WebHome)
# to: [[url|/twiki/Main/WebHome]]
#my_output = my_output.replace("/twiki/bin/view/", "/twiki/")
#my_output="[url](/twiki/bin/view/Main/WebHome) and image [](/twiki/pub/TWiki/TWikiDocGraphics/web-bg-small.gif)"
#
# First succesfull re
# my_output = re.sub(r"\[(.*?)\]\(/twiki/bin/view/(.*?)\)",
# r"\n[[\1|/twiki/\2]]\n",
# my_output)
# Expanding with optional http://wiki.libreplan-enterprise.com/twiki/bin/view/
#my_output = re.sub(r"\[(.*?)\]\((http://wiki.libreplan-enterprise.com)?/twiki/bin/(view|edit)/(.*?)\)",
# r"[[\1|/twiki/\4]]",
# my_output)
# Extra info for me: [[url|pagename]] refers to pagename without directory!
# my_output = re.sub(r"\[(.*?)\]\((.*)/twiki/bin/.*/(.*?)\)",
# r"[[\1|\3]]",
# my_output)
#
# # and image [](/twiki/pub/TWiki/TWikiDocGraphics/web-bg-small.gif)
# my_output = re.sub(r"\[\]\(/twiki/pub/TWiki/TWikiDocGraphics/(.*?)\)",
# r"[[|\1]]",
# my_output)
# File handling part
filename=destination_directory+"/"+link_parts[-1] + ".md"
print "Filename: " + filename
# Save in libreplan.wiki/twiki
text_file = codecs.open(filename, "w", "utf-8")
text_file.write(my_output)
text_file.close()
#output = pypandoc.convert_text(soup2, "markdown_github", format='html', outputfile=filename)
# assert output == ""
# todo: add to git
# Let's halt execution
if counter>=5:
#sys.exit(1)
pass
print "Bugcounter final value: " + str(bugcounter)
# Play sound to indicate end of run
os.system("aplay /usr/share/games/simutrans/pak/sound/boing.wav")