summaryrefslogtreecommitdiff
path: root/build.py
blob: 6a62eeeb041e67eb43b27486ee6317355113705d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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")))


shutil.rmtree(__here__ / "public")
os.mkdir(__here__ / "public")



# grab posts --------------------------------------------------------------------------------------


@dataclass
class Post:
    title: str
    date: str
    content: str
    path: str
    tags: list


posts = []
tags = []
for post in os.listdir(__here__ / "posts"):
    if not post.endswith(".md"):
        shutil.copytree(__here__ / "posts" / post,
                        __here__ / "public" / post)
        continue
    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]
        kwargs["tags"] = md.Meta.get("tags", [""])[0].split()
        tags += kwargs["tags"]
        posts.append(Post(**kwargs))

posts.sort(key=lambda p: p.date, reverse=True)

tags = list(set(tags))
tags.sort()


# index -------------------------------------------------------------------------------------------


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:
    print(post.path)
    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))


# tags --------------------------------------------------------------------------------------------



template = env.get_template("tags.html")
if not os.path.isdir(__here__ / "public" / "tags"):
    os.mkdir(__here__ / "public" / "tags")
with open(__here__ / "public" / "tags" / "index.html", "w") as f:
    f.write(template.render(posts=posts, tags=tags, title="tags", 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())