#!/usr/bin/env python3 """Build public/ from the Markdown in content/. Posts are content/posts/YYYY-MM-DD-slug.md, pages are content/pages/slug.md. The first line of a file is its title; the rest is the body. """ import html import re import shutil from datetime import datetime, timezone from pathlib import Path import markdown SITE_TITLE = "onlocalhost.dev" SITE_URL = "https://onlocalhost.dev" SITE_DESCRIPTION = "Just a personal website, like we used to make." ROOT = Path(__file__).resolve().parent CONTENT = ROOT / "content" TEMPLATES = ROOT / "templates" STATIC = ROOT / "static" OUT = ROOT / "public" FILENAME = re.compile(r"^(\d{4}-\d{2}-\d{2})-(.+)\.md$") EXTERNAL_LINK = re.compile(r']+>") SENTENCE = re.compile(r"(?<=[.!?])\s") def render(template, **values): text = (TEMPLATES / template).read_text(encoding="utf-8") return PLACEHOLDER.sub(lambda m: str(values.get(m.group(1), "")), text) def read(path): heading, _, body = path.read_text(encoding="utf-8").strip().partition("\n") title = heading.lstrip("#").strip() converted = markdown.markdown(body.strip(), extensions=["smarty"]) converted = EXTERNAL_LINK.sub(r'' f'{html.escape(p["title"])}' for p in posts ) write("/", page(SITE_TITLE, render("index.html", posts=items))) def build_feed(posts): items = [] for p in posts: published = datetime.strptime(p["date"], "%Y-%m-%d").replace(tzinfo=timezone.utc) link = SITE_URL + p["url"] items.append( "\n" f"{html.escape(p['title'])}\n" f"{link}\n" f"{link}\n" f"{published.strftime('%a, %d %b %Y %H:%M:%S +0000')}\n" f"\n" "" ) feed = render("feed.xml", site_title=SITE_TITLE, site_url=SITE_URL, site_description=SITE_DESCRIPTION, items="\n".join(items)) (OUT / "feed.xml").write_text(feed, encoding="utf-8") def main(): if OUT.exists(): shutil.rmtree(OUT) OUT.mkdir() posts = build_posts() build_pages() build_index(posts) build_feed(posts) shutil.copytree(STATIC, OUT, dirs_exist_ok=True) print(f"{len(posts)} posts, written to {OUT.relative_to(ROOT)}/") if __name__ == "__main__": main()