Files
kochstube/rebuild.py
T

33 lines
901 B
Python

#!/usr/bin/env python3
"""Rebuild index.html from template + rezepte/*.json, sorted newest first."""
import json, glob, os
BASE = os.path.dirname(os.path.abspath(__file__))
# Load all recipes
rezepte = []
for f in sorted(glob.glob(os.path.join(BASE, 'rezepte', '*.json'))):
with open(f) as fh:
rezepte.append(json.load(fh))
# Sort newest first by 'erstellt' timestamp
rezepte.sort(key=lambda r: r.get('erstellt', '2000-01-01'), reverse=True)
# Load template
with open(os.path.join(BASE, 'template.html')) as fh:
template = fh.read()
# Inject data
js = json.dumps(rezepte, ensure_ascii=False)
html = template.replace('__REZEPTE_JSON__', js)
# Write output
out = os.path.join(BASE, 'index.html')
with open(out, 'w') as fh:
fh.write(html)
print(f'index.html rebuilt: {len(rezepte)} Rezepte, {len(html)} bytes')
for r in rezepte:
print(f' [{r["erstellt"]}] {r["titel"]}')