-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
502 lines (416 loc) · 16.2 KB
/
build.py
File metadata and controls
502 lines (416 loc) · 16.2 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/env uv --script
# /// script
# dependencies = [
# "pyyaml",
# "jinja2",
# "requests",
# "beautifulsoup4",
# "markdown2",
# ]
# ///
from pathlib import Path
import os
import shutil
import xml.etree.cElementTree as ET
from datetime import datetime
import subprocess
from copy import deepcopy as copy
import yaml
from jinja2 import Environment, FileSystemLoader
import requests
from bs4 import BeautifulSoup
from markdown2 import Markdown
config = yaml.safe_load(Path("config.yaml").open().read())
template_dir = Path(config["template_dir"])
content_dir = Path(config["content_dir"])
build_dir = Path(config["build_dir"])
build_dir.mkdir(exist_ok=True, parents=True)
def main():
build_all_pages()
make_sitemap()
make_robots_txt()
def build_all_pages():
environment = Environment(loader=FileSystemLoader(template_dir))
additionals = {"index": ["news"]}
for page in config["pages"]:
if page == "manual":
build_lab_manual()
continue
elif page == "posts":
build_posts()
continue
content_file = content_dir / f"{page}.yaml"
content = yaml.safe_load(content_file.open().read())[page]
page_file = build_dir / config["pages"][page]["file"]
page_file.parent.mkdir(exist_ok=True, parents=True)
page_template = environment.from_string(
(template_dir / config["pages"][page]["template"]).open().read()
)
add = {}
if page in additionals:
add = {
k: yaml.safe_load((content_dir / f"{k}.yaml").open().read())[k][k]
for k in additionals[page]
}
html = page_template.render(
page_url=config["deploy_url"] + config["pages"][page]["url"],
**config,
**content,
**add,
)
with page_file.open("w") as f:
f.write(html)
# if local, copy assets folder to build
if not os.getenv("GITHUB_ACTIONS"):
if (build_dir / "assets").exists():
shutil.rmtree(build_dir / "assets")
shutil.copytree("assets", build_dir / "assets")
def build_posts():
"""Build blog-post-like pages from markdown files in content/posts/."""
posts_dir = content_dir / "posts"
if not posts_dir.exists():
return
environment = Environment(loader=FileSystemLoader(template_dir))
template = environment.from_string((template_dir / "post.html").open().read())
md = Markdown(
extras=[
"fenced-code-blocks",
"highlightjs-lang",
"metadata",
"tables",
"footnotes",
]
)
posts_metadata = [] # Collect metadata for index
for post_file in posts_dir.glob("*.md"):
slug = post_file.stem
raw_content = post_file.read_text()
html_content = md.convert(raw_content)
metadata = getattr(md, "metadata", {}) or {}
page_dir = build_dir / "p" / slug
page_dir.mkdir(exist_ok=True, parents=True)
page_file = page_dir / "index.html"
page_url = f"/p/{slug}/"
# Collect for index
posts_metadata.append(
{
"slug": slug,
"url": page_url,
"title": metadata.get("title", slug.replace("-", " ").title()),
"subtitle": metadata.get("subtitle"),
"date": metadata.get("date"),
}
)
html = template.render(
page_url=config["deploy_url"] + page_url.lstrip("/"),
title=metadata.get("title", slug.replace("-", " ").title()),
subtitle=metadata.get("subtitle"),
date=metadata.get("date"),
content=html_content,
**config,
)
with page_file.open("w") as f:
f.write(html)
# Build index after all posts (even if none)
build_posts_index(posts_metadata, environment)
def build_posts_index(posts_metadata: list[dict], environment: Environment):
"""Build an index page listing all posts."""
template = environment.from_string(
(template_dir / "posts_index.html").open().read()
)
page_dir = build_dir / "p"
page_dir.mkdir(exist_ok=True, parents=True)
html = template.render(
page_url=config["deploy_url"] + "p/",
title="Posts",
posts=sorted(posts_metadata, key=lambda x: x.get("date") or "", reverse=True),
**config,
)
with (page_dir / "index.html").open("w") as f:
f.write(html)
def build_lab_manual():
name = config["pages"]["manual"]["url"][1:-1]
environment = Environment(loader=FileSystemLoader(template_dir))
template = environment.from_string(
(template_dir / config["pages"]["manual"]["template"]).open().read()
)
manual_root_url = config["pages"]["manual"]["url"]
req = requests.get(
f"https://raw.githubusercontent.com/{config['manual_repo']}/refs/heads/main/Makefile"
)
page_order = [
p.split(".md")[0]
for p in req.content.decode().split("\n")
if p.startswith("source/") or p.endswith(".md \\")
]
pages = dict()
for page in page_order:
if page == "README":
page_slug = "index"
page_file = build_dir / name / "index.html"
page_url = f"/{name}/"
else:
page_slug = page.replace("source/", "").lower()
page_file = build_dir / name / page_slug / "index.html"
page_url = f"/{name}/{page_slug}/"
page_file.parent.mkdir(exist_ok=True, parents=True)
req = requests.get(
f"https://raw.githubusercontent.com/{config['manual_repo']}/refs/heads/main/{page}.md"
)
html = Markdown(
extras=[
"fenced-code-blocks",
"highlightjs-lang",
"task_list",
"admonitions",
"tables",
]
).convert(req.content.decode())
body = BeautifulSoup(html, "html.parser")
page_title = body.find("h1").text
if page_slug == "index":
body.find("h1").decompose()
# Replace internal markdown links
links = body.find_all("a")
for link in links:
href = link.get("href")
if href.endswith(".md"):
link["href"] = f"{manual_root_url}{href.replace('.md', '')}"
link = (
f"https://github.com/{config['manual_repo']}/edit/refs/heads/main/{page}.md"
)
msg = f"<hr><p><small>Edit this page on <a href='{link}'>GitHub</a></small></p>"
snippet = BeautifulSoup(msg, "html.parser").extract()
body.append(snippet)
pages[page_slug] = dict(
page_url=page_url,
page_slug=page_slug,
page_title=page_title,
file=page_file,
page_content=str(body),
active=False,
)
for page_slug, data in pages.items():
manual_pages = copy(pages)
manual_pages[page_slug]["active"] = True
html = template.render(
page_url=data["page_url"],
page_title=data["page_title"],
page_content=data["page_content"],
manual_pages=manual_pages,
manual_root_url=manual_root_url,
**config,
)
with data["file"].open("w") as f:
f.write(html)
def get_last_mod_date() -> dict[str, str]:
now = datetime.now().strftime("%Y-%m-%d")
try:
subprocess.run(
["git", "rev-parse", "--is-inside-work-tree"],
check=True,
capture_output=True,
)
except subprocess.CalledProcessError:
print("Not a git repository. Using current date as last modification date.")
return {page.stem: now for page in content_dir.glob("*.yaml")}
last_mod_dates = {}
# Content YAML files
for page in content_dir.glob("*.yaml"):
page_name = page.stem
try:
result = subprocess.run(
["git", "log", "-n", "1", "--format=%ci", "--", page],
check=True,
capture_output=True,
text=True,
)
date_str = result.stdout.strip()
if not date_str:
continue
date_obj = datetime.fromisoformat(
date_str.replace("Z", "+00:00")
.replace(" +0100", " +01:00")
.replace(" +0200", " +02:00")
)
last_mod_dates[page_name] = date_obj.strftime("%Y-%m-%d")
except subprocess.CalledProcessError as e:
print(f"Error getting git log for {page}: {e}")
last_mod_dates[page_name] = now
# Posts (markdown files)
posts_dir = content_dir / "posts"
if posts_dir.exists():
latest_post_date = None
for post in posts_dir.glob("*.md"):
post_key = f"post:{post.stem}" # e.g., "post:my-first-post"
try:
result = subprocess.run(
["git", "log", "-n", "1", "--format=%ci", "--", post],
check=True,
capture_output=True,
text=True,
)
date_str = result.stdout.strip()
if not date_str:
continue
date_obj = datetime.fromisoformat(
date_str.replace("Z", "+00:00")
.replace(" +0100", " +01:00")
.replace(" +0200", " +02:00")
)
formatted_date = date_obj.strftime("%Y-%m-%d")
last_mod_dates[post_key] = formatted_date
# Track latest post date for posts index
if latest_post_date is None or date_obj > latest_post_date:
latest_post_date = date_obj
except subprocess.CalledProcessError as e:
print(f"Error getting git log for {post}: {e}")
last_mod_dates[post_key] = now
# Posts index uses the date of the most recently modified post
if latest_post_date:
last_mod_dates["posts"] = latest_post_date.strftime("%Y-%m-%d")
return last_mod_dates
# def get_manual_mod_dates() -> dict[str, str]:
# """Get last modification dates for lab-manual pages via GitHub API."""
# now = datetime.now().strftime("%Y-%m-%d")
# mod_dates = {}
# repo = config["manual_repo"]
# api_base = f"https://api.github.com/repos/{repo}/commits"
# req = requests.get(
# f"https://api.github.com/repos/{repo}/contents/source",
# headers={"Accept": "application/vnd.github.v3+json"},
# )
# if req.status_code != 200:
# return {}
# for file_info in req.json():
# if not file_info["name"].endswith(".md"):
# continue
# page_name = file_info["name"].replace(".md", "")
# # Get last commit for this file
# commits_req = requests.get(
# api_base,
# params={"path": f"source/{file_info['name']}", "per_page": 1},
# headers={"Accept": "application/vnd.github.v3+json"},
# )
# if commits_req.status_code == 200 and commits_req.json():
# commit_date = commits_req.json()[0]["commit"]["committer"]["date"]
# date_obj = datetime.fromisoformat(commit_date.replace("Z", "+00:00"))
# mod_dates[f"lab-manual:{page_name}"] = date_obj.strftime("%Y-%m-%d")
# else:
# mod_dates[f"lab-manual:{page_name}"] = now
# return mod_dates
def get_manual_mod_dates() -> dict[str, str]:
"""Clone manual repo shallowly and get git log dates."""
import tempfile
now = datetime.now().strftime("%Y-%m-%d")
mod_dates = {}
repo = config["manual_repo"]
with tempfile.TemporaryDirectory() as tmpdir:
# Shallow clone with enough history to get meaningful dates
subprocess.run(
["git", "clone", "--depth", "50", f"https://github.com/{repo}.git", tmpdir],
check=True,
capture_output=True,
)
# Get page order from Makefile (same logic as build_manual)
makefile = Path(tmpdir) / "Makefile"
page_order = [
p.split(".md")[0]
for p in makefile.read_text().split("\n")
if p.startswith("source/") or p.endswith(".md \\")
]
latest_manual_date = None
for page in page_order:
if page == "README":
page_slug = "index"
file_path = "README.md"
else:
page_slug = page.replace("source/", "").lower()
file_path = f"{page}.md"
try:
result = subprocess.run(
[
"git",
"-C",
tmpdir,
"log",
"-n",
"1",
"--format=%ci",
"--",
file_path,
],
check=True,
capture_output=True,
text=True,
)
date_str = result.stdout.strip()
if not date_str:
mod_dates[f"lab-manual:{page_slug}"] = now
continue
date_obj = datetime.fromisoformat(
date_str.replace("Z", "+00:00")
.replace(" +0100", " +01:00")
.replace(" +0200", " +02:00")
)
formatted_date = date_obj.strftime("%Y-%m-%d")
mod_dates[f"lab-manual:{page_slug}"] = formatted_date
# Track latest for manual index
if latest_manual_date is None or date_obj > latest_manual_date:
latest_manual_date = date_obj
except subprocess.CalledProcessError as e:
print(f"Error getting git log for {file_path}: {e}")
mod_dates[f"lab-manual:{page_slug}"] = now
# Manual index uses the most recently modified page
if latest_manual_date:
mod_dates["lab-manual"] = latest_manual_date.strftime("%Y-%m-%d")
return mod_dates
def make_sitemap():
now = datetime.now().strftime("%Y-%m-%d")
mod_dates = get_last_mod_date() | get_manual_mod_dates()
# Build reverse mapping: build file path -> page key
file_to_page = {config["pages"][page]["file"]: page for page in config["pages"]}
sitemap = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
for page in build_dir.glob("**/*.html"):
relative_path = str(page.relative_to(build_dir))
page_key = file_to_page.get(relative_path)
# Handle posts
if (
page_key is None
and relative_path.startswith("p/")
and relative_path.count("/") == 2
):
post_slug = relative_path.split("/")[1]
page_key = f"post:{post_slug}"
# Handle manual pages
elif page_key is None and relative_path.startswith("lab-manual/"):
manual_page_slug = relative_path.replace("manual/", "").replace(
"/index.html", ""
)
if manual_page_slug == "index":
manual_page_slug = "index"
else:
manual_page_slug = manual_page_slug.replace("lab-", "")
page_key = f"lab-manual:{manual_page_slug}"
url = config["deploy_url"] + relative_path.replace("index.html", "")
url_element = ET.SubElement(sitemap, "url")
ET.SubElement(url_element, "loc").text = url
if page_key and page_key in mod_dates:
ET.SubElement(url_element, "lastmod").text = mod_dates[page_key]
else:
print(
f"No last modification date found for {relative_path}. Using current date."
)
ET.SubElement(url_element, "lastmod").text = now
tree = ET.ElementTree(sitemap)
ET.indent(tree, space="\t", level=0)
with open(build_dir / "sitemap.xml", "wb") as f:
tree.write(f, encoding="utf-8", xml_declaration=True, method="xml")
def make_robots_txt():
txt = f"Sitemap: {config['deploy_url']}sitemap.xml"
with open(build_dir / "robots.txt", "w") as f:
f.write(txt)
def clean_build_dir():
shutil.rmtree(build_dir)
if __name__ == "__main__":
main()