-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.py
38 lines (34 loc) · 1.13 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
# Converts a MediaWiki source file to Quarto markdown. Assumes
# converted elements (hyperlinks, math sections, etc.) do not span
# multiple lines.
import re
import sys
if len(sys.argv) != 2:
print("usage: python convert.py mediawiki-source-file")
sys.exit(1)
print("---")
print("title: \"page title goes here\"")
print("---")
print()
for l in open(sys.argv[1]):
m = re.match("(=+)(.*?)=+ *$", l)
if m: # header
level = len(m[1])
header = m[2].strip()
print("#"*level + " " + header)
else:
l = l.strip()
# Bold
l = re.sub("'''(.*?)'''", r"**\1**", l)
l = re.sub("<strong>(.*?)</strong>", r"**\1**", l)
# Italic
l = re.sub("''(.*?)''", r"_\1_", l)
# TeX math sections
l = re.sub("<math> *(.*?) *</math>", r"$\1$", l)
# Citations - just comment out for now
l = re.sub("<ref> *(.*?) *</ref>", r"<!-- [@citation] \1 -->", l)
# Hyperlinks
l = re.sub(r"\[(http[^ ]+) +(.*?) *\]", r"[\2](\1)", l)
# Embedded images
l = re.sub(r"\[\[File:(.*?)\|.*\| *(.*?) *\]\]", r"![\2](\1)", l)
print(l)