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
2929import shutil
3030import subprocess
3131import sys
32from urllib.parse import quote
3233from datetime import datetime
3334from pathlib import Path
3435
@@ -267,6 +268,45 @@ def run_emacs_publish(dev_mode=True):
267268 sys.exit(1)
268269
269270
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
270310def deploy_to_server(build_dir, server):
271311 remote_path = f"{server}:/var/www/cleberg.net/"
272312 print(f"Deploying .build/ → {remote_path}")
@@ -329,6 +369,9 @@ def main():
329369 # Update index page with latest posts
330370 update_index_html(html_snippet)
331371
372 # Generate sitemap
373 generate_sitemap()
374
332375 # Deploy changes
333376 deploy_to_server(build_dir, homelab_server)
334377
@@ -347,6 +390,9 @@ def main():
347390 # Update index page with latest posts
348391 update_index_html(html_snippet)
349392
393 # Generate sitemap
394 generate_sitemap()
395
350396 # Launch development web server
351397 start_dev_server(build_dir)
352398