-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
261 lines (216 loc) · 8.71 KB
/
Copy pathconvert.py
File metadata and controls
261 lines (216 loc) · 8.71 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
#!/usr/bin/env python3
"""
Convert markdown posts to HTML.
Usage:
python3 convert.py # Convert all markdown files
python3 convert.py --watch # Watch for changes and rebuild
"""
import os
import sys
import re
import subprocess
from pathlib import Path
from datetime import datetime
# Directories
MARKDOWN_DIR = Path("posts/markdown")
POSTS_DIR = Path("posts")
TEMPLATE_PATH = POSTS_DIR / "template.html"
INDEX_PATH = POSTS_DIR / "index.html"
def extract_metadata(md_content):
"""Extract title and description from markdown."""
title_match = re.search(r'^# (.+)$', md_content, re.MULTILINE)
title = title_match.group(1) if title_match else "Untitled"
# Get first paragraph as description
desc_match = re.search(r'^(?!#)(.{0,150})', md_content, re.MULTILINE)
description = desc_match.group(1).strip() if desc_match else "Blog post"
return title, description.replace('\n', ' ')
def markdown_to_html(md_path):
"""Convert markdown file to HTML using pandoc."""
try:
result = subprocess.run(
['pandoc', '-f', 'markdown', '-t', 'html', str(md_path)],
capture_output=True,
text=True,
check=True
)
return result.stdout
except FileNotFoundError:
# pandoc binary not found — try Python fallback via pypandoc
try:
import pypandoc
try:
return pypandoc.convert_file(str(md_path), 'html', format='md')
except (OSError, RuntimeError):
# Attempt to download pandoc for pypandoc
try:
from pypandoc import download_pandoc
print("Pandoc binary missing — downloading pandoc via pypandoc (may take a while)...")
download_pandoc()
return pypandoc.convert_file(str(md_path), 'html', format='md')
except Exception as e:
print("ERROR: failed to download or use pandoc via pypandoc:", e)
print("Please install pandoc system-wide: https://pandoc.org/installing.html")
sys.exit(1)
except ImportError:
# Provide OS-specific install hint
if os.name == 'nt':
print("ERROR: pandoc not found. On Windows install with Chocolatey: `choco install pandoc` or from https://pandoc.org/installing.html")
else:
print("ERROR: pandoc not found. Install with your package manager (e.g., `brew install pandoc`) or from https://pandoc.org/installing.html")
print("Alternatively, install the Python helper `pypandoc` to enable automatic download: `pip install pypandoc`")
sys.exit(1)
except subprocess.CalledProcessError as e:
print(f"ERROR converting {md_path}: {e.stderr}")
return None
def get_date_from_filename(filename):
"""Extract date from filename (e.g., 2026-01-12-title.md -> 2026-01-12)."""
match = re.match(r'(\d{4}-\d{2}-\d{2})', filename)
return match.group(1) if match else datetime.now().strftime('%Y-%m-%d')
def format_date(date_str):
"""Format YYYY-MM-DD to 'Jan 12, 2026'."""
try:
d = datetime.strptime(date_str, '%Y-%m-%d')
return d.strftime('%b %d, %Y')
except:
return date_str
def estimate_read_time(html_content):
"""Rough estimate of read time in minutes."""
# Count words (roughly)
word_count = len(re.findall(r'\w+', html_content))
read_time = max(1, round(word_count / 200)) # ~200 words per minute
return read_time
def convert_post(md_path):
"""Convert a single markdown file to HTML post."""
md_file = md_path.name
slug = md_file.replace('.md', '')
html_filename = f"{slug}.html"
html_path = POSTS_DIR / html_filename
print(f"Converting {md_file}...")
# Read markdown
with open(md_path, 'r', encoding='utf-8') as f:
md_content = f.read()
# Extract metadata
title, description = extract_metadata(md_content)
date_str = get_date_from_filename(md_file)
formatted_date = format_date(date_str)
# Convert to HTML
html_content = markdown_to_html(md_path)
if html_content is None:
return None
# Strip the first <h1> from pandoc output since it's already in the header
html_content = re.sub(r'^<h1[^>]*>.*?</h1>\s*', '', html_content, flags=re.DOTALL)
# Estimate read time
read_time = estimate_read_time(html_content)
# Read template
with open(TEMPLATE_PATH, 'r', encoding='utf-8') as f:
template = f.read()
# Build final HTML
output = template.replace(
'<title>Post Title — Your Name</title>',
f'<title>{title} — Your Name</title>'
).replace(
'<h1>Post Title Goes Here</h1>',
f'<h1>{title}</h1>'
).replace(
'<p class="post-meta">Jan 12, 2026 · 5 min read</p>',
f'<p class="post-meta">{formatted_date} · {read_time} min read</p>'
)
# Replace all content between <main class="post-content"> and </main>
output = re.sub(
r'(<main class="post-content">)\s*.*?(\s*</main>)',
f'\\1\n{html_content}\n \\2',
output,
flags=re.DOTALL
)
# Write output
with open(html_path, 'w', encoding='utf-8') as f:
f.write(output)
print(f" ✓ Created {html_filename}")
return {
'slug': slug,
'title': title,
'description': description,
'date': date_str,
'formatted_date': formatted_date,
'read_time': read_time,
'html_filename': html_filename
}
def update_blog_index(posts_metadata):
"""Update posts/index.html with links to all posts."""
if not posts_metadata:
return
# Sort by date descending (newest first)
sorted_posts = sorted(posts_metadata, key=lambda x: x['date'], reverse=True)
# Generate post list HTML
post_links = []
for post in sorted_posts:
link = f""" <article class="post">
<h2><a href="/posts/{post['html_filename']}">{post['title']}</a></h2>
<p class="meta">{post['formatted_date']} · {post['read_time']} min read</p>
<p>{post['description']}</p>
</article>"""
post_links.append(link)
post_list_html = "\n\n".join(post_links)
# Read current index
with open(INDEX_PATH, 'r', encoding='utf-8') as f:
index_content = f.read()
# Find and replace the post list section
# Look for: <article class="post"> ... </article> patterns and replace them
pattern = r'(<main>\s*).*?(<p style="margin-top:22px">)'
replacement = f'\\1{post_list_html}\n\n \\2'
updated_index = re.sub(pattern, replacement, index_content, flags=re.DOTALL)
# Write updated index
with open(INDEX_PATH, 'w', encoding='utf-8') as f:
f.write(updated_index)
print(f"\n✓ Updated {INDEX_PATH} with {len(sorted_posts)} post(s)")
def convert_all():
"""Convert all markdown files."""
if not MARKDOWN_DIR.exists():
print(f"ERROR: {MARKDOWN_DIR} not found")
sys.exit(1)
if not TEMPLATE_PATH.exists():
print(f"ERROR: {TEMPLATE_PATH} not found")
sys.exit(1)
# Find all markdown files
md_files = sorted(MARKDOWN_DIR.glob('*.md'))
if not md_files:
print(f"No markdown files found in {MARKDOWN_DIR}")
return
posts_metadata = []
for md_path in md_files:
metadata = convert_post(md_path)
if metadata:
posts_metadata.append(metadata)
# Update index
if posts_metadata:
update_blog_index(posts_metadata)
print(f"\n✓ Done! Converted {len(posts_metadata)} post(s)")
def watch_and_convert():
"""Watch markdown directory for changes and rebuild."""
try:
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MarkdownHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path.endswith('.md'):
print(f"\nDetected change: {event.src_path}")
convert_all()
observer = Observer()
observer.schedule(MarkdownHandler(), str(MARKDOWN_DIR))
observer.start()
print(f"Watching {MARKDOWN_DIR} for changes (Ctrl+C to stop)...")
while True:
time.sleep(1)
except ImportError:
print("ERROR: watchdog not installed. Install with: pip install watchdog")
sys.exit(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
print("\nStopped watching")
if __name__ == "__main__":
if "--watch" in sys.argv:
watch_and_convert()
else:
convert_all()