Skip to content

Commit

Permalink
Merge pull request #10 from williln/fix-alphabetization
Browse files Browse the repository at this point in the history
📝 Fix alphabetization
  • Loading branch information
williln authored Mar 19, 2024
2 parents 35a9704 + 4768359 commit d3594c2
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions update_readme.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Cribbed from Simon Willison: https://github.com/simonw/til/blob/0abdc32464f1bc726abebdbc147b945d22bae7a8/update_readme.py
"Run this after build_database.py - it needs til.db"
import pathlib
import sqlite_utils
import sys
Expand All @@ -12,26 +10,32 @@
if __name__ == "__main__":
db = sqlite_utils.Database(root / "til.db")
by_topic = {}
for row in db["til"].rows_where(order_by="created_utc"):
for row in db["til"].rows_where(order_by="topic"):
by_topic.setdefault(row["topic"], []).append(row)
index = ["<!-- index starts -->"]

# Alphabetize the topics
topics = list(by_topic.keys())
topics = sorted(by_topic.keys())

index = ["<!-- index starts -->"]
for topic in topics:
index.append("## {}\n".format(topic))
rows = by_topic[topic]

# Sort rows within the topic by title alphabetically
rows = sorted(by_topic[topic], key=lambda x: x["title"].lower())

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()
Expand Down

0 comments on commit d3594c2

Please sign in to comment.