Commit 8c8c09b13e
Unsigned
Layout: unified · split
build.py +23 −21
| @@ -7,6 +7,7 @@ import sys | |||
| 7 | from pathlib import Path | 7 | from pathlib import Path |
| 8 | from datetime import datetime | 8 | from datetime import datetime |
| 9 | 9 | ||
| 10 | |||
| 10 | def update_index_html(html_snippet, template_path="./theme/templates/index.html"): | 11 | def update_index_html(html_snippet, template_path="./theme/templates/index.html"): |
| 11 | """ | 12 | """ |
| 12 | Read the index.html file at `template_path`, replace everything between | 13 | Read the index.html file at `template_path`, replace everything between |
| @@ -31,7 +32,7 @@ def update_index_html(html_snippet, template_path="./theme/templates/index.html" | |||
| 31 | # Include the newline after BEGIN_POSTS | 32 | # Include the newline after BEGIN_POSTS |
| 32 | insert_start = begin_index + len(begin_marker) | 33 | insert_start = begin_index + len(begin_marker) |
| 33 | # Ensure we capture the newline character if present | 34 | # Ensure we capture the newline character if present |
| 34 | if content[insert_start:insert_start+1] == "\n": | 35 | if content[insert_start : insert_start + 1] == "\n": |
| 35 | insert_start += 1 | 36 | insert_start += 1 |
| 36 | 37 | ||
| 37 | # If there is a newline before END_POSTS, trim trailing whitespace from snippet block | 38 | # If there is a newline before END_POSTS, trim trailing whitespace from snippet block |
| @@ -59,16 +60,13 @@ def update_index_html(html_snippet, template_path="./theme/templates/index.html" | |||
| 59 | end_line_start = end_index | 60 | end_line_start = end_index |
| 60 | 61 | ||
| 61 | # Construct the new content | 62 | # Construct the new content |
| 62 | new_content = ( | 63 | new_content = content[:insert_start] + indented_snippet + content[end_line_start:] |
| 63 | content[:insert_start] | ||
| 64 | + indented_snippet | ||
| 65 | + content[end_line_start:] | ||
| 66 | ) | ||
| 67 | 64 | ||
| 68 | # Write back to index.html | 65 | # Write back to index.html |
| 69 | with open(template_path, "w", encoding="utf-8") as f: | 66 | with open(template_path, "w", encoding="utf-8") as f: |
| 70 | f.write(new_content) | 67 | f.write(new_content) |
| 71 | 68 | ||
| 69 | |||
| 72 | def get_recent_posts_html(content_dir="./content/blog", num_posts=3): | 70 | def get_recent_posts_html(content_dir="./content/blog", num_posts=3): |
| 73 | """ | 71 | """ |
| 74 | Scan the `content_dir` for .org blog post files, extract their headers, | 72 | Scan the `content_dir` for .org blog post files, extract their headers, |
| @@ -86,10 +84,10 @@ def get_recent_posts_html(content_dir="./content/blog", num_posts=3): | |||
| 86 | posts = [] | 84 | posts = [] |
| 87 | 85 | ||
| 88 | header_patterns = { | 86 | header_patterns = { |
| 89 | "title": re.compile(r'^#\+title:\s*(.+)$', re.IGNORECASE), | 87 | "title": re.compile(r"^#\+title:\s*(.+)$", re.IGNORECASE), |
| 90 | "date": re.compile(r'^#\+date:\s*<(\d{4}-\d{2}-\d{2})'), | 88 | "date": re.compile(r"^#\+date:\s*<(\d{4}-\d{2}-\d{2})"), |
| 91 | "slug": re.compile(r'^#\+slug:\s*(.+)$', re.IGNORECASE), | 89 | "slug": re.compile(r"^#\+slug:\s*(.+)$", re.IGNORECASE), |
| 92 | "filetags": re.compile(r'^#\+filetags:\s*(.+)$', re.IGNORECASE), | 90 | "filetags": re.compile(r"^#\+filetags:\s*(.+)$", re.IGNORECASE), |
| 93 | } | 91 | } |
| 94 | 92 | ||
| 95 | for org_path in Path(content_dir).glob("*.org"): | 93 | for org_path in Path(content_dir).glob("*.org"): |
| @@ -139,13 +137,15 @@ def get_recent_posts_html(content_dir="./content/blog", num_posts=3): | |||
| 139 | # Skip files with invalid date format | 137 | # Skip files with invalid date format |
| 140 | continue | 138 | continue |
| 141 | 139 | ||
| 142 | posts.append({ | 140 | posts.append( |
| 143 | "title": title, | 141 | { |
| 144 | "date_str": date_str, | 142 | "title": title, |
| 145 | "date_obj": date_obj, | 143 | "date_str": date_str, |
| 146 | "slug": slug, | 144 | "date_obj": date_obj, |
| 147 | "tags": tags, | 145 | "slug": slug, |
| 148 | }) | 146 | "tags": tags, |
| 147 | } | ||
| 148 | ) | ||
| 149 | 149 | ||
| 150 | # Sort posts by date (newest first) | 150 | # Sort posts by date (newest first) |
| 151 | posts.sort(key=lambda x: x["date_obj"], reverse=True) | 151 | posts.sort(key=lambda x: x["date_obj"], reverse=True) |
| @@ -157,16 +157,18 @@ def get_recent_posts_html(content_dir="./content/blog", num_posts=3): | |||
| 157 | lines = [] | 157 | lines = [] |
| 158 | for post in recent: | 158 | for post in recent: |
| 159 | lines.append('\t<div class="post">') | 159 | lines.append('\t<div class="post">') |
| 160 | lines.append(f'\t\t<time datetime="{post["date_str"]}">{post["date_str"]}</time>') | 160 | lines.append( |
| 161 | f'\t\t<time datetime="{post["date_str"]}">{post["date_str"]}</time>' | ||
| 162 | ) | ||
| 161 | lines.append('\t\t<div class="post-content">') | 163 | lines.append('\t\t<div class="post-content">') |
| 162 | lines.append(f'\t\t\t<a href="/blog/{post["slug"]}.html">{post["title"]}</a>') | 164 | lines.append(f'\t\t\t<a href="/blog/{post["slug"]}.html">{post["title"]}</a>') |
| 163 | if post["tags"]: | 165 | if post["tags"]: |
| 164 | lines.append('\t\t\t<div class="post-tags">') | 166 | lines.append('\t\t\t<div class="post-tags">') |
| 165 | for tag in post["tags"]: | 167 | for tag in post["tags"]: |
| 166 | lines.append(f'\t\t\t\t<span class="tag">{tag}</span>') | 168 | lines.append(f'\t\t\t\t<span class="tag">{tag}</span>') |
| 167 | lines.append('\t\t\t</div>') | 169 | lines.append("\t\t\t</div>") |
| 168 | lines.append('\t\t</div>') | 170 | lines.append("\t\t</div>") |
| 169 | lines.append('\t</div>') | 171 | lines.append("\t</div>") |
| 170 | 172 | ||
| 171 | return "\n".join(lines) | 173 | return "\n".join(lines) |
| 172 | 174 | ||