bin/update_index.py (view raw)
1#!/usr/bin/env python3
2
3from markdown2 import markdown_path
4import os
5import fileinput
6import sys
7
8# change our cwd
9os.chdir("bin")
10
11blog = "../pages/blog/"
12
13# bunch of file hacks to get to the most recent file
14def getrecent(path):
15 files = [path + f for f in os.listdir(blog) if f not in ["_index.md", "feed.xml"]]
16 files.sort(key=os.path.getmtime, reverse=True)
17 return files[0]
18
19
20def update_index(s):
21 path = "../pages/_index.md"
22 with open(path, "r") as f:
23 md = f.readlines()
24 ruler = md.index("| --- | --: |\n")
25 md[ruler + 1] = s + "\n"
26
27 with open(path, "w") as f:
28 f.writelines(md)
29
30
31def update_blog(s):
32 path = "../pages/blog/_index.md"
33 s = s + "\n"
34 for l in fileinput.FileInput(path, inplace=1):
35 if "--:" in l:
36 l = l.replace(l, l + s)
37 print(l, end=""),
38
39
40# fetch title and date
41meta = markdown_path(getrecent(blog), extras=["metadata"]).metadata
42fname = os.path.basename(os.path.splitext(getrecent(blog))[0])
43url = "/blog/" + fname
44line = f"| [{meta['title']}]({url}) | `{meta['date']}` |"
45
46update_index(line)
47update_blog(line)