-
Notifications
You must be signed in to change notification settings - Fork 26
/
migrate.py
executable file
·1421 lines (1173 loc) · 51.2 KB
/
migrate.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#***************************************************************************
#* Copyright (c) 2021 Yorik van Havre <[email protected]> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
"""
migrate.py - MediaWiki to Markdown migration tool
This tool allows to download all pages and images from a MediaWiki instance,
and save all images and pages as markdown files. It keeps track of page versions,
so it can update an existing download by just updating what has changed.
Basic command-line usage:
-------------------------
migrate.py --init
See bottom of file for available functions
Python usage:
-------------
First time:
from migrate import MediaWiki
wiki = MediaWiki()
wiki.getPageNames() # not strictly needed, done automatically by next step
wiki.getAllPages() # this writes to disk cache, so it can be interrupted/resumed
wiki.writeAllPages() # write md files
wiki.getAllImages() # fetches and saves all images
You will get two json files: a text cache and a revision set.
Next times:
from migrate import MediaWiki
wiki = MediaWiki() # this loads filecache.json
oldrevisions = wiki.readRevisions() # this loads the latest revision set
wiki.getPageNames() # this fetches new page names, if any
newrevisions = wiki.getRevisions()
wiki.updateRevisions(oldrevisions,newrevisions) # this writes to disk cache like getAllPages
wiki.writeAllPages() # write md files - overwrites everything
wiki.getAllImages() # fetches and saves all images - existing ones are not overwritten
"""
# write threads: https://www.pythontutorial.net/advanced-python/python-threading/
import requests
import sys
import os
import re
import json
from datetime import datetime, date
import pypandoc
import threading
from PySide2 import QtCore
import FreeCAD
import git
import inspect
unhandledTemplates = [] # holder for unhandled templates
BASE_URL = "https://wiki.freecadweb.org"
THREADS = 8 # number of threads for file writing
WIKIFOLDER = "wiki"
CATEGORY_COLUMNS = 3 # the number of columns on category pages
PART_API_PAGES = ["Vertex","Edge","Wire","Face","Shell","Solid","CompSolid","Compound",
"Arc","Circle","Line","Ellipse","Cone","BSplineCurve","BSplineSurface",
"BezierCurve","Cylinder","Point","Sphere","Shape"]
# templates that are safe to remove entirely
UNUSED_TEMPLATES = ["Userdocnavi","Powerdocnavi","Arch Tools navi","\\#translation:","clear",
"Part Tools navi","Draft Tools navi","UnfinishedDocu","Tutorials navi",
"Arch_Tools_navi"]
class MediaWiki:
"""MediaWiki([url]) - A class to represent an online MediaWiki instance.
default url is wiki.freecadweb.org"""
def __init__(self,url=BASE_URL):
if not url.endswith("api.php"):
if not url.endswith("/"):
url += "/"
url += "api.php"
self.url = url
self.cachefile = os.path.join(os.path.dirname(__file__),"cache.json")
self.session = requests.Session()
self.pagenames = []
self.pagecount = 0
self.pagecontents = {}
self.readCache()
self.imagecount = 0
self.images = {}
self.wikifolder = WIKIFOLDER
self.output = os.path.join(os.path.dirname(__file__),self.wikifolder)
if not os.path.exists(self.output):
os.mkdir(self.output)
self.imagefolder = "images"
self.translationfolder = "translations"
self.rootpage = "README.md"
self.workbenches = self.getWorkbenches()
self.icon_function = os.path.join(self.imagefolder,"Arrow-right.svg")
self.icon_module = os.path.join(self.imagefolder,"Applications-python.svg")
self.icon_type = os.path.join(self.imagefolder,"A2p_PartLabel.svg")
self.icon_builtin = os.path.join(self.imagefolder,"BIM_Column.svg")
self.icon_nav = os.path.join(self.imagefolder,"Button_right.svg")
### UTILS
def printProgress(self,count=None,total=100,text=""):
"""printProgress([count,total,text]):
Prints a progress bar indicating the current progresses (count/total)
and optionally an info text. Calling printProgress() terminates with a
newline"""
sys.stdout.flush()
if count is None:
sys.stdout.write("\r"+" ".ljust(72)+"\r")
return
sys.stdout.write(('\r'+text+' '+str(int((count/total)*100))+'%').ljust(72))
def getWorkbenches(self):
"""getWorkbenches():
Returns a list of workbenches from FreeCAD"""
appm = os.path.join(FreeCAD.getHomePath(),"Mod")
userm = os.path.join(FreeCAD.getUserAppDataDir(),"Mod")
mods = []
for mod in os.listdir(appm):
if os.path.isdir(os.path.join(appm,mod)):
mods.append(mod)
for umod in os.listdir(userm):
if os.path.isdir(os.path.join(userm,mod)):
if not mod in mods:
mods.append(mod)
return mods
def getWorkbench(self,page):
"""getWorkbench(page):
Returns the workbench this page belongs to"""
if not ("Workbench" in page):
for w in self.workbenches:
if page.replace(" ","_").startswith(w+"_"):
return w
return None
### CACHE OPERATIONS
def writeCache(self):
"""writeCache():
Writes the contents of self.pagecontents to disk"""
if self.pagecontents:
with open(self.cachefile,'w',encoding='utf8') as jfile:
json.dump(self.pagecontents, jfile, ensure_ascii = False)
def readCache(self):
"""readCache():
Reads the contents of self.pagecontents from disk"""
if os.path.exists(self.cachefile):
with open(self.cachefile) as jfile:
self.pagecontents = json.load(jfile)
self.pagenames = list(self.pagecontents.keys())
self.pagecount = len(self.pagecontents)
### PAGE OPERATIONS
def getPageCount(self):
"""getPageCount():
Returns the number of content pages.
Also stores the total in self.pagecount"""
params = { "action": "query",
"meta": "siteinfo",
"formatversion": "2",
"format": "json",
"siprop": "statistics",
}
result = self.session.get(url=self.url, params=params)
data = result.json()
pagecount = data["query"]["statistics"]["articles"]
self.pagecount = pagecount
return pagecount
def getPageNames(self):
"""getPageNames():
Returns a list of all pages of the wiki.
Also stores the list in self.pagenames"""
pages = []
count = 1
apfrom = None
params = { "action": "query",
"format": "json",
"list": "allpages",
"aplimit": "500",
}
if not self.pagecount:
self.getPageCount()
while True:
self.printProgress(len(pages),self.pagecount,"Getting pages list...")
if apfrom:
params["apfrom"] = apfrom
else:
if "apfrom" in params:
break
result = self.session.get(url=self.url, params=params)
data = result.json()
pages.extend([page["title"].replace(" ","_") for page in data["query"]["allpages"]])
apfrom = None
if "continue" in data:
if "apcontinue" in data["continue"]:
if data["continue"]["apcontinue"]:
apfrom = data["continue"]["apcontinue"]
count += 1
else:
break
self.printProgress()
self.pagenames = list(pages)
self.pagecount = len(self.pagenames)
return pages
def getPage(self,name):
"""getPage(name):
Returns the wiki content of a page and a revision ID.
Also stores it in self.pagecontents[name]"""
params = { "action": "parse",
"format": "json",
"page": name,
"prop": "wikitext|revid",
"formatversion": "2",
}
result = self.session.get(url=self.url, params=params)
data = result.json()
if "parse" in data:
wikitext = data["parse"]["wikitext"]
revision = data["parse"]["revid"]
self.pagecontents[name] = wikitext
else:
#print("Page",BASE_URL+"/"+name,"not found")
if name in self.pagenames:
self.pagenames.remove(name)
if name in self.pagecontents:
del self.pagecontents[name]
return None,None
return wikitext,revision
def getAllPages(self,pageset=None):
"""getAllPages([pageset])
Gets the contents of all of the wiki pages
and stores everything in self.pagecontents. If pageset is
not given, all pages from self.pagenames are retrieved"""
revfile = os.path.join(os.path.dirname(__file__),"revisions_tmp.json")
revisions = self.readRevisions(revfile)
overwrite = True
if not pageset:
overwrite = False
if not self.pagenames:
self.getPageNames()
self.getCategories()
pageset = self.pagenames
count = 1
for page in pageset:
self.printProgress(count,len(pageset),"Getting page "+page[:64]+"...")
if overwrite or (not page in self.pagecontents):
text,revid = self.getPage(page)
if text:
revisions[page] = revid
if count % 10 == 0:
self.writeCache()
self.writeRevisions(revisions,revfile)
count += 1
self.writeCache()
self.printProgress()
if os.path.exists(revfile):
os.remove(revfile)
self.writeRevisions(revisions)
return revisions
### CATEGORIES
def getCategories(self):
"""getCategories():
Returns a list of categories of the whole wiki.
Also stores the list in self.pagenames"""
categories = []
count = 1
acfrom = None
params = { "action": "query",
"format": "json",
"list": "allcategories",
"aclimit": "500",
}
while True:
if acfrom:
params["acfrom"] = acfrom
else:
if "acfrom" in params:
break
result = self.session.get(url=self.url, params=params)
data = result.json()
categories.extend(["Category:"+c["*"] for c in data["query"]["allcategories"]])
acfrom = None
if "continue" in data:
if "accontinue" in data["continue"]:
if data["continue"]["accontinue"]:
acfrom = data["continue"]["accontinue"]
count += 1
else:
break
self.pagenames.extend(categories)
self.writeCache()
return categories
def getCategoryContents(self,name):
"""getCategoryContents():
Returns a list of pages that use the given category"""
if not name.startswith("Category:"):
name = "Category:"+name
members = []
count = 1
cmfrom = None
params = { "action": "query",
"format": "json",
"list": "categorymembers",
"cmtitle": name,
"cmlimit": "5000",
}
# perfomr only one big query (cmcontinue is bugged)
result = self.session.get(url=self.url, params=params)
data = result.json()
for m in data["query"]["categorymembers"]:
if ("title" in m) and m["title"]:
members.append(m["title"])
return members
# cmcontinue is apparently bugged in categorymembers...
while True:
if cmfrom:
params["cmfrom"] = cmfrom
else:
if "cmfrom" in params:
break
result = self.session.get(url=self.url, params=params)
data = result.json()
members.extend([m["title"] for m in data["query"]["categorymembers"]])
cmfrom = None
if "continue" in data:
#print(data["continue"])
if "cmcontinue" in data["continue"]:
if data["continue"]["cmcontinue"]:
cmfrom = data["continue"]["cmcontinue"]
count += 1
else:
break
return members
def formatCategoryContents(self,name):
cpages = self.getCategoryContents(name)
# mediawiki table - obsolete, pandoc does not support it?
#result = "{|\n"
#col = 1
#for p in cpages:
# result += "| [["+p+"]]\n"
# if col == CATEGORY_COLUMNS:
# result += "|-\n"
# col = 1
# else:
# col += 1
#result += "|}\n"
result = "|"
for i in range(CATEGORY_COLUMNS):
result += " |"
result += "\n|"
for i in range(CATEGORY_COLUMNS):
result += " --- |"
result += "\n|"
col = 1
for p in cpages:
result += " ["+p+"]("+p.replace(":","_").replace(" ","_")+".md) |"
if col == CATEGORY_COLUMNS:
result += "\n|"
col = 1
else:
col += 1
return result
def getPageCategory(self,page):
"""getPageCategory(self,page):
Returns the first category a page belongs to, or None"""
if "API" in page:
return "API"
if page in self.pagecontents:
content = self.pagecontents[page]
cats = re.findall("Category:(.*?)[{}\[]",content)
if cats:
return cats[0]
if re.findall("Arch Tools navi",content):
return "Arch"
return None
def getPageCategories(self,page):
"""getPageCategories(self,page):
Returns the categories a page belongs to"""
cats = []
if "API" in page:
cats.append("API")
if page in self.pagecontents:
content = self.pagecontents[page]
if "Tutorials navi" in content:
cats.append("Tutorials")
cs = re.findall("Category:(.*?)[{}\[]",content)
for c in cs:
if not c in cats:
cats.append(c)
tcs = re.findall("\{\{(.*?) Tools navi",content)
for tc in tcs:
if not tc in cats:
cats.append(tc)
return cats
def getLinks(self,page):
"""getLinks(page):
Returns the links found in a page as md links: [...](...)"""
links = []
pagefile = os.path.join(self.output,page.replace(" ","_").replace(":","_")+".md")
if os.path.exists(pagefile):
with open(pagefile) as f:
for line in f:
ll = re.findall("\[.*?\]\(.*?\)",line)
for l in ll:
if not ".png" in l:
if not ".jpg" in l:
if not "http" in l:
links.append(l)
return links
### IMAGE OPERATIONS
def getImageCount(self):
"""getImageCount():
Returns the number of images.
Also stores the total in self.imagecount"""
params = { "action": "query",
"meta": "siteinfo",
"formatversion": "2",
"format": "json",
"siprop": "statistics",
}
result = self.session.get(url=self.url, params=params)
data = result.json()
imagecount = data["query"]["statistics"]["images"]
self.imagecount = imagecount
return imagecount
def getImageNames(self):
"""getImageNames():
Returns a list of all images of the wiki.
Also stores the paths in self.images"""
images = {}
count = 1
aifrom = None
params = { "action": "query",
"format": "json",
"list": "allimages",
"ailimit": "100",
}
if not self.imagecount:
self.getImageCount()
while True:
self.printProgress(count,self.imagecount,"Getting images list...")
if aifrom:
params["aifrom"] = aifrom
else:
if "aifrom" in params:
break
result = self.session.get(url=self.url, params=params)
data = result.json()
for image in data["query"]["allimages"]:
images[image["name"]] = image["url"]
count += 1
aifrom = None
if "continue" in data:
if "aicontinue" in data["continue"]:
if data["continue"]["aicontinue"]:
aifrom = data["continue"]["aicontinue"]
else:
break
self.printProgress()
self.images = images
self.imagecount = len(self.images.keys())
return images
def getImage(self,name,overwrite=False,basepath=None):
"""getImage(name,[overwrite,basepath]):
Downloads and saves the given image in an images subfolder.
If no basepath is given, the current dir is used.
If overwrite is True, file is downloaded again"""
if not basepath:
basepath = self.output
basedir = os.path.join(basepath,self.imagefolder)
if not os.path.isdir(basedir):
os.mkdir(basedir)
filename = os.path.join(basedir,name)
if overwrite or (not os.path.exists(filename)):
filecontents = requests.get(self.images[name])
with open(filename,"wb") as imagefile:
imagefile.write(filecontents.content)
def getAllImages(self,overwrite=False,basepath=None):
"""getAllImages([overwrite]):
Saves all images to disk.
If no basepath is given, the current dir is used.
If overwrite is True, file is downloaded again"""
if not basepath:
basepath = self.output
if not self.images:
self.getImageNames()
count = 1
for name in self.images.keys():
self.printProgress(count,self.imagecount,"Downloading "+name+"...")
self.getImage(name,overwrite,basepath)
count += 1
### REVISION OPERATIONS
def getRevisions(self):
"""getRevisions():
Returns a list of revisions for the pages stored in self.pagenames"""
revisions = {}
count = 1
apfrom = None
params = { "action": "query",
"format": "json",
"prop": "revisions",
"rvprop": "ids",
"rvslots": "main",
"formatversion": "2",
}
if not self.pagenames:
self.getPageNames()
self.printProgress(0,100,"Getting pages revisions...")
titles = ""
for page in self.pagenames:
if titles:
titles += "|"
titles += page
count += 1
if count % 10 != 0:
params["titles"] = titles
result = self.session.get(url=self.url, params=params)
data = result.json()
for page in data["query"]["pages"]:
if "revisions" in page:
revisions[page["title"]] = page["revisions"][0]["revid"]
self.printProgress(count,self.pagecount,"Getting pages revisions...")
titles = ""
self.printProgress()
return revisions
def writeRevisions(self,revisions,filename=None):
"""writeRevisions(revisions,[filename]):
Writes the contents of revisions to disk. If no filename is
given, one is created automatically from the current timestamp"""
if not filename:
d = str(datetime.now())
fp = os.path.dirname(__file__)
filename = os.path.join(fp,"revisions_"+d+".json")
with open(filename,'w',encoding='utf8') as jfile:
json.dump(revisions, jfile, ensure_ascii = False)
def readRevisions(self,filename=None):
"""readRevisions([filename]):
Returns a revisions dictionary previously stored in a file. If
no filename is given, the latest one is taken"""
data = {}
if not filename:
files = os.listdir(os.path.dirname(__file__))
jsonfiles = [f for f in files if (f.endswith(".json") and f.startswith("revision"))]
jsonfiles.sort(key=os.path.getmtime, reverse=True)
if not jsonfiles:
return data
filename = os.path.join(os.path.dirname(__file__),jsonfiles[0])
print("Opening",os.path.basename(filename))
if os.path.exists(filename):
with open(filename) as jfile:
data = json.load(jfile)
return data
def updateRevisions(self,oldrevision,newrevision):
"""updateRevisions(oldrevision,newrevision):
Reads again the pages that need an update"""
pageset = []
for page,revid in newrevision.items():
if page in oldrevision:
if oldrevision[page] == revid:
continue
pageset.append(page)
print(len(pageset),"pages (",int((len(pageset)/len(self.pagenames))*100),"% ) to update...")
self.getAllPages(pageset)
return pageset
### MARKDOWN OPERATIONS
def getMarkdown(self,page,clean=True):
"""getMarkdown(page,wikitext,[clean]):
Returns a markdown version of a text in wiki format.
If clean is false, raw pandoc output is returned. clean
can also be a numeric debug value to pass to cleanMarkdown()"""
wikitext = self.pagecontents[page]
xargs = ['--markdown-headings=atx'] # pandoc arguments
try:
fmt = 'markdown+hard_line_breaks+pipe_tables'
result = pypandoc.convert_text(wikitext, fmt, format='mediawiki', extra_args=xargs)
except:
print("Error converting",page)
return None
if clean:
result = self.cleanMarkdown(result,debug=clean)
if page.startswith("Category:"):
# add list of pages
cpages = self.formatCategoryContents(page)
cpages = "\n\n### Contents\n\n" + cpages
result += cpages
result = self.addTitle(page,result)
result = self.addFooter(page,result)
return result
def addTitle(self,page,mdtext):
"""addTitle(self,page,result):
adds the page title to the given markdown"""
# insert page title
if mdtext.startswith("<img"):
#print("img replace")
mdtext = mdtext.split("\n")
mdtext[0] = "# "+ mdtext[0].replace("128","64") + " " + page.replace("_"," ")
mdtext = "\n".join(mdtext)
elif "\n---\n\n" in mdtext:
#print("yaml replace")
mdtext = mdtext.replace("\n---\n\n","\n---\n\n# "+page.replace("_"," ")+"\n\n")
else:
#print("std replace")
mdtext = "# "+page.replace("_"," ")+"\n"+mdtext
return mdtext
def addFooter(self,page,mdtext):
"""addFooter(self,mdtext):
adds a footer with navigation links to the bottom of the page"""
breadcrumb = " > "
footer = "\n\n\n\n---\n"
#footer += "![]("+self.icon_nav+") "
footer += "⏵ "
footer += "[documentation index](../"+self.rootpage+")"
c = self.getPageCategories(page)
w = self.getWorkbench(page)
for cat in c:
if cat != w:
footer += breadcrumb + "[" + cat + "](Category_" + cat + ".md)"
if w:
footer += breadcrumb + "[" + w + "](" + w + "_Workbench.md)"
footer += breadcrumb + page.replace("_"," ") + "\n"
mdtext += footer
return mdtext
def cleanMarkdown(self,mdtext,imagepath=None,debug=0):
"""cleanMarkdown(mdtext,[imagepath,debug]):
Returns a cleaned version of the given markdown text.
Imagepath indicates the location of images relative to this page
(default = "images"). debug (default = 0) is used to control the
level of cleaning done. 0 means disabled (all cleaning is perfomed),
1 = first cleaning step, 2 = 2 first cleaning steps, etc. Check the code
to see what each step does."""
global unhandledTemplates
result = mdtext
flags = re.DOTALL|re.MULTILINE
if not imagepath:
imagepath = self.imagefolder
if (not debug) or (debug is True):
debug = 100000
if debug >= 1:
# path replacements
result = re.sub("\!\[(.*?)\]\(",r"![\1]("+imagepath+"/",result) # add /image to image paths
if debug >= 1.5:
result = re.sub(" \"wikilink\"",".md",result) # add .md to wiki page links
if debug >= 2:
# template that are simply removed
result = re.sub("\`.*?\`\{\=html\}","",result)
result = re.sub("<!--.*?-->","",result,flags=flags)
result = re.sub("{{Docnav.*?}}","",result,flags=flags)
result = re.sub("{{Page in progress}}","",result,flags=flags)
result = re.sub("{{\#translation\:}}","",result,flags=flags)
result = re.sub("{{\\\#translation\:}}","",result,flags=flags)
result = re.sub("{{UnfinishedDocu.*?}}","",result,flags=flags)
result = re.sub("{{TOCright}}","",result,flags=flags)
if debug >= 3:
# templates that get turned into italic text
result = re.sub("{{Caption\|(.*?)}}",r"\n*\1*",result,flags=flags)
if debug >= 4:
# templates that get turned into bold text
result = re.sub("{{KEY\|(.*?)}}",r"**\1**",result,flags=flags)
result = re.sub("{{Button\|(.*?)}}",r"**\1**",result,flags=flags)
result = re.sub("{{MenuCommand\|(.*?)}}",r"**\1**",result,flags=flags)
result = re.sub("{{PropertyData\|(.*?)}}",r"**\1**",result,flags=flags)
result = re.sub("{{PropertyView\|(.*?)}}",r"**\1**",result,flags=flags)
result = re.sub("{{Emphasis\|(.*?)}}",r"**\1**",result,flags=flags)
result = re.sub("{{VeryImportantMessage\|(.*?)}}",r"**\1**",result,flags=flags)
result = re.sub("{{FileName\|(.*?)}}",r"**\1**",result,flags=flags)
if debug >= 5:
# templates that get turned into <small> text
result = re.sub("{{Version\|(.*?)}}",r"<small>(v\1)</small> ",result,flags=flags)
result = re.sub("{{version\|(.*?)}}",r"<small>(v\1)</small> ",result,flags=flags)
result = re.sub("{{VersionPlus\|(.*?)}}",r"<small>(v\1)</small> ",result,flags=flags)
if debug >= 6:
# templates that get turned into a newline char
result = re.sub("{{Clear}}",r"\n",result,flags=flags)
# all other templates are simply converted to normal text (see below)
if debug >= 7:
# turning GuiCommand block into YAML
if "{{GuiCommand" in result:
guicommandblk = re.findall("```{\=mediawiki}.*?{{GuiCommand(.*?)}}\n```",result,flags=flags)
if guicommandblk:
guicommandblk = guicommandblk[0]
guicommandblk = re.sub("\|(.*?)\=(.*?)",r' \1: \2',guicommandblk) # fixing GuiCommand contents
guicommandblk = self.fix_yaml(guicommandblk)
result = re.sub("```{\=mediawiki}.*?{{GuiCommand(.*?)}}\n```",r"---\n GuiCommand:"+guicommandblk+"---\n",result,flags=flags)
result = "---"+"---".join(result.split("---")[1:]) # removing empty line before yaml block
if debug >= 7.5:
# turning TutorialInfo block into YAML
if "{{TutorialInfo" in result:
tutblk = re.findall("{{TutorialInfo(.*?)}}\n",result,flags=flags)
if tutblk:
tutblk = tutblk[0]
tutblk = tutblk.strip()
tutblk = re.sub("\|(.*?)\=(.*?)",r" \1: \2",tutblk) # fixing GuiCommand contents
if not tutblk.startswith("\n"):
tutblk = "\n" + tutblk
tutblk = self.fix_yaml(tutblk)
tutblk = "---\n TutorialInfo:"+tutblk+"\n---\n\n"
result = re.sub("{{TutorialInfo.*?}}\n","",result,flags=flags)
result = tutblk + result
if debug >= 8:
# remove code fences
result = re.sub("\`","",result)
result = re.sub("\{\=mediawiki\}","",result)
result = re.sub("\{\:mediawiki\}","",result)
if debug >= 9:
# creating new code fences
result = re.sub("{{Code\|code\=(.*?)}}",r"```python\1```",result,flags=flags) # replace {{Code}} templates
result = re.sub("{{Code\|lang\=json\|code\=(.*?)}}",r"```json\1```",result,flags=flags) # replace {{Code}} templates
result = re.sub("{{incode\|(.*?)}}",r"`\1`",result,flags=flags) # replace {{incode}} templates
result = re.sub(" \`\`\`",r" \n```",result,flags=flags) # make sure all ``` are on a new line
result = re.sub("{{TRUE}}",r"`True`",result,flags=flags) # replace {{TRUE}} templates
result = re.sub("{{FALSE}}",r"`False`",result,flags=flags) # replace {{TRUE}} templates
if debug >= 10:
# fixing links
result = re.sub("(!\[.*?\]\(.*?)\".*?\"(\))",r"\1\2",result) # remove image path descriptions
for l1 in re.findall("\[\[Image\:.*?\|.*?px\]\]",result):
iml1 = re.findall("Image\:(.*?)\|",l1)[0]
iml2 = re.findall("\|(.*?)px\]\]",l1)[0]
l2 = "<img src=\""+imagepath+"/"+iml1.replace(" ","_")+"\" width="+iml2+"px>"
result = result.replace(l1,l2)
result = re.sub("\[\[Image\:(.*?)\|(.*?)\]\]",r"![]("+imagepath+"/\1)",result)
result = re.sub("\[\[(.*?)\|(.*?)\]\]",r"[\2](\1.md)",result)
result = re.sub(r"\]\(.*?\.",lambda x:x.group().replace(" ","_"),result) # replace spaces by underscores in all remaining links
result = re.sub("!\[(.*?)\]\((.*?)\){width=\"(.*?)\"}",r'<img alt="\1" src=\2 style="width:\3px;">',result) # fix img sizes
#for l in re.findall("\[.*?\]\(.*?\)",result):
# print(" ",l)
if debug >= 11:
# fixing misc formatting glitches
result = re.sub("\n- \n","\n-",result,flags=flags) # condensing newlines in bullet point lists
result = re.sub("\n \n ","",result,flags=flags) # condensate empty lines
result = re.sub("\n\n\n\(v",r" (v",result,flags=flags) # condensate badly formatted version templates
result = re.sub("\n:\n","",result,flags=flags) # condensate remaining : lines
result = re.sub("\n\n\n\n-","\n-",result,flags=flags) # condensate bad - lists
if debug >= 11.5:
result = re.sub("(<img.*?>.*?)(\*.*?\*\n)",r"\1\n\2",result) # put captions on a newline
result = re.sub("\[(.*?)\]\(image:(.*?)\.md\)",r"![\1]("+imagepath+r"/\2)",result) # fix image: links
if debug >= 11.7:
result = re.sub("([0-9]+)px\]\(File\:(.*?)\.md\)",r'<img src='+imagepath+r'/\2 style="width:\1px">',result) # fix File: links
result = re.sub("\:(\w+\.md\))",r"_\1",result) # rename Namespace:Page to Namespace_Page
if debug >= 12:
# removing other leftovers
result = re.sub("\\\_\\\_NOTOC\\\_\\\_","",result,flags=flags) # removing __NOTOC__ entries
result = re.sub("\{\#.*?\}","",result,flags=flags) # removing {#...} tags
result = re.sub(r"\:\\\*"," -",result) # fix second-level bullets
if debug >= 13:
# removing all remaining templates
for template in re.findall("{{.*?}}",result,flags=flags):
if template.strip("{").strip("}").strip() in UNUSED_TEMPLATES:
result = re.sub(template,"",result,flags=flags) # remove all remaining templates
else:
#print("WARNING: Unhandled template:",template)
try:
template = re.findall("{{(.*?)[|}]",template,flags=flags)[0].strip()
except:
print("error in template:",template)
sys.exit(1)
if not template in unhandledTemplates:
unhandledTemplates.append(template)
if debug >= 14:
# remove horizontal lines
result = re.sub(r"(-)\1{3,}","",result)
if debug >= 15:
# remove category links (included in footer)
# result = re.sub("Category:","<img src=\""+imagepath+"/Property.png\" style=\"width:16px\"> ",result) # Replace Category: with icon
result = re.sub("\[Category\:.*?md\)","",result)
if debug >= 16:
# handle MacroLink templates
result = re.sub("{{MacroLink\|Icon\=(.*?)\|(.*?)\|(.*?)}}","<img style=\"width:16px;\" src=\""+imagepath+r"/\1"+"\"> "+r"[\3](\2.md)",result)
return result.strip()
def fix_yaml(self, blk):
'''stricter formatting of yaml stuff for github pages'''
blk = blk.replace("→",",") # github does not like unicode in here...
if blk.strip().startswith("/"): # fix malformation of translated GuiCommands
blk = blk[3:]
blk = re.sub("/:\s*/", ": ", blk) # add spaces after colon
blk = re.sub("\(.*?\)", "", blk) # removes markdown links
blk = re.sub("\|.*?\]", "", blk)
blk = blk.replace("[","")
blk = blk.replace("]","")
return blk
def writeMarkdown(self,page,overwrite=True,basepath=None,clean=True):
"""writeMarkdown(page,[overwrite,basepath,clean]):
Writes the given page as a .md file. If basepath is
not given, file is written in the current dir.
If overwrite is False, existing files are skipped.
If clean is false, raw pandoc output is returned"""
if not basepath:
basepath = self.output
if not os.path.exists(basepath):
print("base path",basepath,"does not exist")
return page
result = self.getMarkdown(page,clean)
if not result:
print("Error writing page:",page)
return page
if "REDIRECT" in result.split("\n")[0]:
truepage = re.findall("\((.*?)\.md\)",result)
if not truepage:
truepage = re.findall("\((.*?)[\"\)]",result)
if not truepage[0].strip() in self.pagecontents:
print("Error:redirecting to",truepage[0].strip())
return page
result = self.getMarkdown(truepage[0].strip(),clean)
if not result:
print("Error writing page:",page)
return page
else:
filename = page
if "/" in filename:
filename = os.path.join(self.output,self.translationfolder,page.split("/")[-1],page.split("/")[0])
transpath = os.path.dirname(filename)
if not os.path.exists(transpath):
if not os.path.exists(os.path.join(self.output,self.translationfolder)):
os.mkdir(os.path.join(self.output,self.translationfolder))
os.mkdir(transpath)
filename += ".md"
filename = os.path.join(basepath,filename)
filename = filename.replace(" ","_")
filename = filename.replace(":","_")
if overwrite or (not os.file.exists(filename)):
with open(filename,"w") as mdfile:
mdfile.write(result)
return None
def writeAllPages(self,overwrite=True,basepath=None,threads=THREADS):