blog/index_gen.py
2025-05-22 16:39:18 +00:00

37 lines
982 B
Python

import re
import json
import subprocess
def get_last_commit_info():
# Format: subject%n%ISO 8601 date
result = subprocess.run(
["git", "log", "-1", "--pretty=%s%n%cI"],
stdout=subprocess.PIPE,
text=True,
check=True
)
message, date = result.stdout.strip().split("\n", 1)
return message, date
def parse_blog_links(md_text):
pattern = re.compile(r"- \[(.*?)\]\((.*?)\)")
return [{"title": title, "url": url} for title, url in pattern.findall(md_text)]
def main():
with open("README.md", "r", encoding="utf-8") as f:
md_content = f.read()
blog_entries = parse_blog_links(md_content)
last_commit_msg, commit_date = get_last_commit_info()
for entry in blog_entries:
entry["last_commit"] = last_commit_msg
entry["commit_date"] = commit_date
with open("index.json", "w", encoding="utf-8") as f:
json.dump(blog_entries, f, indent=2)
if __name__ == "__main__":
main()