-
Notifications
You must be signed in to change notification settings - Fork 1
/
export-to-a9v2.py
executable file
·71 lines (60 loc) · 1.84 KB
/
export-to-a9v2.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
#!/usr/bin/env python3
import json
import os
import re
import shutil
import sys
BASEDIR = 'a9v2-exported'
INDEXED_TITLE = re.compile(r'^\[([^\]]*)\] (.*)$')
def normalize(name):
"""Returns a name suitable for a filename."""
name = re.sub(r'[^a-z0-9]', ' ', name.lower())
name = re.sub(r'\s+', '-', name.strip())
if not name:
name = 'untitled'
return name
def format_content(content):
# KaTeX uses aligned instead
content = content.replace('align*', 'aligned')
# Renamed the macro
content = content.replace(r'\trps', r'\T')
return content
def main():
# Read the names file
books = {}
with open('data/names') as fin:
for line in fin:
tokens = line.rstrip('\n').split('\t')
if tokens[0] == 'B':
name = tokens[2]
norm_name = normalize(name)
while norm_name in books.values():
norm_name += '-alt'
books[tokens[1]] = norm_name
os.makedirs(os.path.join(BASEDIR, norm_name), exist_ok=True)
else:
nid = tokens[1]
book = books[tokens[2]]
name = tokens[3]
m = INDEXED_TITLE.match(name)
if m:
index, title = m.groups()
else:
index, title = '', name
norm_name = normalize(name)
print(book, '/', norm_name, '|||', index, '|||', title)
src_file = os.path.join('data', nid + '.md')
tgt_file = os.path.join(BASEDIR, book, norm_name + '.md')
while os.path.exists(tgt_file):
tgt_file = tgt_file + '-alt.md'
with open(src_file) as fin:
with open(tgt_file, 'w') as fout:
print('<!-- ' + json.dumps({
'index': index,
'title': title,
'timestamp': 0,
}) + ' -->', file=fout)
content = format_content(fin.read())
fout.write(content)
if __name__ == '__main__':
main()