Commit 8c8c09b13e
Unsigned
Layout: unified · split
build.py +23 −21
| @@ -7,6 +7,7 @@ import sys | ||
| 7 | 7 | from pathlib import Path |
| 8 | 8 | from datetime import datetime |
| 9 | 9 | |
| 10 | ||
| 10 | 11 | def update_index_html(html_snippet, template_path="./theme/templates/index.html"): |
| 11 | 12 | """ |
| 12 | 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 | 32 | # Include the newline after BEGIN_POSTS |
| 32 | 33 | insert_start = begin_index + len(begin_marker) |
| 33 | 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 | 36 | insert_start += 1 |
| 36 | 37 | |
| 37 | 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 | 60 | end_line_start = end_index |
| 60 | 61 | |
| 61 | 62 | # Construct the new content |
| 62 | new_content = ( | |
| 63 | content[:insert_start] | |
| 64 | + indented_snippet | |
| 65 | + content[end_line_start:] | |
| 66 | ) | |
| 63 | new_content = content[:insert_start] + indented_snippet + content[end_line_start:] | |
| 67 | 64 | |
| 68 | 65 | # Write back to index.html |
| 69 | 66 | with open(template_path, "w", encoding="utf-8") as f: |
| 70 | 67 | f.write(new_content) |
| 71 | 68 | |
| 69 | ||
| 72 | 70 | def get_recent_posts_html(content_dir="./content/blog", num_posts=3): |
| 73 | 71 | """ |
| 74 | 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 | 84 | posts = [] |
| 87 | 85 | |
| 88 | 86 | header_patterns = { |
| 89 | "title": re.compile(r'^#\+title:\s*(.+)$', re.IGNORECASE), | |
| 90 | "date": re.compile(r'^#\+date:\s*<(\d{4}-\d{2}-\d{2})'), | |
| 91 | "slug": re.compile(r'^#\+slug:\s*(.+)$', re.IGNORECASE), | |
| 92 | "filetags": re.compile(r'^#\+filetags:\s*(.+)$', re.IGNORECASE), | |
| 87 | "title": re.compile(r"^#\+title:\s*(.+)$", re.IGNORECASE), | |
| 88 | "date": re.compile(r"^#\+date:\s*<(\d{4}-\d{2}-\d{2})"), | |
| 89 | "slug": re.compile(r"^#\+slug:\s*(.+)$", re.IGNORECASE), | |
| 90 | "filetags": re.compile(r"^#\+filetags:\s*(.+)$", re.IGNORECASE), | |
| 93 | 91 | } |
| 94 | 92 | |
| 95 | 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 | 137 | # Skip files with invalid date format |
| 140 | 138 | continue |
| 141 | 139 | |
| 142 | posts.append({ | |
| 143 | "title": title, | |
| 144 | "date_str": date_str, | |
| 145 | "date_obj": date_obj, | |
| 146 | "slug": slug, | |
| 147 | "tags": tags, | |
| 148 | }) | |
| 140 | posts.append( | |
| 141 | { | |
| 142 | "title": title, | |
| 143 | "date_str": date_str, | |
| 144 | "date_obj": date_obj, | |
| 145 | "slug": slug, | |
| 146 | "tags": tags, | |
| 147 | } | |
| 148 | ) | |
| 149 | 149 | |
| 150 | 150 | # Sort posts by date (newest first) |
| 151 | 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 | 157 | lines = [] |
| 158 | 158 | for post in recent: |
| 159 | 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 | 163 | lines.append('\t\t<div class="post-content">') |
| 162 | 164 | lines.append(f'\t\t\t<a href="/blog/{post["slug"]}.html">{post["title"]}</a>') |
| 163 | 165 | if post["tags"]: |
| 164 | 166 | lines.append('\t\t\t<div class="post-tags">') |
| 165 | 167 | for tag in post["tags"]: |
| 166 | 168 | lines.append(f'\t\t\t\t<span class="tag">{tag}</span>') |
| 167 | lines.append('\t\t\t</div>') | |
| 168 | lines.append('\t\t</div>') | |
| 169 | lines.append('\t</div>') | |
| 169 | lines.append("\t\t\t</div>") | |
| 170 | lines.append("\t\t</div>") | |
| 171 | lines.append("\t</div>") | |
| 170 | 172 | |
| 171 | 173 | return "\n".join(lines) |
| 172 | 174 | |