#!/usr/bin/env python3 import sys from strictyaml import load as load_yaml from ipo import read, write, map, starstarmap, join, sort, ipo from glob import glob from markdown import markdown import re @ipo def parted(data, regex): """ Two parts of data, delimited by the first element on which regex fullmatches. If a delimiter is found, the second part is an iterator over the remaining elements. Else it is None. """ first = [] it = iter(data) try: while True: item = next(it) if re.fullmatch(regex, item): break first.append(item) except StopIteration: return (first, None) return (first, it) with open("blog.html") as blog_fh: TEMPLATE = blog_fh.read() with open(sys.argv[1]) as file_in: metadata_yaml, content = read(file_in) | parted("---+") metadata = load_yaml(metadata_yaml | join("\n")).data body = markdown(content | join("\n"), extras=[]) with open(sys.argv[2], "w") as file_out: TEMPLATE.format(**metadata, body=body) | write(file=file_out)