-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
158 lines (124 loc) · 4.98 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
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
"""HengHuH.github.io 的构建脚本。"""
import os
from datetime import date
import yaml
from bs4 import BeautifulSoup as bs
from urllib import parse
root = os.path.dirname(os.path.abspath(__file__))
root_url = "https://henghuh.github.io"
class Page:
def __init__(self) -> None:
self.name = ""
self.addr = ""
self.url = ""
self.title = ""
self.content = ""
self.published = True
@staticmethod
def extract_meta_content(path):
meta = {}
content = ""
with open(path, "r", encoding="utf-8") as f:
fc = f.read()
fc = fc.strip()
if fc.startswith("---"):
fc = fc[3:]
idx = fc.find("---")
meta = yaml.safe_load(fc[:idx])
content = str.strip(fc[idx + 3 :])
else:
raise ValueError(f"{path} is not a valid page file, missing meta data")
return meta, content
def load(self, path):
assert os.path.exists(path), f"{path} not exists"
basename = os.path.basename(path)
self.name, _ = os.path.splitext(basename)
self.addr = self.name + ".html"
self.url = parse.urljoin(root_url, self.addr)
meta, self.content = self.extract_meta_content(path)
self.title = meta.get("title", self.name)
self.published = meta.get('published', True)
class Post(Page):
def __init__(self) -> None:
super(Post, self).__init__()
self.date = None
def load(self, path):
assert os.path.exists(path), f"{path} not exists"
basename = os.path.basename(path)
fname, _ = os.path.splitext(basename)
fns = fname.split("-")
assert len(fns) >= 4, f"{path} is not a valid post file"
self.name = "-".join(fns[3:])
self.addr = os.path.join(fns[0], fns[1], self.name + ".html")
self.url = parse.urljoin(root_url, self.addr)
meta, self.content = self.extract_meta_content(path)
self.date = meta.get('date', "-".join(fns[0:3]))
self.title = meta.get("title", self.name)
self.published = meta.get('published', True)
class Site:
def __init__(self, root) -> None:
self.root = root
self._posts = set()
self._pages = set()
def add_post(self, post):
self._posts.add(post)
@property
def posts(self):
return self._posts
def add_page(self, page):
self._pages.add(page)
@property
def pages(self):
return self._pages
class Builder:
def __init__(self, site) -> None:
self._site = site
with open(os.path.join(root, "page_template.html"), "r", encoding="utf-8") as f:
self.page_temp = f.read()
def fillin_content(self, content):
content = content.replace("{{date}}", str(date.today()))
alllinks = []
for post in sorted(self._site.posts, key=lambda x: (x.date), reverse=True):
alllinks.append(f'<a href="{post.addr}">{post.title}</a>')
content = content.replace("{{site.pages}}", "<br>\n".join(alllinks))
return content
def build(self):
os.makedirs(self._site.root, exist_ok=True)
for post in self._site.posts:
abspath = os.path.join(self._site.root, post.addr)
dirname = os.path.dirname(abspath)
os.makedirs(dirname, exist_ok=True)
with open(abspath, "w", encoding="utf-8") as f:
posthtml = self.page_temp.replace("{{page.title}}", post.title)
content = self.fillin_content(post.content)
content += f'<hr/><font size="-1">发布于 {post.date}</font><br>\nHeng - <a href="https://henghuh.github.io">https://henghuh.github.io</a>'
posthtml = posthtml.replace("{{page.content}}", content)
f.write(bs(posthtml, "html.parser").prettify())
print(f"build post: {post.addr} --- DONE")
for page in self._site.pages:
abspath = os.path.join(self._site.root, page.addr)
with open(abspath, "w", encoding="utf-8") as f:
pagecontent = self.fillin_content(page.content)
pagehtml = self.page_temp.replace("{{page.title}}", page.title)
pagehtml = pagehtml.replace("{{page.content}}", pagecontent)
f.write(bs(pagehtml, "html.parser").prettify())
print(f"build page: {page.addr} --- DONE")
if __name__ == "__main__":
site = Site(os.path.join(root, "build"))
for fname in os.listdir(os.path.join(root, "_posts")):
fpath = os.path.join(root, "_posts", fname)
post = Post()
post.load(fpath)
if post.published:
site.add_post(post)
for fname in os.listdir(root):
if not fname.endswith(".page"):
continue
fpath = os.path.join(root, fname)
page = Page()
page.load(fpath)
if page.published:
site.add_page(page)
builder = Builder(site)
builder.build()