Commit faf7486902

faf748690276a16add3f039c4e957e1f1381cb86

parent: 6049be2866

Unsigned

cmc <hello@cleberg.net> · 2025-07-14 21:04 UTC
committer: <noreply@github.com>

add function to generate sitemap

Layout: unified · split

build.py +46
@@ -29,6 +29,7 @@ import re
29import shutil 29import shutil
30import subprocess 30import subprocess
31import sys 31import sys
32from urllib.parse import quote
32from datetime import datetime 33from datetime import datetime
33from pathlib import Path 34from pathlib import Path
34 35
@@ -267,6 +268,45 @@ def run_emacs_publish(dev_mode=True):
267 sys.exit(1) 268 sys.exit(1)
268 269
269 270
271def generate_sitemap(public_dir="public", base_url="https://cleberg.net"):
272 """
273 Generates a sitemap.xml based on contents of the public directory.
274 Only includes .html files (except 404.html).
275 """
276 sitemap_entries = []
277 for root, dirs, files in os.walk(public_dir):
278 for filename in files:
279 if filename.endswith(".html") and filename != "404.html":
280 full_path = os.path.join(root, filename)
281 rel_path = os.path.relpath(full_path, public_dir)
282 url_path = "/" + quote(rel_path.replace(os.sep, "/"))
283 # Remove index.html for cleaner URLs
284 if url_path.endswith('/index.html'):
285 url_path = url_path[:-10] or '/'
286 elif url_path == '/index.html':
287 url_path = '/'
288 loc = f"{base_url}{url_path}"
289
290 # Last modified time
291 lastmod = datetime.utcfromtimestamp(os.path.getmtime(full_path)).strftime("%Y-%m-%d")
292
293 sitemap_entries.append(f""" <url>
294 <loc>{loc}</loc>
295 <lastmod>{lastmod}</lastmod>
296 </url>""")
297
298 sitemap_xml = f"""<?xml version="1.0" encoding="UTF-8"?>
299<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
300{os.linesep.join(sitemap_entries)}
301</urlset>
302"""
303 # Write to public/sitemap.xml
304 sitemap_path = os.path.join(public_dir, "sitemap.xml")
305 with open(sitemap_path, "w", encoding="utf-8") as f:
306 f.write(sitemap_xml)
307 print(f"Sitemap generated at {sitemap_path} with {len(sitemap_entries)} entries.")
308
309
270def deploy_to_server(build_dir, server): 310def deploy_to_server(build_dir, server):
271 remote_path = f"{server}:/var/www/cleberg.net/" 311 remote_path = f"{server}:/var/www/cleberg.net/"
272 print(f"Deploying .build/ → {remote_path}") 312 print(f"Deploying .build/ → {remote_path}")
@@ -329,6 +369,9 @@ def main():
329 # Update index page with latest posts 369 # Update index page with latest posts
330 update_index_html(html_snippet) 370 update_index_html(html_snippet)
331 371
372 # Generate sitemap
373 generate_sitemap()
374
332 # Deploy changes 375 # Deploy changes
333 deploy_to_server(build_dir, homelab_server) 376 deploy_to_server(build_dir, homelab_server)
334 377
@@ -347,6 +390,9 @@ def main():
347 # Update index page with latest posts 390 # Update index page with latest posts
348 update_index_html(html_snippet) 391 update_index_html(html_snippet)
349 392
393 # Generate sitemap
394 generate_sitemap()
395
350 # Launch development web server 396 # Launch development web server
351 start_dev_server(build_dir) 397 start_dev_server(build_dir)
352 398