-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerate-html.py
executable file
·89 lines (70 loc) · 2.44 KB
/
generate-html.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
#!/usr/bin/env python
# copied from https://github.com/tonybaloney/tonybaloney.github.io/blob/master/blog-gen.py
from dataclasses import dataclass
from datetime import date, datetime, time
from email.utils import formatdate, format_datetime # for RFC2822 formatting
from pathlib import Path
import jinja2
import markdown
TEMPLATE_FILE = "templates/blog_post_template.html"
FEED_TEMPLATE_FILE = "templates/rss_feed_template.xml"
BLOG_POSTS_PATH = Path("posts")
OUTPUT_DIR = Path(".")
@dataclass
class Post:
title: str
author: str
md_path: Path
date: date
@property
def html_path(self):
return OUTPUT_DIR / "blog" / self.md_path.name.replace('.md', '.html')
@property
def url(self):
return f"/blog/{self.html_path.name}"
@property
def rfc2822_date(self):
return format_datetime(datetime.combine(self.date, time(12, 00)))
def main():
md_post_paths = sorted(BLOG_POSTS_PATH.glob("*.md"))
extensions = ['extra', 'smarty', 'meta', 'codehilite']
_md = markdown.Markdown(extensions=extensions, output_format='html5')
loader = jinja2.FileSystemLoader(searchpath="./")
env = jinja2.Environment(loader=loader)
all_posts = []
for md_post_path in md_post_paths:
# print("rendering", md_post_path)
post_date = date.fromisoformat(md_post_path.name[:10])
html_content = _md.convert(md_post_path.read_text())
post = Post(
md_path=md_post_path, date=post_date,
author=_md.Meta['author'][0],
title=_md.Meta['title'][0],
)
post_html = env.get_template(TEMPLATE_FILE).render(
content=html_content, url=post.url, post=post,
**_md.Meta,
)
print("writing", post.html_path)
post.html_path.write_text(post_html)
all_posts.append(post)
# Order blog posts by date published
all_posts.sort(key=lambda p: p.date, reverse=True)
# index
# print("rendering index.html")
index_html = env.get_template('pages/index.html').render(
posts=all_posts,
)
index_html_path = OUTPUT_DIR / 'index.html'
print("writing", index_html_path)
index_html_path.write_text(index_html)
# Make the RSS feed
rss_path = OUTPUT_DIR / "rss.xml"
print("writing", rss_path)
rss_path.write_text(
env.get_template(FEED_TEMPLATE_FILE).render(
posts=all_posts, date=formatdate()
)
)
if __name__ == "__main__":
main()