1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
#!/usr/bin/env python3
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
from datetime import datetime
def update_index_html(html_snippet, template_path="./.build/index.html"):
"""
Read the index.html file at `template_path`, replace everything between
<!-- BEGIN_POSTS --> and <!-- END_POSTS --> with the provided html_snippet,
and write the updated content back to the same file.
"""
# Read the current contents of index.html
with open(template_path, "r", encoding="utf-8") as f:
content = f.read()
begin_marker = "<!-- BEGIN_POSTS -->"
end_marker = "<!-- END_POSTS -->"
# Find the indices of the markers
begin_index = content.find(begin_marker)
end_index = content.find(end_marker)
if begin_index == -1 or end_index == -1:
raise ValueError(f"Markers not found in {template_path}")
# Compute insertion points: after the end of begin_marker line, before end_marker
# Include the newline after BEGIN_POSTS
insert_start = begin_index + len(begin_marker)
# Ensure we capture the newline character if present
if content[insert_start : insert_start + 1] == "\n":
insert_start += 1
# If there is a newline before END_POSTS, trim trailing whitespace from snippet block
# We will preserve indentation of BEGIN_POSTS line
indent = ""
# Determine the indentation by looking at characters after the newline that follows begin_marker
lines_after_begin = content[begin_index:].splitlines(True)
if len(lines_after_begin) > 1:
# The second line starts with the indentation to preserve
second_line = lines_after_begin[1]
indent = ""
for ch in second_line:
if ch.isspace():
indent += ch
else:
break
# Prepare the replacement block: indent each line of html_snippet
snippet_lines = html_snippet.splitlines()
indented_snippet = "\n".join(indent + line for line in snippet_lines) + "\n"
# Compute the position just before end_marker (excluding any preceding whitespace/newline)
end_line_start = content.rfind("\n", 0, end_index)
if end_line_start == -1:
end_line_start = end_index
# Construct the new content
new_content = content[:insert_start] + indented_snippet + content[end_line_start:]
# Write back to index.html
with open(template_path, "w", encoding="utf-8") as f:
f.write(new_content)
def get_recent_posts_html(content_dir="./content/blog", num_posts=3):
"""
Scan the `content_dir` for .org blog post files, extract their headers,
and return an HTML snippet for the `num_posts` most recent posts.
Expects each .org file to contain headers of the form:
#+title: Post Title
#+date: <YYYY-MM-DD Day HH:MM:SS>
#+slug: post-slug
#+filetags: :tag1:tag2:tag3:
Returns a string containing the section with the three most recent posts,
formatted like the snippet in the prompt.
"""
posts = []
header_patterns = {
"title": re.compile(r"^#\+title:\s*(.+)$", re.IGNORECASE),
"date": re.compile(r"^#\+date:\s*<(\d{4}-\d{2}-\d{2})"),
"slug": re.compile(r"^#\+slug:\s*(.+)$", re.IGNORECASE),
"filetags": re.compile(r"^#\+filetags:\s*(.+)$", re.IGNORECASE),
}
for org_path in Path(content_dir).glob("*.org"):
title = None
date_str = None
slug = None
tags = []
with org_path.open("r", encoding="utf-8") as f:
for line in f:
if title is None:
m = header_patterns["title"].match(line)
if m:
title = m.group(1).strip()
continue
if date_str is None:
m = header_patterns["date"].match(line)
if m:
# date_str is just YYYY-MM-DD
date_str = m.group(1)
continue
if slug is None:
m = header_patterns["slug"].match(line)
if m:
slug = m.group(1).strip()
continue
if not tags:
m = header_patterns["filetags"].match(line)
if m:
# filetags are in the form ":tag1:tag2:tag3:"
raw = m.group(1).strip()
# split on ":" and filter out empty strings
tags = [t for t in raw.split(":") if t]
continue
# Stop scanning once we have all required fields
if title and date_str and slug and tags:
break
if title and date_str and slug:
try:
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
except ValueError:
# Skip files with invalid date format
continue
posts.append(
{
"title": title,
"date_str": date_str,
"date_obj": date_obj,
"slug": slug,
"tags": tags,
}
)
# Sort posts by date (newest first)
posts.sort(key=lambda x: x["date_obj"], reverse=True)
# Take the top `num_posts`
recent = posts[:num_posts]
# Build HTML lines
lines = []
for post in recent:
lines.append('\t<div class="post">')
lines.append(
f'\t\t<time datetime="{post["date_str"]}">{post["date_str"]}</time>'
)
lines.append('\t\t<div class="post-content">')
lines.append(f'\t\t\t<a href="/blog/{post["slug"]}.html">{post["title"]}</a>')
if post["tags"]:
lines.append('\t\t\t<div class="post-tags">')
for tag in post["tags"]:
lines.append(f'\t\t\t\t<span class="tag">{tag}</span>')
lines.append("\t\t\t</div>")
lines.append("\t\t</div>")
lines.append("\t</div>")
return "\n".join(lines)
def prompt(prompt_text):
try:
return input(prompt_text).strip()
except EOFError:
return ""
def remove_build_directory(build_dir):
if build_dir.exists():
print(f"Removing previous build directory: {build_dir}/")
shutil.rmtree(build_dir)
build_dir.mkdir(parents=True, exist_ok=True)
def minify_css(src_css, dest_css):
print(f"Minifying CSS: {src_css} → {dest_css}")
result = subprocess.run(
["minify", "-o", str(dest_css), str(src_css)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if result.returncode != 0:
print("Error during CSS minification:")
print(result.stderr, file=sys.stderr)
sys.exit(1)
def run_emacs_publish(dev_mode=True):
if dev_mode:
print("Running Emacs publish script (development)...")
result = subprocess.run(
["emacs", "--script", "publish.el"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
else:
print("Running Emacs publish script (production)...")
result = subprocess.run(
["emacs", "--script", "publish.el"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
text=True,
)
if result.returncode != 0:
print("Error running publish.el:")
print(result.stderr, file=sys.stderr)
sys.exit(1)
def deploy_to_server(build_dir, server):
remote_path = f"{server}:/var/www/cleberg.net/"
print(f"Deploying .build/ → {remote_path}")
result = subprocess.run(
["rsync", "-r", "--delete-before", f"{build_dir}/", remote_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if result.returncode != 0:
print("Error during rsync deployment:")
print(result.stderr, file=sys.stderr)
sys.exit(1)
def start_dev_server(build_dir):
print(f"Starting development HTTP server from {build_dir}/ on port 8000")
os.chdir(build_dir)
# This will run until interrupted (Ctrl+C)
try:
subprocess.run([sys.executable, "-m", "http.server", "8000"], check=True)
except KeyboardInterrupt:
print("\nDevelopment server stopped.")
except subprocess.CalledProcessError as e:
print(f"Error starting development server: {e}", file=sys.stderr)
sys.exit(1)
def main():
html_snippet = get_recent_posts_html("./content/blog", num_posts=3)
build_dir = Path(".build")
theme_dir = Path("theme/static")
css_src = theme_dir / "styles.css"
css_min = theme_dir / "styles.min.css"
env = os.environ.get("ENV", "").lower()
if env == "prod":
print("Environment: Production")
method = prompt("Publishing on remote or LAN? [r|l] ").lower()
if method == "r":
homelab_server = "homelab-remote"
elif method == "l":
homelab_server = "homelab"
else:
print("Invalid input. Assuming LAN (homelab)")
homelab_server = "homelab"
# Remove previous build
remove_build_directory(build_dir)
# Minify CSS
minify_css(css_src, css_min)
# Run publishing script (silenced output)
run_emacs_publish(dev_mode=False)
# Update index page with latest posts
update_index_html(html_snippet)
# Deploy changes
deploy_to_server(build_dir, homelab_server)
else:
print("Environment: Development")
# Remove previous build
remove_build_directory(build_dir)
# Minify CSS
minify_css(css_src, css_min)
# Run publishing script (with console output)
run_emacs_publish(dev_mode=True)
# Update index page with latest posts
update_index_html(html_snippet)
# Launch development web server
start_dev_server(build_dir)
if __name__ == "__main__":
main()
|