-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
215 lines (181 loc) · 7.3 KB
/
build.py
File metadata and controls
215 lines (181 loc) · 7.3 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import re, pathlib, datetime, markdown
import xml.etree.ElementTree as ET
# ---------- Paths ----------
ROOT = pathlib.Path(__file__).parent
POSTS_DIR = ROOT / "posts"
TPL_DIR = ROOT / "templates"
OUT_DIR = ROOT / "content"
HOME_DIR = OUT_DIR / "0000" # section homepages live here
SITE_URL = "https://lmpkessels.com"
# ---------- Templates ----------
POST_TPL = (TPL_DIR / "post.html").read_text(encoding="utf-8")
HOME_TPL = (TPL_DIR / "homepage.html").read_text(encoding="utf-8")
# ---------- Defaults ----------
DEFAULT_IMAGE = "/assets/default.png"
# ---------- Regex ----------
FM_RE = re.compile(r"^---\s*\n(.*?)\n---\s*\n(.*)$", re.S)
FNAME_RE = re.compile(r"^(?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2})-(?P<slug>.+)\.md$", re.I)
SECTION_LABELS = {
"math": "Mathematics",
"cryptography": "Cryptography",
"comp-sci": "Comp-sci",
"essays": "Essays",
"hq": "lkessels blog",
"thinking": "Thinking Vault",
"misc": "lkessels blog", # fallback
}
def label_for(sec: str) -> str:
return SECTION_LABELS.get(sec.lower(), sec.title())
# ---------- Section Titles (for <title> in <head>) ----------
SECTION_TITLES = {
"math": "Mathematics | lkessels blog",
"cryptography": "Cryptography | lkessels blog",
"comp-sci": "Comp-sci | lkessels blog",
"essays": "Essays | lkessels blog",
"thinking": "Thinking Vault | lkessels blog",
"hq": "lkessels Headquarters | lkessels blog",
"misc": "lkessels Misc | lkessels blog",
}
# ---------- Helpers ----------
def slugify(s: str) -> str:
s = s.lower().strip().replace(" ", "-")
s = re.sub(r"[^a-z0-9\-]", "", s)
return re.sub(r"-{2,}", "-", s)
def fmt_date_h(d: datetime.date) -> str:
return d.strftime("%Y %b %d") # → "2025 Apr 16"
def parse_front_matter(text: str):
m = FM_RE.match(text)
if not m:
return {}, text
raw, body = m.groups()
meta = {}
for line in raw.splitlines():
if ":" in line:
k, v = line.split(":", 1)
meta[k.strip().lower()] = v.strip()
return meta, body
def parse_title(md_text: str, fallback: str) -> str:
m = re.search(r"^\s*#\s+(.+)$", md_text, re.M)
return m.group(1).strip() if m else fallback
def dedupe(items):
seen, out = set(), []
for d, t, u in items:
if u not in seen:
seen.add(u)
out.append((d, t, u))
return out
def safe_alt(text: str, limit: int = 420) -> str:
# strip any tags and collapse whitespace
text = re.sub(r"<[^>]+>", "", text)
text = re.sub(r"\s+", " ", text).strip()
return text[:limit]
# ---------- Build ----------
def build():
OUT_DIR.mkdir(parents=True, exist_ok=True)
HOME_DIR.mkdir(parents=True, exist_ok=True)
all_posts, by_section, by_month = [], {}, {}
for md_path in sorted(POSTS_DIR.glob("*.md")):
raw = md_path.read_text(encoding="utf-8")
fm, body_md = parse_front_matter(raw)
# --- defaults from filename ---
m = FNAME_RE.match(md_path.name)
if m:
date = datetime.date(int(m["y"]), int(m["m"]), int(m["d"]))
slug = slugify(m["slug"])
else:
date = datetime.date.today()
slug = slugify(md_path.stem)
# --- front matter overrides ---
if "date" in fm:
try:
date = datetime.date.fromisoformat(fm["date"])
except ValueError:
pass
if "slug" in fm:
slug = slugify(fm["slug"])
section = (fm.get("section") or "misc").lower()
title = fm.get("title") or parse_title(body_md, md_path.stem)
desc = (fm.get("description") or "")[:160]
image = fm.get("image", DEFAULT_IMAGE)
alt = fm.get("alternative", "").strip()
if not alt:
alt = desc or re.sub(r"[-_]+", " ", slug)
alt = safe_alt(alt)
# html_title logic
if "html_title" in fm:
post_html_title = fm["html_title"]
else:
post_html_title = f"{title} | lkessels blog"
# convert Markdown
md = markdown.Markdown(extensions=["tables", "fenced_code"])
body_html = md.convert(body_md)
# --- output path: /content/YYYY/YYYY-MM/slug.html ---
year_dir = OUT_DIR / f"{date.year}"
month_code = f"{date.year}-{date.month:02d}"
month_dir = year_dir / month_code
month_dir.mkdir(parents=True, exist_ok=True)
post_file = month_dir / f"{slug}.html"
canonical = f"{SITE_URL}/content/{date.year}/{month_code}/{slug}.html"
html_out = (POST_TPL
.replace("$html_title$", post_html_title)
.replace("$title$", title)
.replace("$date$", fmt_date_h(date))
.replace("$body$", body_html)
.replace("$desc$", desc)
.replace("$canonical$", canonical)
.replace("$image$", image)
.replace("$alternative$", alt)
)
post_file.write_text(html_out, encoding="utf-8")
print("Built post:", post_file)
url_abs = f"/content/{date.year}/{month_code}/{slug}.html"
item = (date, title, url_abs)
all_posts.append(item)
by_section.setdefault(section, []).append(item)
by_month.setdefault((date.year, date.month), []).append(item)
# ----- Root homepage -----
all_posts.sort(key=lambda x: x[0], reverse=True)
all_posts = dedupe(all_posts)
all_items = [
f'<li class="post-li"><span class="post-date">{fmt_date_h(d)}</span> '
f'<h2 class="h2-home"><a href="{url}" class="content-header">{t}</a></h2></li>'
for d, t, url in all_posts
]
root_home = (HOME_TPL
.replace("$html_title$", "lkessels | blog") # <title> in <head>
.replace("$section$", "lkessels blog")
.replace("$posts$", "\n".join(all_items))
)
(ROOT / "index.html").write_text(root_home, encoding="utf-8")
# ----- Section pages (Math, etc) -----
for sec, posts in by_section.items():
posts.sort(key=lambda x: x[0], reverse=True)
posts = dedupe(posts)
items = [
f'<li class="post-li"><span class="post-date">{fmt_date_h(d)}</span> '
f'<h2 class="h2-home"><a href="{url}" class="content-header">{t}</a></h2></li>'
for d, t, url in posts
]
label = label_for(sec)
html = (HOME_TPL
.replace("$html_title$", SECTION_TITLES.get(sec, f"{label} | lkessels")) # <title>
.replace("$section$", label)
.replace("$posts$", "\n".join(items))
)
(HOME_DIR / f"{sec}.html").write_text(html, encoding="utf-8")
# ----- Sitemap ----- #
nsmap = {"custom": "https://lmpkessels.com/ns"}
urlset = ET.Element("urlset", {
"xmlns": "http://www.sitemaps.org/schemas/sitemap/0.9",
**{f"xmlns:{k}": v for k, v in nsmap.items()}
})
for d, t, url in all_posts:
url_el = ET.SubElement(urlset, "url")
ET.SubElement(url_el, "loc").text = f"{SITE_URL}{url}"
ET.SubElement(url_el, "lastmod").text = d.isoformat()
ET.SubElement(url_el, "{https://lukefi.com/ns}title").text = t
ET.SubElement(url_el, "{https://lukefi.com/ns}date").text = d.isoformat()
sitemap_xml = ET.tostring(urlset, encoding="utf-8", xml_declaration=True)
(ROOT / "sitemap.xml").write_bytes(sitemap_xml)
if __name__ == "__main__":
build()