-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_readme.py
40 lines (36 loc) · 1.4 KB
/
update_readme.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
"Run this after build_database.py - it needs til.db"
import pathlib
import sqlite_utils
import sys
import re
root = pathlib.Path(__file__).parent.resolve()
index_re = re.compile(r"<!\-\- index starts \-\->.*<!\-\- index ends \-\->", re.DOTALL)
count_re = re.compile(r"<!\-\- count starts \-\->.*<!\-\- count ends \-\->", re.DOTALL)
COUNT_TEMPLATE = "<!-- count starts -->{}<!-- count ends -->"
if __name__ == "__main__":
db = sqlite_utils.Database(root / "til.db")
by_topic = {}
for row in db["til"].rows_where(order_by="created_utc"):
by_topic.setdefault(row["topic"], []).append(row)
index = ["<!-- index starts -->"]
for topic, rows in by_topic.items():
index.append("## {}\n".format(topic))
for row in rows:
index.append(
"* [{title}]({url}) - {date}".format(
date=row["created"].split("T")[0], **row
)
)
index.append("")
if index[-1] == "":
index.pop()
index.append("<!-- index ends -->")
if "--rewrite" in sys.argv:
readme = root / "README.md"
index_txt = "\n".join(index).strip()
readme_contents = readme.open().read()
rewritten = index_re.sub(index_txt, readme_contents)
rewritten = count_re.sub(COUNT_TEMPLATE.format(db["til"].count), rewritten)
readme.open("w").write(rewritten)
else:
print("\n".join(index))