all repos — site @ aeb5040362059210aaf0ad6fd5fc9c8370bc4a75

source for my site, found at icyphox.sh

bin/update_index.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
#!/usr/bin/env python3

from markdown2 import markdown_path
import os
import fileinput
import sys

# change our cwd
os.chdir("bin")

blog = "../pages/blog/"

print("this is going to take a while...")
print("you might as well serve the site!")

# order files by recency
def get_recents(path):
    files = [path + f for f in os.listdir(blog) if f not in ["_index.md", "feed.xml"]]
    files.sort(
        key=lambda f: markdown_path(f, extras=["metadata"]).metadata["date"],
        reverse=True,
    )
    return files


def update_index(posts):
    path = "../pages/_index.md"
    with open(path, "r") as f:
        md = f.readlines()
    ruler = md.index("| :-- | --: |\n")
    for post, i in zip(posts, range(5)):
        md[ruler + i + 1] = post + "\n"

    with open(path, "w") as f:
        f.writelines(md)


def update_blog(s):
    path = "../pages/blog/_index.md"
    s = s + "\n"
    with open(path) as f:
        tempf = f.readlines()

    if s in tempf:
        print("index has already been updated. quitting...")
        sys.exit()
    for l in fileinput.FileInput(path, inplace=1):
        if "--:" in l:
            l = l.replace(l, l + s)
        print(l, end=""),


top_five = []
metas = []
lines = []
fnames = []

for i in range(5):
    top_five.append(get_recents(blog)[i])
    metas.append(markdown_path(get_recents(blog)[i], extras=["metadata"]).metadata)
    fnames.append(os.path.basename(os.path.splitext(get_recents(blog)[i])[0]))

for meta, fname in zip(metas, fnames):
    url = "/blog/" + fname
    new_line = f"| [{meta['title']}]({url}) | {meta['date']} |"
    lines.append(new_line)

update_index(lines)
update_blog(lines[0])