| @@ -86,9 +86,7 @@ def update_index_html(html_snippet, template_path="./.build/index.html"): |
| 86 | end_line_start = end_index |
86 | end_line_start = end_index |
| 87 | |
87 | |
| 88 | # Construct the new content |
88 | # Construct the new content |
| 89 | new_content = ( |
89 | new_content = content[:insert_start] + indented_snippet + content[end_line_start:] |
| 90 | content[:insert_start] + indented_snippet + content[end_line_start:] |
| |
| 91 | ) |
| |
| 92 | |
90 | |
| 93 | # Write back to index.html |
91 | # Write back to index.html |
| 94 | with open(template_path, "w", encoding="utf-8") as f: |
92 | with open(template_path, "w", encoding="utf-8") as f: |
| @@ -190,9 +188,7 @@ def get_recent_posts_html(content_dir="./content/blog", num_posts=3): |
| 190 | lines.append( |
188 | lines.append( |
| 191 | f'\t\t<time datetime="{post["date_str"]}">{post["date_full"]}</time>' |
189 | f'\t\t<time datetime="{post["date_str"]}">{post["date_full"]}</time>' |
| 192 | ) |
190 | ) |
| 193 | lines.append( |
191 | lines.append(f'\t\t\t<a href="/blog/{post["slug"]}.html">{post["title"]}</a>') |
| 194 | f'\t\t\t<a href="/blog/{post["slug"]}.html">{post["title"]}</a>' |
| |
| 195 | ) |
| |
| 196 | lines.append("\t</div>") |
192 | lines.append("\t</div>") |
| 197 | |
193 | |
| 198 | return "\n".join(lines) |
194 | return "\n".join(lines) |
| @@ -267,7 +263,9 @@ def run_emacs_publish(dev_mode=True): |
| 267 | if annoying_file.exists(): |
263 | if annoying_file.exists(): |
| 268 | os.remove(annoying_file) |
264 | os.remove(annoying_file) |
| 269 | else: |
265 | else: |
| 270 | print("Warning: .build/cleberg-net.html not found, but Emacs exited successfully.") |
266 | print( |
| |
267 | "Warning: .build/cleberg-net.html not found, but Emacs exited successfully." |
| |
268 | ) |
| 271 | |
269 | |
| 272 | |
270 | |
| 273 | def generate_sitemap(build_dir=".build", base_url="https://cleberg.net"): |
271 | def generate_sitemap(build_dir=".build", base_url="https://cleberg.net"): |
| @@ -290,9 +288,9 @@ def generate_sitemap(build_dir=".build", base_url="https://cleberg.net"): |
| 290 | loc = f"{base_url}{url_path}" |
288 | loc = f"{base_url}{url_path}" |
| 291 | |
289 | |
| 292 | # Last modified time |
290 | # Last modified time |
| 293 | lastmod = datetime.fromtimestamp( |
291 | lastmod = datetime.fromtimestamp(os.path.getmtime(full_path)).strftime( |
| 294 | os.path.getmtime(full_path) |
292 | "%Y-%m-%d" |
| 295 | ).strftime("%Y-%m-%d") |
293 | ) |
| 296 | |
294 | |
| 297 | sitemap_entries.append(f""" <url> |
295 | sitemap_entries.append(f""" <url> |
| 298 | <loc>{loc}</loc> |
296 | <loc>{loc}</loc> |
| @@ -308,9 +306,7 @@ def generate_sitemap(build_dir=".build", base_url="https://cleberg.net"): |
| 308 | sitemap_path = os.path.join(build_dir, "sitemap.xml") |
306 | sitemap_path = os.path.join(build_dir, "sitemap.xml") |
| 309 | with open(sitemap_path, "w", encoding="utf-8") as f: |
307 | with open(sitemap_path, "w", encoding="utf-8") as f: |
| 310 | f.write(sitemap_xml) |
308 | f.write(sitemap_xml) |
| 311 | print( |
309 | print(f"Sitemap generated at {sitemap_path} with {len(sitemap_entries)} entries.") |
| 312 | f"Sitemap generated at {sitemap_path} with {len(sitemap_entries)} entries." |
| |
| 313 | ) |
| |
| 314 | |
310 | |
| 315 | |
311 | |
| 316 | def deploy_to_server(build_dir, server): |
312 | def deploy_to_server(build_dir, server): |
| @@ -333,9 +329,7 @@ def start_dev_server(build_dir): |
| 333 | os.chdir(build_dir) |
329 | os.chdir(build_dir) |
| 334 | # This will run until interrupted (Ctrl+C) |
330 | # This will run until interrupted (Ctrl+C) |
| 335 | try: |
331 | try: |
| 336 | subprocess.run( |
332 | subprocess.run([sys.executable, "-m", "http.server", "8000"], check=True) |
| 337 | [sys.executable, "-m", "http.server", "8000"], check=True |
| |
| 338 | ) |
| |
| 339 | except KeyboardInterrupt: |
333 | except KeyboardInterrupt: |
| 340 | print("\nDevelopment server stopped.") |
334 | print("\nDevelopment server stopped.") |
| 341 | except subprocess.CalledProcessError as e: |
335 | except subprocess.CalledProcessError as e: |
| @@ -352,66 +346,57 @@ def main(): |
| 352 | css_min = theme_dir / "styles.min.css" |
346 | css_min = theme_dir / "styles.min.css" |
| 353 | |
347 | |
| 354 | env = os.environ.get("ENV", "").casefold() |
348 | env = os.environ.get("ENV", "").casefold() |
| 355 | skip_deploy = os.environ.get("SKIP_DEPLOY", "").casefold() == "true" |
349 | deploy = os.environ.get("DEPLOY", "").casefold() == "true" |
| 356 | |
| |
| 357 | if env == "prod": |
| |
| 358 | print("Environment: Production") |
| |
| 359 | method = prompt("Publishing on remote or LAN? [r|l] ").lower() |
| |
| 360 | if method == "r": |
| |
| 361 | homelab_server = "homelab-remote" |
| |
| 362 | elif method == "l": |
| |
| 363 | homelab_server = "homelab" |
| |
| 364 | else: |
| |
| 365 | print("Invalid input. Assuming LAN (homelab)") |
| |
| 366 | homelab_server = "homelab" |
| |
| 367 | |
350 | |
| 368 | # Remove previous build |
351 | if deploy: |
| 369 | remove_build_directory(build_dir) |
352 | print("Deploying to production...") |
| |
353 | deploy_to_server(build_dir, "homelab-remote") |
| |
354 | return |
| |
355 | else: |
| |
356 | if env == "prod": |
| |
357 | print("Environment: Production") |
| 370 | |
358 | |
| 371 | # Minify CSS |
359 | # Remove previous build |
| 372 | minify_css(css_src, css_min) |
360 | remove_build_directory(build_dir) |
| 373 | |
361 | |
| 374 | # Run publishing script (silenced output) |
362 | # Minify CSS |
| 375 | run_emacs_publish(dev_mode=False) |
363 | minify_css(css_src, css_min) |
| 376 | |
364 | |
| 377 | # Update index page with latest posts |
365 | # Run publishing script (silenced output) |
| 378 | update_index_html(html_snippet) |
366 | run_emacs_publish(dev_mode=False) |
| 379 | |
367 | |
| 380 | # Minify index page |
368 | # Update index page with latest posts |
| 381 | minify_html("./.build/index.html", "./.build/index.html") |
369 | update_index_html(html_snippet) |
| 382 | |
370 | |
| 383 | # Generate sitemap |
371 | # Minify index page |
| 384 | generate_sitemap() |
372 | minify_html("./.build/index.html", "./.build/index.html") |
| 385 | |
373 | |
| 386 | # Deploy changes |
374 | # Generate sitemap |
| 387 | if not skip_deploy: |
375 | generate_sitemap() |
| 388 | deploy_to_server(build_dir, homelab_server) |
| |
| 389 | else: |
| |
| 390 | print("SKIP_DEPLOY is true. Skipping rsync step.") |
| |
| 391 | |
376 | |
| 392 | else: |
377 | else: |
| 393 | print("Environment: Development") |
378 | print("Environment: Development") |
| 394 | |
379 | |
| 395 | # Remove previous build |
380 | # Remove previous build |
| 396 | remove_build_directory(build_dir) |
381 | remove_build_directory(build_dir) |
| 397 | |
382 | |
| 398 | # Minify CSS |
383 | # Minify CSS |
| 399 | minify_css(css_src, css_min) |
384 | minify_css(css_src, css_min) |
| 400 | |
385 | |
| 401 | # Run publishing script (with console output) |
386 | # Run publishing script (with console output) |
| 402 | run_emacs_publish(dev_mode=True) |
387 | run_emacs_publish(dev_mode=True) |
| 403 | |
388 | |
| 404 | # Update index page with latest posts |
389 | # Update index page with latest posts |
| 405 | update_index_html(html_snippet) |
390 | update_index_html(html_snippet) |
| 406 | |
391 | |
| 407 | # Minify index page |
392 | # Minify index page |
| 408 | minify_html("./.build/index.html", "./.build/index.html") |
393 | minify_html("./.build/index.html", "./.build/index.html") |
| 409 | |
394 | |
| 410 | # Generate sitemap |
395 | # Generate sitemap |
| 411 | generate_sitemap() |
396 | generate_sitemap() |
| 412 | |
397 | |
| 413 | # Launch development web server |
398 | # Launch development web server |
| 414 | start_dev_server(build_dir) |
399 | start_dev_server(build_dir) |
| 415 | |
400 | |
| 416 | |
401 | |
| 417 | if __name__ == "__main__": |
402 | if __name__ == "__main__": |