2025-05-22 18:39:18 +02:00
|
|
|
import re
|
|
|
|
import json
|
|
|
|
import subprocess
|
2025-05-22 18:42:46 +02:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
def get_local_path_from_url(url):
|
|
|
|
"""
|
2025-05-22 18:44:34 +02:00
|
|
|
Converts blog URL to local file path assuming structure:
|
|
|
|
https://.../src/branch/main/<file> --> blog/<file>
|
2025-05-22 18:42:46 +02:00
|
|
|
"""
|
2025-05-22 18:44:34 +02:00
|
|
|
path = urlparse(url).path
|
2025-05-22 18:42:46 +02:00
|
|
|
parts = path.strip("/").split("/")
|
2025-05-22 18:44:34 +02:00
|
|
|
|
2025-05-22 18:42:46 +02:00
|
|
|
try:
|
|
|
|
idx = parts.index("main")
|
|
|
|
relpath = "/".join(parts[idx + 1:])
|
2025-05-22 18:46:21 +02:00
|
|
|
return f"{relpath}"
|
2025-05-22 18:42:46 +02:00
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_commit_info(filepath):
|
|
|
|
try:
|
|
|
|
result = subprocess.run(
|
|
|
|
["git", "log", "-1", "--pretty=%s%n%cI", "--", filepath],
|
|
|
|
stdout=subprocess.PIPE,
|
2025-05-22 18:44:34 +02:00
|
|
|
stderr=subprocess.DEVNULL,
|
2025-05-22 18:42:46 +02:00
|
|
|
text=True,
|
|
|
|
check=True
|
|
|
|
)
|
2025-05-22 18:44:34 +02:00
|
|
|
output = result.stdout.strip().split("\n")
|
|
|
|
if len(output) < 2:
|
|
|
|
return "uncommitted or no history", "unknown"
|
|
|
|
return output[0], output[1]
|
2025-05-22 18:42:46 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2025-05-22 18:44:34 +02:00
|
|
|
return "uncommitted or error", "unknown"
|
2025-05-22 18:39:18 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
for entry in blog_entries:
|
2025-05-22 18:42:46 +02:00
|
|
|
local_path = get_local_path_from_url(entry["url"])
|
|
|
|
if local_path:
|
|
|
|
msg, date = get_commit_info(local_path)
|
|
|
|
else:
|
2025-05-22 18:44:34 +02:00
|
|
|
msg, date = "invalid url", "unknown"
|
|
|
|
entry["last_commit"] = msg
|
|
|
|
entry["commit_date"] = date
|
2025-05-22 18:39:18 +02:00
|
|
|
|
|
|
|
with open("index.json", "w", encoding="utf-8") as f:
|
|
|
|
json.dump(blog_entries, f, indent=2)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|