/
ssg.py
150 lines (127 loc) · 4.81 KB
/
ssg.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
"""Minimal Python SSG
Does just enough to generate a static website
- Generate HTML from a folder of Markdown files
- Insert the files into a template so they can be found
"""
import glob
import os
import pathlib
import re
import shutil
import sys
from jinja2 import Environment, FileSystemLoader
import yaml
from markdown_it import MarkdownIt
from mdit_py_plugins.front_matter.index import front_matter_plugin
class SSGBlog:
"""GenerateBlog class contains the ingredients to transform the Markdown pages
into an HTML website. The class takes `inputs` and creates a website as an
`output`. The following structure defines the input and output conventions. These
conventions allow me to use the previous Jekyll structure without reformatting
my posts.
"""
# TODO: make this a config file
web_root = "docs" # if using Github pages
styles = "_styles"
templates = "_templates"
index_page = "index.html"
post_source = "_posts"
post_output = os.path.join(web_root, "posts")
blog_title = "My Blog"
def __init__(self, source_path: str):
self.source_path = source_path
pathlib.Path(self.web_root).mkdir(exist_ok=True)
pathlib.Path(self.post_output).mkdir(exist_ok=True)
@property
def post_list(self):
"""List of Markdown posts"""
posts_path = os.path.join(self.source_path, self.post_source)
posts = list(glob.glob(os.path.join(posts_path, "*.md")))
posts.sort(reverse=True)
return posts
def _create_index(self):
index_template = "index.html.j2"
index_list = []
for page in self.post_list:
page = BlogPost(page)
title = page.front_matter.get("title")
href = os.path.join("posts", str(page.html_filename))
index_list.append(
{"post_title": title, "post_link": href, "date": page.post_date}
)
env = Environment(loader=FileSystemLoader(self.templates))
template = env.get_template(index_template)
content = template.render(page_title=self.blog_title, post_list=index_list)
index_path = os.path.join(self.web_root, self.index_page)
with open(index_path, "w", encoding="utf-8") as f:
f.write(content)
def _create_posts(self):
post_template = "post.html.j2"
for page in self.post_list:
pg = BlogPost(page)
post_out_path = os.path.join(self.post_output, pg.html_filename)
post_title = pg.front_matter.get("title")
page_title = f"{self.blog_title}::{post_title}"
env = Environment(loader=FileSystemLoader(self.templates))
template = env.get_template(post_template)
content = template.render(
post_title=post_title,
body_content=pg.html,
page_title=page_title,
post_date=pg.post_date,
)
with open(post_out_path, "w", encoding="utf-8") as file:
file.write(content)
def _create_styles(self):
"""Process CSS files if they exist"""
styles_dir = os.path.join(self.web_root, "styles")
css_files = glob.glob(os.path.join(self.styles, "*.css"))
if css_files:
if not os.path.exists(styles_dir):
os.mkdir(styles_dir)
for file in css_files:
shutil.copy(file, styles_dir)
def generate(self):
"""Call all methods that create the website"""
self._create_index()
self._create_posts()
self._create_styles()
class BlogPost:
"""BlogPost class represent a single markdown post and its attributes"""
post_extension = ".html"
def __init__(self, markdown_post: str):
self.md_path = pathlib.Path(markdown_post)
self.html_filename = pathlib.Path(self.md_path.stem).with_suffix(
self.post_extension
)
self.post_text = self.md_path.read_text(encoding="utf-8")
self._md = (
MarkdownIt("commonmark", {"breaks": False, "html": True})
.use(front_matter_plugin)
.enable("table")
)
@property
def post_date(self) -> str:
date_re = r"(\d{4}-\d{2}-\d{2})"
result = re.match(date_re, self.md_path.stem)
if result:
return result.group()
return ""
@property
def front_matter(self):
"Frontmatter of post in YAML format"
tokens = self._md.parse(self.post_text)
for token in tokens:
if token.type == "front_matter":
fm = yaml.safe_load(token.content)
return fm
@property
def html(self):
"""HTML body of a BlogPost"""
return self._md.render(self.post_text)
def main():
blog = SSGBlog(".")
blog.generate()
return 0
if __name__ == "__main__":
sys.exit(main())