diff options
author | Blaise Thompson <blaise@untzag.com> | 2020-04-11 18:59:35 -0500 |
---|---|---|
committer | Blaise Thompson <blaise@untzag.com> | 2020-04-11 18:59:35 -0500 |
commit | 5e2a82641aab330f9a94a74b5fa4386dd684d7f1 (patch) | |
tree | c7ec42c4c9e9a89c09660472f41a365cffd53d7b /build.py |
initial commit
Diffstat (limited to 'build.py')
-rwxr-xr-x | build.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/build.py b/build.py new file mode 100755 index 0000000..6afa673 --- /dev/null +++ b/build.py @@ -0,0 +1,77 @@ +import os +from pprint import pprint +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']) + + +env = jinja2.Environment(loader = jinja2.FileSystemLoader(str(__here__ / "templates"))) + + +# grab posts -------------------------------------------------------------------------------------- + + +@dataclass +class Post: + title: str + date: str + content: str + path: str + + +posts = [] +for post in os.listdir(__here__ / "posts"): + with open(__here__ / "posts" / post, "r") as f: + content = md.convert(f.read()) + kwargs = dict() + kwargs["title"] = md.Meta["title"][0] + kwargs["date"] = md.Meta["date"][0] + kwargs["content"] = content + kwargs["path"] = post[:-3] + print(kwargs) + posts.append(Post(**kwargs)) + +posts.sort(key=lambda p: p.date, reverse=True) + + +# 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(posts=posts, title="blog", date=date)) + + +# posts ------------------------------------------------------------------------------------------- + + +template = env.get_template("post.html") +for post in posts: + if not os.path.isdir(__here__ / "public" / post.path): + os.mkdir(__here__ / "public" / post.path) + with open(__here__ / "public" / post.path / "index.html", "w") as f: + f.write(template.render(post=post, title=post.title, 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 fh: + fh.write(template.render()) |