From 6fa3d6d03c4f0731cf388166bacebbf2e062fd1c Mon Sep 17 00:00:00 2001 From: Blaise Thompson Date: Thu, 8 Jul 2021 16:42:51 -0500 Subject: refactor with actual building --- build.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100755 build.py (limited to 'build.py') diff --git a/build.py b/build.py new file mode 100755 index 0000000..b55b62e --- /dev/null +++ b/build.py @@ -0,0 +1,86 @@ +import os +import shutil +import jinja2 +import markdown +import pathlib +from datetime import datetime +from dataclasses import dataclass + + +__here__ = pathlib.Path(__file__).resolve().parent + + +date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + + +md = markdown.Markdown(extensions=['meta', "toc", "extra"]) + + +env = jinja2.Environment(loader = jinja2.FileSystemLoader(str(__here__ / "templates"))) + + +# grab memories ----------------------------------------------------------------------------------- + + +@dataclass +class Memory: + title: str + start: str + end: str + content: str + path: tuple + + +(__here__ / "public").mkdir(exist_ok = True) +template = env.get_template("memory.html") +memories = [] + +for directory, _, files in os.walk(__here__ / "memories"): + directory = pathlib.Path(directory) + directory = directory.relative_to(__here__ / "memories") + if directory == pathlib.Path("."): + continue + (__here__ / "public" / directory).mkdir(exist_ok = True) + for f in files: + f = pathlib.Path(f) + shutil.copy(__here__ / "memories" / directory / f, + __here__ / "public" / directory / f) + if f.suffix == ".md": + with open(__here__ / "memories" / directory / f, "r") as f: + content = md.convert(f.read()) + kwargs = dict() + kwargs["title"] = md.Meta["title"][0] + kwargs["start"] = md.Meta["start"][0] + kwargs["end"] = md.Meta.get("end", ["ongoing"])[0] + kwargs["content"] = content + kwargs["path"] = directory.parts + memory = Memory(**kwargs) + memories.append(memory) + with open(__here__ / "public" / directory / "index.html", "w") as out: + out.write(template.render(memory=memory, date=date)) + +memories.sort(key=lambda p: p.end, reverse=True) + +for memory in memories: + print(memory.title) + + +# index ------------------------------------------------------------------------------------------- + + +if not os.path.isdir(__here__ / "public"): + os.mkdir(__here__ / "public") + + +template = env.get_template("index.html") +with open(__here__ / "public" / "index.html", "w") as f: + f.write(template.render(memories=memories, date=date)) + + +# css --------------------------------------------------------------------------------------------- + + +template = env.get_template('style.css') +for d, _, _ in os.walk(__here__ / "public", topdown=False): + with open(os.path.join(d, "style.css"), 'w') as f: + f.write(template.render()) -- cgit v1.2.3