forked from KermMartian/wikidot-to-mediawiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
executable file
·236 lines (198 loc) · 9.7 KB
/
convert.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
#!/usr/bin/env python3
# Copyright 2012 Philipp Klaus
# Part of https://github.com/vLj2/wikidot-to-markdown
# Improved 2016 by Christopher Mitchell
# https://github.com/KermMartian/wikidot-to-markdown
# Improved 2022 by Matthew Walker
# https://github.com/bodekerscientific/wikidot-to-mediawiki
import codecs ## for codecs.open()
import argparse
from pathlib import Path
import shutil
from datetime import datetime
import regex
from wikidot import WikidotToMediaWiki
from page_xml import PageXMLParser
class ConversionController():
def __init__(self, arguments):
# self.__input_wiki_file = options.filename
# self.__output_directory = options.output_dir
# self.__fill_blog = options.blog
# self.__create_individual_files = options.individual
self.__converter = WikidotToMediaWiki()
self.__args = arguments
def __get_xml_files(self, source):
source = Path(source)
if source.is_dir():
xml_files = sorted(source.glob("*.xml"))
elif source.is_file():
xml_files = [source.parent / (source.stem+".xml")]
else:
raise Exception(f"Source ({source}) should be either a directory or file")
return xml_files
def __get_text_files(self, source):
source = Path(source)
if source.is_dir():
input_files = sorted(source.glob("*.txt"))
elif source.is_file():
input_files = [source]
else:
raise Exception(f"Source ({source}) should be either a directory or file")
print("Input files for pages:")
for input_file in input_files:
print(f" {input_file}")
return input_files
def __process_dest(self, dest):
dest_dir = Path(dest)
if dest_dir.is_file():
raise Exception("Destination is not a directory")
dest_dir.mkdir(parents=True, exist_ok=True)
print("Output directory:")
print(f" {dest_dir}")
return dest_dir
def convert(self, source, dest):
input_files = self.__get_text_files(source)
xml_files = self.__get_xml_files(source)
dest_dir = self.__process_dest(dest)
internal_links_map = {}
if self.__args.include_associated_xml_files:
print("Including XML files in directories of associated files.")
else:
print("Ignoring XML files in directories of associated files.")
# Go through the xml files. Obtain a map from filename to title
print(f"Processing metadata from XML files:")
fullname_to_title = {}
for xml_file in xml_files:
print(f" Processing {xml_file}")
page_xml_parser = PageXMLParser(xml_file.read_text())
title = page_xml_parser.title
#title = regex.sub(" ", "_", title)
fullname = page_xml_parser.fullname
print(f" {fullname}: '{title}'")
fullname_to_title[fullname] = title
# It's possible the filename could be used as the fullname too,
# just make sure it doesn't just exist in the map
filename = xml_file.stem
if fullname != filename:
assert filename not in fullname_to_title
fullname_to_title[filename] = title
# Go through the txt files
for input_file in input_files:
print(f"Processing page {input_file.stem}")
# Read associated XML file to obtain the fullname and title. Hypens and spaces make
# it impossible to automatically convert from the fullname to the title.
f = codecs.open(input_file, encoding='utf-8')
text = f.read()
base_filename = input_file.stem
assert base_filename in fullname_to_title
file_prefix = base_filename+"__"
converted_text, internal_links, linked_files = self.__converter.convert(
text, file_prefix=file_prefix, fullname_to_title=fullname_to_title
)
internal_links_map[fullname_to_title[base_filename]] = internal_links
# Get associated files
associated_dir = input_file.parent / input_file.stem
if not associated_dir.is_dir():
print(f" Did not find directory of associated files at {associated_dir}")
associated_files = []
associated_paths = []
else:
associated_paths = sorted(associated_dir.glob("*"))
associated_files = [f.name for f in associated_paths]
# Check that all files linked in the text can be found in the associated files
for linked_file in linked_files:
if linked_file not in associated_files:
print(" Linked file not found in associated dir:", linked_file)
# Check that all associated files have been linked
unlinked_associated_files = []
for associated_file in associated_files:
if associated_file not in linked_files:
xml_file = associated_file.split(".")[-1] == "xml"
if xml_file and not self.__args.include_associated_xml_files:
continue
print(" Associated file not found in linked files:", associated_file)
unlinked_associated_files.append(associated_file)
# Add any unlinked associated files to text
if len(unlinked_associated_files) > 0:
appendix = (
"\n== Unlinked Associated Files ==\n"
+ "In Wikidot, there were files associated with this page that were not linked in the text above:\n"
)
for unlinked_associated_file in unlinked_associated_files:
appendix += "* [[Media:"+file_prefix+unlinked_associated_file+"|"+unlinked_associated_file+"]]\n"
converted_text = converted_text + appendix
print(" Added appendix to converted text listing unlinked associated files")
# Copy all associated files to upload
upload_dir = dest_dir / "files_to_upload"
upload_dir.mkdir(parents=True, exist_ok=True)
existing_files = [f.name for f in sorted(upload_dir.glob("*"))]
for associated_path in associated_paths:
xml_file = associated_path.suffix == ".xml"
if xml_file and not self.__args.include_associated_xml_files:
continue
if associated_path.name in existing_files:
message = f"A file with the name {associated_path.name} already exists in {upload_dir}."
print(" "+message)
# Check if the files are identical
associated_bytes = associated_path.read_bytes()
upload_bytes = (upload_dir / associated_path.name).read_bytes()
if associated_bytes == upload_bytes:
print(" But the two files are identical.")
continue
else:
print(" And the two files are different.")
raise Exception(message)
upload_path = upload_dir / (file_prefix+associated_path.name)
print(f" Copying {associated_path} to {upload_path}")
shutil.copy(associated_path, upload_path)
# Write converted text
output_file = dest_dir / (fullname_to_title[base_filename]+'.mktxt')
print(f" Writing {output_file}")
self.write_unicode_file(output_file, converted_text)
# Find orphaned pages
orphaned_pages = {page:True for page in internal_links_map.keys()}
for _, links in internal_links_map.items():
for link in links:
if link in orphaned_pages:
orphaned_pages[link] = False
# Create page of pages that were processed, highlighting orphaned pages
processed_pages = (
"[https://github.com/bodekerscientific/wikidot-to-mediawiki Wikidot-to-MediaWiki] ran at "
+ f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}. It processed the following {len(input_files)} pages:\n"
)
any_orphaned = False
pages = sorted(orphaned_pages.keys(), key=str.lower)
for page in pages:
processed_pages += f"* [[{page}]]"
if orphaned_pages[page]:
processed_pages += " *"
any_orphaned = True
processed_pages += "\n"
if any_orphaned:
processed_pages += "\n<nowiki>*</nowiki> Orphaned page (that is, no pages link to the page).\n"
print("Writing report of conversion")
output_file = dest_dir / "Wikidot_to_MediaWiki_report.mktxt"
self.write_unicode_file(output_file, processed_pages)
def write_unicode_file(self, path_to_file, content):
try:
out_file = codecs.open(path_to_file,encoding='utf-8', mode='w')
out_file.write(content)
except:
print("Error on writing to file %s." % path_to_file)
def main():
""" Main function called to start the conversion."""
parser = argparse.ArgumentParser()
parser.add_argument('source', help="File or directory containing source files from Wikidot site")
parser.add_argument('dest', help="Directory to output files converted to MediaWiki format")
parser.add_argument(
"-x",
"--include-associated-xml-files",
default=False,
action='store_true',
help="Include any XML files in the files associated with source files"
)
arguments = parser.parse_args()
converter = ConversionController(arguments)
converter.convert(arguments.source, arguments.dest)
if __name__ == '__main__':
main()