all repos — site @ ca17f2e0e1b0ca9365736ccd15f66e029c3cba7a

source for my site, found at icyphox.sh

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
13print("this is going to take a while...")
14print("you might as well serve the site!")
15
16# order files by recency
17def get_recents(path):
18    files = [path + f for f in os.listdir(blog) if f not in ["_index.md", "feed.xml"]]
19    files.sort(
20        key=lambda f: markdown_path(f, extras=["metadata"]).metadata["date"],
21        reverse=True,
22    )
23    return files
24
25
26def update_index(posts):
27    path = "../pages/_index.md"
28    with open(path, "r") as f:
29        md = f.readlines()
30    ruler = md.index("| :-- | --: |\n")
31    for post, i in zip(posts, range(5)):
32        md[ruler + i + 1] = post + "\n"
33
34    with open(path, "w") as f:
35        f.writelines(md)
36
37
38def update_blog(s):
39    path = "../pages/_index.md"
40    s = s + "\n"
41    with open(path) as f:
42        tempf = f.readlines()
43
44    if s in tempf:
45        print("index has already been updated. quitting...")
46        sys.exit()
47    for l in fileinput.FileInput(path, inplace=1):
48        if "--:" in l:
49            l = l.replace(l, l + s)
50        print(l, end=""),
51
52
53top_five = []
54metas = []
55lines = []
56fnames = []
57
58for i in range(5):
59    top_five.append(get_recents(blog)[i])
60    metas.append(markdown_path(get_recents(blog)[i], extras=["metadata"]).metadata)
61    fnames.append(os.path.basename(os.path.splitext(get_recents(blog)[i])[0]))
62
63for meta, fname in zip(metas, fnames):
64    url = "/blog/" + fname
65    new_line = f"| [{meta['title']}]({url}) | {meta['date']} |"
66    lines.append(new_line)
67
68update_blog(lines[0])