all repos — site @ 8f1b973072c45c5fd7abd4c43626ede7e4bf3c6f

source for my site, found at icyphox.sh

bin/update_index.py (view raw)

 1#!/usr/bin/env python3
 2
 3import os
 4import frontmatter as fm
 5import jinja2
 6from jinja2 import DebugUndefined
 7
 8
 9def get_metas(blog):
10    all_metas = []
11    files  = [ blog + f for f in os.listdir(blog) if f not in ["_index.md", "feed.xml"]]
12    for f in files:
13        with open(f) as fx:
14            meta, _ = fm.parse(fx.read())
15            all_metas.append(meta)
16    
17    all_metas.sort(key=lambda x: x['date'], reverse=True)
18    return all_metas
19
20
21def jinja_render(posts, tmpl):
22    template_loader = jinja2.FileSystemLoader("./")
23    env = jinja2.Environment(loader=template_loader, undefined=DebugUndefined)
24    try:
25        template = env.get_template(tmpl)
26    except jinja2.exceptions.TemplateNotFound:
27        print("error: template not found")
28
29    out = template.render(posts=posts)
30    return out
31
32
33if __name__ == "__main__":
34    all_metas = get_metas("pages/blog/")
35    rendered = jinja_render(all_metas, "templates/_index.html") 
36    with open("templates/index.html", "w") as f:
37        f.write(rendered)