-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpublish.py
More file actions
172 lines (128 loc) · 4.68 KB
/
Copy pathpublish.py
File metadata and controls
172 lines (128 loc) · 4.68 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Blog publisher: reads all posts/*.md (with YAML frontmatter), renders HTML for each
post, regenerates index.html, and regenerates feed.rss.
Usage:
python publish.py # build everything
python publish.py <post.md> # build a single post only (no index/feed update)
Each post must have a YAML frontmatter block with at least:
---
title: "Post Title"
date: YYYY-MM-DD
---
"""
import os
import re
import sys
from email import utils
from datetime import datetime, date, timezone
import pystache
import pypandoc
import yaml
POSTS_DIR = "posts"
OUT_DIR = "out"
FM_RE = re.compile(r"\A---\s*\n(.*?)\n---\s*\n", re.DOTALL)
# ---------------------------------------------------------------------------
# Frontmatter parsing
# ---------------------------------------------------------------------------
def split_frontmatter(md):
m = FM_RE.match(md)
if not m:
return {}, md
meta = yaml.safe_load(m.group(1)) or {}
return meta, md[m.end():]
# ---------------------------------------------------------------------------
# Post helpers
# ---------------------------------------------------------------------------
def stem(filename):
"""'Some_Post.md' → 'Some_Post'"""
return os.path.basename(filename)[:-3]
def out_file(filename):
return stem(filename) + ".html"
def to_date(value):
"""Coerce a frontmatter date value to a Python date."""
if isinstance(value, date):
return value
if isinstance(value, str):
return date.fromisoformat(value)
raise ValueError(f"Cannot parse date: {value!r}")
def rfc2822(d):
"""Format a date as RFC 2822 for RSS pubDate."""
dt = datetime(d.year, d.month, d.day, tzinfo=timezone.utc)
return utils.format_datetime(dt)
def get_post(filename):
path = os.path.join(POSTS_DIR, filename)
with open(path) as f:
contents = f.read()
meta, body = split_frontmatter(contents)
post_date = to_date(meta["date"]) if "date" in meta else date.fromtimestamp(os.stat(path).st_mtime)
title = meta.get("title") or stem(filename).replace("_", " ")
return {
"filename": filename,
"out_file": out_file(filename),
"title": title,
"date": str(post_date), # YYYY-MM-DD, used in post template
"date_and_time": rfc2822(post_date), # RFC 2822, used in RSS
"post": pypandoc.convert_text(body, "html", format="md"),
"_date_obj": post_date,
}
def get_posts():
today = date.today()
filenames = [f for f in os.listdir(POSTS_DIR) if f.endswith(".md")]
posts = [get_post(f) for f in filenames]
posts = [p for p in posts if p["_date_obj"] <= today]
posts.sort(key=lambda p: p["_date_obj"], reverse=True)
return posts
# ---------------------------------------------------------------------------
# Rendering
# ---------------------------------------------------------------------------
def render_post(tpl, post):
html = pystache.render(tpl, post)
with open(os.path.join(OUT_DIR, post["out_file"]), "w+") as f:
f.write(html)
def render_index(tpl, posts):
html = pystache.render(tpl, {"posts": posts})
with open(os.path.join(OUT_DIR, "index.html"), "w+") as f:
f.write(html)
def render_feed(tpl, posts):
build_date = rfc2822(date.today())
xml = pystache.render(tpl, {"date": build_date, "posts": posts})
with open(os.path.join(OUT_DIR, "feed.rss"), "w+") as f:
f.write(xml)
# ---------------------------------------------------------------------------
# Entry points
# ---------------------------------------------------------------------------
def build_all():
os.makedirs(OUT_DIR, exist_ok=True)
posts = get_posts()
print(f"Building {len(posts)} posts into {OUT_DIR}/...")
with open("layout.html") as f:
post_tpl = f.read()
for post in posts:
render_post(post_tpl, post)
with open("index_layout.html") as f:
index_tpl = f.read()
render_index(index_tpl, posts)
print(" index.html written")
with open("feed_tpl.rss") as f:
feed_tpl = f.read()
render_feed(feed_tpl, posts)
print(" feed.rss written")
def build_one(target):
"""Render a single post file (path or bare filename inside posts/)."""
os.makedirs(OUT_DIR, exist_ok=True)
filename = os.path.basename(target)
if not filename.endswith(".md"):
filename += ".md"
post = get_post(filename)
with open("layout.html") as f:
tpl = f.read()
render_post(tpl, post)
print(f" {OUT_DIR}/{post['out_file']} written")
if __name__ == "__main__":
if len(sys.argv) == 1:
build_all()
elif len(sys.argv) == 2:
build_one(sys.argv[1])
else:
print(__doc__)
sys.exit(1)