-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.py
executable file
·80 lines (64 loc) · 2.14 KB
/
split.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
#!/usr/bin/python3
import re
from pathlib import Path
from collections import defaultdict
import urllib.parse
output = None
seen = set()
recipes = defaultdict(dict)
with open("export.txt") as fd:
for line in fd.readlines():
line = line.strip()
line = re.sub(r' +', ' ', line)
# print(line)
if line == '_____':
continue
if line == '@@@@@':
title = None
category = None
section = 0
continue
if line == '|':
section += 1
if section == 1:
output.write("\n")
if section == 2:
output.write("\n### Ingredients\n\n")
if section == 3:
output.write("\n### Directions\n\n")
continue
if section == 0:
if category is None:
category = line
continue
if title is None:
title = line
Path(category).mkdir(parents=True, exist_ok=True)
if title in seen:
assert False, f"Duplicate title: {title}"
seen.add(title)
filename = f"{category}/{title}.md"
output = open(filename, "w")
output.write(f"# {category}\n\n")
output.write(f"## {title}\n")
recipes[category][title] = filename
continue
assert False, f"{title}: There was another line in section 0: {line}"
if section == 1:
output.write(f"* {line}\n")
if section == 2:
output.write(f"* {line}\n")
if section == 3:
output.write(f"{line}\n")
if section == 4:
assert False, f"Section #{section}???"
if output:
output.close()
with open("README.md", "w") as fd:
fd.write("# Grandma's Cookbook\n")
for category in sorted(recipes.keys()):
fd.write(f"## {category}\n")
for title in sorted(recipes[category].keys()):
filename = recipes[category][title]
filename = urllib.parse.quote(filename)
fd.write(f"* [{title}]({filename})\n")