all repos — site @ 5525c34a114052c4de0107c3dc1c7665ad12bb15

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
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)
16             if f not in ['_index.md', 'feed.xml']]
17    files.sort(key=os.path.getmtime, reverse=True)
18    return files[0]
19
20
21def update_index(s):
22    path = '../pages/_index.md'
23    with open(path, 'r') as f:
24        md = f.readlines()
25    ruler = md.index('| --- | --: |\n')
26    md[ruler + 1] = s + '\n'
27
28    with open(path, 'w') as f:
29        f.writelines(md)
30
31
32def update_blog(s):
33    path = '../pages/blog/_index.md'
34    s = s + '\n'
35    for l in fileinput.FileInput(path, inplace=1):
36        if "--:" in l:
37            l=l.replace(l, l + s)
38        print(l, end=''),
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)
48
49