forked from hollobit/ttmik
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.py
More file actions
42 lines (33 loc) · 1.23 KB
/
compile.py
File metadata and controls
42 lines (33 loc) · 1.23 KB
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
# compile notes.md into cards.txt, ready to be imported into Anki
# only cards (of the form front=back) are compiled
# both directions are compiled, so front=back and back=front
import os
import re
# read the input markdown
in_path = "notes.md"
lines = open(in_path, 'r').readlines()
line_from = 0 # we may wish to filter which lines to compile
line_to = len(lines)
# output file
out_path = "cards.txt"
output = open(out_path, 'w')
# process all the lines
ncards = 0
for line in lines[line_from:line_to]:
if not "=" in line:
# only lines with = denote cards we want to compile
continue
# preprocessing
sline = line
# get rid of any romanization, which is inside square brackets
sline = re.sub(r'\[.*?\]', '', sline)
# strip any whitespace on both ends
sline = sline.strip()
# find either the = delimiter, and add both the front and back of the card
parts = [p.strip() for p in sline.split('=')]
assert len(parts) == 2, 'Invalid line: ' + line
# write to output both the normal and reversed order
output.write(parts[0] + ';' + parts[1] + '\n') # normal order
output.write(parts[1] + ';' + parts[0] + '\n') # reversed order
ncards += 2
print(ncards, 'cards compiled')