commit d6ae48ed2539f78e5b0d32c670d0e521d7cd3c08 Author: Sergio Martínez Portela Date: Sat Aug 14 18:04:22 2021 +0200 Add base structure for generation script. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c7ac740 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_gen \ No newline at end of file diff --git a/_scripts/generate.py b/_scripts/generate.py new file mode 100644 index 0000000..a898e4d --- /dev/null +++ b/_scripts/generate.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 + +import logging +import os +import sys +import uuid +from datetime import datetime + +from org_rw import OrgTime +from org_rw import dump as dump_org +from org_rw import dumps as dumps_org +from org_rw import load as load_org + +EXTENSIONS = [ + ".org", + ".org.txt", +] + + +def load_all(top_dir_relative): + top = os.path.abspath(top_dir_relative) + + docs = [] + + for root, dirs, files in os.walk(top): + for name in files: + if ".org" not in name: + continue + + path = os.path.join(root, name) + + try: + doc = load_org(open(path), extra_cautious=True) + docs.append(doc) + except Exception as err: + import traceback + + traceback.print_exc() + print(f"== On {path}") + sys.exit(1) + + logging.info("Collected {} files".format(len(docs))) + return docs + + +def main(src_top, dest_top): + docs = load_all(src_top) + + os.makedirs(dest_top, exist_ok=True) + for doc in docs: + relpath = os.path.relpath(doc.path, src_top) + changed = False + headlines = list(doc.getTopHeadlines()) + related = None + + i = len(headlines) + while i > 0: + i -= 1 + headline = headlines[i] + if headline.title.strip().lower() == "related": + assert related is None + related = headline + headlines.pop(i) + + for headline in headlines: + if headline.id is None: + headline.id = str(uuid.uuid4()) + changed = True + + if changed: + print("Updated", relpath) + save_changes(doc) + + if not relpath.startswith("public/"): + # print("Skip:", relpath) + continue + pubpath = relpath.split("/", 1)[1] + endpath = pubpath + ".html" + for extension in EXTENSIONS: + if pubpath.endswith(extension): + endpath = pubpath[: -len(extension)] + ".html" + break + print("PUBLIC", pubpath, "→", endpath) + + endpath = os.path.join(dest_top, endpath) + dirname = os.path.dirname(endpath) + if len(dirname) > 0: + os.makedirs(dirname, exist_ok=True) + with open(endpath, "wt") as f: + f.write(render(doc)) + + +def render(doc): + return dumps_org(doc) + + +def save_changes(doc): + assert doc.path is not None + with open(doc.path, "wt") as f: + dumps_org(doc, f) + + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Usage: {} SOURCE_TOP DEST_TOP".format(sys.argv[0])) + exit(0) + + logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s") + main(sys.argv[1], sys.argv[2])