Milán Major

devsite-builder

1 branch
Code

site.py

#!/usr/bin/env python3

"""
Top-level orchestrator: builds both the blog and the git
browser into www/ with a single command.
"""

import argparse
import os

import blog
import gitweb
import siteutils

OUTPUT_DIR = "www"


def build_top_level_pages():

    os.makedirs(OUTPUT_DIR, exist_ok=True)

    for name in ("home", "about"):

        template = siteutils.load_template(f"{name}.html")
        html = siteutils.render(template, {})

        filename = "index.html" if name == "home" else "about.html"

        with open(
            os.path.join(OUTPUT_DIR, filename),
            "w",
            encoding="utf-8"
        ) as f:
            f.write(html)

    print("  home")
    print("  about")


def build():
    blog_site = blog.Blog()
    blog_site.load()

    print(f"Loaded {len(blog_site.posts)} posts.")

    blog.build(blog_site)
    gitweb.build()

    print("Building top-level pages...")
    build_top_level_pages()


def clean():
    blog.clean()
    gitweb.clean()


def main():

    parser = argparse.ArgumentParser(
        description="Milán Major's tiny site builder"
    )

    subparsers = parser.add_subparsers(dest="command")

    subparsers.add_parser("build", help="build the whole site")
    subparsers.add_parser("clean", help="remove all generated files")

    args = parser.parse_args()

    if args.command == "clean":
        clean()
        return

    if args.command == "build":
        build()
        return

    parser.print_help()


if __name__ == "__main__":
    main()