-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuild.py
150 lines (109 loc) · 3.93 KB
/
build.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import datetime
import pathlib
from typing import Sequence
import shutil
import markdown
import markdown.extensions.fenced_code
import markdown_link_attr_modifier
import pymdownx.magiclink
import frontmatter
import jinja2
import highlighting
import witchhazel
jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader("templates"),
)
jinja_env.globals["current_year"] = datetime.datetime.now().year
markdown_ = markdown.Markdown(
extensions=[
"toc",
"admonition",
"tables",
"abbr",
"attr_list",
"footnotes",
"pymdownx.smartsymbols",
"pymdownx.tilde",
"pymdownx.caret",
markdown.extensions.fenced_code.FencedCodeExtension(lang_prefix="language-"),
pymdownx.magiclink.MagiclinkExtension(
hide_protocol=False,
),
markdown_link_attr_modifier.LinkAttrModifierExtension(
new_tab="external_only", custom_attrs=dict(referrerpolicy="origin")
),
]
)
def copy_static():
pathlib.Path("./docs").mkdir(parents=True, exist_ok=True)
shutil.copytree(
pathlib.Path("./static"), pathlib.Path("./docs/static"), dirs_exist_ok=True
)
def get_sources():
yield from pathlib.Path(".").glob("srcs/*.md")
yield from pathlib.Path(".").glob("srcs/*/index.md")
def parse_source(source: pathlib.Path) -> frontmatter.Post:
post = frontmatter.load(str(source))
return post
def fixup_styles(content: str) -> str:
content = content.replace("<table>", '<table class="table">')
return content
def render_markdown(content: str) -> str:
markdown_.reset()
content = markdown_.convert(content)
content = highlighting.highlight(content)
content = fixup_styles(content)
return content
def write_post(post: frontmatter.Post, content: str):
dst = pathlib.Path(f"./docs/{post['stem']}")
dst.mkdir(parents=True, exist_ok=True)
index = dst / "index.html"
template = jinja_env.get_template("post.html")
rendered = template.render(post=post, content=content)
index.write_text(rendered)
def copy_post_resources(post: frontmatter.Post):
src = post["source"].parent
dst = pathlib.Path(f"./docs/{post['stem']}")
dst.mkdir(parents=True, exist_ok=True)
shutil.copytree(src, dst, dirs_exist_ok=True)
def write_posts() -> Sequence[frontmatter.Post]:
posts = []
sources = get_sources()
for source in sources:
post = parse_source(source)
content = render_markdown(post.content)
post["source"] = source
if source.match("*/index.md"):
post["stem"] = source.parent.name
copy_post_resources(post)
else:
post["stem"] = source.stem
write_post(post, content)
posts.append(post)
return posts
def write_pygments_style_sheet():
css = highlighting.get_style_css(witchhazel.WitchHazelStyle)
pathlib.Path("./docs/static/pygments.css").write_text(css)
def write_index(posts: Sequence[frontmatter.Post]):
posts = sorted(posts, key=lambda post: post["date"], reverse=True)
path = pathlib.Path("./docs/index.html")
template = jinja_env.get_template("index.html")
rendered = template.render(posts=posts)
path.write_text(rendered)
def write_rss(posts: Sequence[frontmatter.Post]):
posts = sorted(posts, key=lambda post: post["date"], reverse=True)
path = pathlib.Path("./docs/feed.xml")
template = jinja_env.get_template("rss.xml")
rendered = template.render(posts=posts, root="https://blog.thea.codes")
path.write_text(rendered)
def write_cname():
pathlib.Path("./docs/CNAME").write_text("blog.thea.codes")
def main():
copy_static()
write_pygments_style_sheet()
posts = write_posts()
write_index(posts)
write_rss(posts)
write_cname()
if __name__ == "__main__":
main()