forked from leonidk/leonidk.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
290 lines (254 loc) · 9.45 KB
/
generate.py
File metadata and controls
290 lines (254 loc) · 9.45 KB
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
#!/usr/bin/env python3
"""
generate.py — Static site generator for robrui.github.io.
Processes Markdown drafts in drafts/ and generates HTML in posts/NAME/index.html.
Also auto-updates blog/index.html with new entries.
Usage:
python3 generate.py # Process all drafts
python3 generate.py --watch # Watch for changes (requires watchdog)
Dependencies:
pip install markdown
"""
import argparse
import os
import re
import glob
import json
from datetime import datetime
try:
import markdown
HAS_MD = True
except ImportError:
HAS_MD = False
SITE_ROOT = os.path.dirname(os.path.abspath(__file__))
DRAFTS_DIR = os.path.join(SITE_ROOT, "drafts")
POSTS_DIR = os.path.join(SITE_ROOT, "posts")
BLOG_FILE = os.path.join(SITE_ROOT, "blog", "index.html")
ASSETS_DIR = os.path.join(SITE_ROOT, "assets")
BLOG_TEMPLATE_HEAD = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog — Robert Ruidisch</title>
<meta name="description" content="Deep-dive articles on open-source contributions by Robert Ruidisch.">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../assets/style.css">
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><text y='28' font-size='28'>🐙</text></svg>">
</head>
<body>
<nav>
<div class="container">
<a href="../" class="logo"><span>r</span>obrui</a>
<div class="nav-links">
<a href="../">Home</a>
<a href="../projects/">Contributions</a>
<a href="./" class="active">Blog</a>
</div>
</div>
</nav>
<section class="hero" style="padding-bottom: 32px;">
<div class="container-wide container">
<h1>Deep-Dive <span>Blog</span></h1>
<p class="subtitle">Behind every PR is a story — the science, the debugging, the fix.</p>
</div>
</section>
<section class="section" style="padding-top: 0;">
<div class="container-wide container">
<ul class="blog-list">
"""
BLOG_TEMPLATE_TAIL = """ </ul>
</div>
</section>
<footer>
<div class="container">
<div class="social-links">
<a href="https://github.com/robrui">GitHub</a>
<a href="https://www.linkedin.com/in/robert-ruidisch/">LinkedIn</a>
<a href="mailto:robert.ruidisch@gmail.com">Email</a>
</div>
<p>© 2025 Robert Ruidisch</p>
</div>
</footer>
</body>
</html>
"""
def parse_frontmatter(text):
"""Parse YAML-like front matter from markdown."""
meta = {
"title": "Untitled",
"date": datetime.now().strftime("%Y-%m-%d"),
"tags": [],
"pr": "",
"excerpt": "",
"thumbnail": "",
"read_time": 10,
}
if text.startswith("---"):
parts = text.split("---", 2)
if len(parts) >= 3:
for line in parts[1].strip().split("\n"):
if ":" in line:
key, value = line.split(":", 1)
key = key.strip().lower()
value = value.strip().strip('"\'')
if key == "tags":
meta["tags"] = [t.strip() for t in value.split(",")]
elif key == "read_time":
meta["read_time"] = int(value)
else:
meta[key] = value
return meta, parts[2].strip()
return meta, text
def md_to_html(md_text):
"""Convert markdown to HTML."""
if HAS_MD:
return markdown.markdown(md_text, extensions=["fenced_code", "codehilite", "tables"])
else:
# Basic fallback
return "<p>" + md_text.replace("\n\n", "</p><p>") + "</p>"
def generate_post(draft_file):
"""Generate a single post from a markdown draft."""
with open(draft_file, "r") as f:
text = f.read()
meta, body_md = parse_frontmatter(text)
slug = os.path.splitext(os.path.basename(draft_file))[0]
post_dir = os.path.join(POSTS_DIR, slug)
os.makedirs(post_dir, exist_ok=True)
body_html = md_to_html(body_md)
html = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{meta['title']} — Robert Ruidisch</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../../assets/style.css">
<script>window.MathJax={{tex:{{inlineMath:[['$','$']],displayMath:[['$$','$$']]}},options:{{enableMenu:false}}}};</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js" async></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<nav>
<div class="container">
<a href="../../" class="logo"><span>r</span>obrui</a>
<div class="nav-links">
<a href="../../">Home</a>
<a href="../../projects/">Contributions</a>
<a href="../../blog/">Blog</a>
</div>
</div>
</nav>
<article>
<header class="post-header">
<div class="container-narrow container">
<div class="post-label">📝 Deep-Dive</div>
<h1>{meta['title']}</h1>
<div class="post-meta">
<span>{meta['pr']}</span>
· <span>{meta['date']}</span>
· <span>~{meta['read_time']} min read</span>
</div>
</div>
</header>
<div class="post-content">
<div class="container-narrow container">
{body_html}
<hr style="margin: 48px 0; border: none; border-top: 1px solid var(--border);">
<p style="text-align: center; color: var(--muted); font-size: 0.9rem;">
<em>Found this useful?</em> · <a href="https://github.com/robrui">Follow on GitHub</a>
</p>
</div>
</div>
</article>
<footer>
<div class="container">
<div class="social-links">
<a href="https://github.com/robrui">GitHub</a>
<a href="https://www.linkedin.com/in/robert-ruidisch/">LinkedIn</a>
<a href="mailto:robert.ruidisch@gmail.com">Email</a>
</div>
<p>© 2025 Robert Ruidisch</p>
</div>
</footer>
</body>
</html>"""
out_path = os.path.join(post_dir, "index.html")
with open(out_path, "w") as f:
f.write(html)
return meta, slug
def rebuild_blog_index(entries):
"""Rebuild blog/index.html from sorted entries."""
entries.sort(key=lambda e: e["date"], reverse=True)
items = []
for e in entries:
thumbnail = e.get("thumbnail", "")
thumb_html = f'<a href="../posts/{e["slug"]}/" class="blog-thumb"><img src="../{thumbnail}" loading="lazy"></a>' if thumbnail else ""
tags_html = "".join(f'<span class="tag">{t}</span>' for t in e.get("tags", []))
pr_link = f'<a href="https://github.com/{e["pr"]}" style="color:var(--accent);text-decoration:none;">{e["pr"]}</a>' if e.get("pr") else ""
items.append(f""" <li class="blog-entry">
{thumb_html}
<div class="blog-info">
<h3><a href="../posts/{e["slug"]}/">{e["title"]}</a></h3>
<div class="blog-meta">
<span>📅 {e["date"]}</span>
<span>⏱ ~{e["read_time"]} min</span>
{f'<span>🔧 {pr_link}</span>' if pr_link else ''}
</div>
<p class="blog-excerpt">{e.get("excerpt", "")}</p>
<div class="blog-tags">{tags_html}</div>
</div>
</li>""")
with open(BLOG_FILE, "w") as f:
f.write(BLOG_TEMPLATE_HEAD)
f.write("\n".join(items) + "\n")
f.write(BLOG_TEMPLATE_TAIL)
def process_all():
"""Process all drafts."""
if not os.path.exists(DRAFTS_DIR):
print("No drafts/ directory found.")
return
draft_files = sorted(glob.glob(os.path.join(DRAFTS_DIR, "*.md")))
entries = []
for df in draft_files:
meta, slug = generate_post(df)
meta["slug"] = slug
entries.append(meta)
print(f" ✓ {slug}")
if entries:
rebuild_blog_index(entries)
print(f"\nBlog index updated with {len(entries)} entries.")
else:
print("No drafts to process.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate HTML from Markdown drafts")
parser.add_argument("--watch", action="store_true", help="Watch for file changes")
args = parser.parse_args()
if args.watch:
try:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class DraftHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path.endswith(".md"):
print(f"Change detected: {event.src_path}")
process_all()
observer = Observer()
os.makedirs(DRAFTS_DIR, exist_ok=True)
observer.schedule(DraftHandler(), DRAFTS_DIR, recursive=False)
observer.start()
print(f"Watching {DRAFTS_DIR} for changes... (Ctrl+C to stop)")
try:
observer.join()
except KeyboardInterrupt:
observer.stop()
observer.join()
except ImportError:
print("watchdog not installed. Run: pip install watchdog")
process_all()
else:
process_all()