-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
95 lines (80 loc) · 2.58 KB
/
update.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
import os
import re
import subprocess
from datetime import datetime
import csv
import json
if __name__ == "__main__":
cwd = os.getcwd()
# collect unhidden directories
directories = sorted([
f for f in os.listdir()
if os.path.isdir(os.path.join(cwd, f))
and not f.startswith(".")
])
# collect entries within directories
topics = []
for directory in directories:
path = os.path.join(cwd, directory)
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) and f.endswith('.md')]
entries = []
for file in files:
heading = file
with open(os.path.join(path, file)) as f:
for line in f:
line = line.strip()
if line.startswith('# '):
heading = line[1:].strip()
break
# get creation date from git
dates_str = subprocess.run([
"git", "log",
"--pretty=format:%cs",
os.path.join(path, file),
], stdout=subprocess.PIPE)
dates = [d for d in dates_str.stdout.decode('utf-8').split("\n") if d]
if dates:
date = re.match(r'.*(\d\d\d\d-\d\d-\d\d).*', dates[-1]).groups()[0]
else:
date = datetime.fromtimestamp(os.path.getmtime(os.path.join(path, file))).strftime("%Y-%m-%d")
entries.append({
"path": os.path.join(directory, file),
"heading": heading,
"date": date
})
# sort articles alphabetically
entries = sorted(entries, key=lambda d: d['heading'])
topics.append({ "heading": directory, "entries": entries })
# build readme file
readme_file = os.path.join(cwd, "README.md")
readme_list = []
with open(readme_file) as f:
for line in f:
readme_list.append(line)
if line.startswith("<!-- entries: start -->"):
break
readme_list.append("\n")
for topic in topics:
readme_list.append(f"## {topic['heading']}\n\n")
for entry in topic["entries"]:
readme_list.append(f"- [{entry['heading']}]({entry['path']}) - {entry['date']}\n")
readme_list.append("\n")
# write to readme file
with open(readme_file, "w") as f:
f.write("".join(readme_list))
# flatten topics
topics_flat = [
{**entry, 'topic': topic['heading']}
for topic in topics
for entry in topic["entries"]
]
# write to csv file
with open('data.csv', 'w') as f:
fieldnames = topics_flat[0].keys()
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for row in topics_flat:
writer.writerow(row)
# write to json file
with open('data.json', 'w') as f:
json.dump(topics_flat, f)