| @@ -1,11 +1,36 @@ |
| 1 | #!/usr/bin/env python3 |
1 | #!/usr/bin/env python3 |
| |
2 | """ |
| |
3 | This script automates the process of building, testing, and deploying the website. |
| |
4 | It handles tasks such as: |
| |
5 | |
| |
6 | - Removing and recreating the build directory. |
| |
7 | - Minifying CSS assets. |
| |
8 | - Running the Emacs publishing script to generate site content. |
| |
9 | - Updating the index.html file with the latest blog posts. |
| |
10 | - Optionally deploying the built site to a remote server. |
| |
11 | - Starting a local development server for previewing changes. |
| |
12 | |
| |
13 | Usage: |
| |
14 | Set the environment variable ENV to 'prod' for production builds. |
| |
15 | Run the script to perform the build process accordingly. |
| |
16 | |
| |
17 | Dependencies: |
| |
18 | - Python 3 |
| |
19 | - Emacs with the publish.el script |
| |
20 | - minify tool for CSS minification |
| |
21 | - rsync for deployment |
| |
22 | |
| |
23 | Author: |
| |
24 | Christian Cleberg <hello@cleberg.net> |
| |
25 | """ |
| |
26 | |
| 2 | import os |
27 | import os |
| 3 | import re |
28 | import re |
| 4 | import shutil |
29 | import shutil |
| 5 | import subprocess |
30 | import subprocess |
| 6 | import sys |
31 | import sys |
| 7 | from pathlib import Path |
| |
| 8 | from datetime import datetime |
32 | from datetime import datetime |
| |
33 | from pathlib import Path |
| 9 | |
34 | |
| 10 | |
35 | |
| 11 | def update_index_html(html_snippet, template_path="./.build/index.html"): |
36 | def update_index_html(html_snippet, template_path="./.build/index.html"): |
| @@ -60,7 +85,9 @@ def update_index_html(html_snippet, template_path="./.build/index.html"): |
| 60 | end_line_start = end_index |
85 | end_line_start = end_index |
| 61 | |
86 | |
| 62 | # Construct the new content |
87 | # Construct the new content |
| 63 | new_content = content[:insert_start] + indented_snippet + content[end_line_start:] |
88 | new_content = ( |
| |
89 | content[:insert_start] + indented_snippet + content[end_line_start:] |
| |
90 | ) |
| 64 | |
91 | |
| 65 | # Write back to index.html |
92 | # Write back to index.html |
| 66 | with open(template_path, "w", encoding="utf-8") as f: |
93 | with open(template_path, "w", encoding="utf-8") as f: |
| @@ -161,7 +188,9 @@ def get_recent_posts_html(content_dir="./content/blog", num_posts=3): |
| 161 | f'\t\t<time datetime="{post["date_str"]}">{post["date_str"]}</time>' |
188 | f'\t\t<time datetime="{post["date_str"]}">{post["date_str"]}</time>' |
| 162 | ) |
189 | ) |
| 163 | lines.append('\t\t<div class="post-content">') |
190 | lines.append('\t\t<div class="post-content">') |
| 164 | lines.append(f'\t\t\t<a href="/blog/{post["slug"]}.html">{post["title"]}</a>') |
191 | lines.append( |
| |
192 | f'\t\t\t<a href="/blog/{post["slug"]}.html">{post["title"]}</a>' |
| |
193 | ) |
| 165 | if post["tags"]: |
194 | if post["tags"]: |
| 166 | lines.append('\t\t\t<div class="post-tags">') |
195 | lines.append('\t\t\t<div class="post-tags">') |
| 167 | for tag in post["tags"]: |
196 | for tag in post["tags"]: |
| @@ -245,7 +274,9 @@ def start_dev_server(build_dir): |
| 245 | os.chdir(build_dir) |
274 | os.chdir(build_dir) |
| 246 | # This will run until interrupted (Ctrl+C) |
275 | # This will run until interrupted (Ctrl+C) |
| 247 | try: |
276 | try: |
| 248 | subprocess.run([sys.executable, "-m", "http.server", "8000"], check=True) |
277 | subprocess.run( |
| |
278 | [sys.executable, "-m", "http.server", "8000"], check=True |
| |
279 | ) |
| 249 | except KeyboardInterrupt: |
280 | except KeyboardInterrupt: |
| 250 | print("\nDevelopment server stopped.") |
281 | print("\nDevelopment server stopped.") |
| 251 | except subprocess.CalledProcessError as e: |
282 | except subprocess.CalledProcessError as e: |