forked from simonw/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_index.py
More file actions
executable file
·255 lines (221 loc) · 7.29 KB
/
build_index.py
File metadata and controls
executable file
·255 lines (221 loc) · 7.29 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
#!/usr/bin/env python3
"""Generate index.html from README.md with recent additions and updates."""
from __future__ import annotations
import json
from datetime import datetime
from pathlib import Path
from typing import Iterable, List, Sequence
try:
import markdown
except ModuleNotFoundError as exc: # pragma: no cover - dependency should be installed
raise SystemExit(
"The 'markdown' package is required to build index.html. "
"Install it with 'pip install markdown'."
) from exc
README_PATH = Path("README.md")
TOOLS_JSON_PATH = Path("tools.json")
OUTPUT_PATH = Path("index.html")
def _ordinal(value: int) -> str:
"""Return the ordinal suffix for a day value."""
if 10 <= value % 100 <= 20:
suffix = "th"
else:
suffix = {1: "st", 2: "nd", 3: "rd"}.get(value % 10, "th")
return f"{value}{suffix}"
def _parse_iso_datetime(value: str | None) -> datetime | None:
if not value:
return None
try:
cleaned = value.replace("Z", "+00:00")
return datetime.fromisoformat(cleaned)
except ValueError:
return None
def _has_distinct_update(tool: dict) -> bool:
"""Return True if the tool has an update distinct from its creation."""
updated = _parse_iso_datetime(tool.get("updated"))
if updated is None:
return False
created = _parse_iso_datetime(tool.get("created"))
if created is None:
return True
return updated > created
def _format_display_date(dt: datetime) -> str:
return f"{_ordinal(dt.day)} {dt.strftime('%B %Y')}"
def _load_tools() -> List[dict]:
if not TOOLS_JSON_PATH.exists():
return []
with TOOLS_JSON_PATH.open("r", encoding="utf-8") as fp:
return json.load(fp)
def _select_recent(
tools: Sequence[dict],
*,
key: str,
limit: int,
exclude_slugs: Iterable[str] | None = None,
) -> List[dict]:
excluded = set(exclude_slugs or [])
dated_tools = [
(tool, _parse_iso_datetime(tool.get(key)))
for tool in tools
if tool.get(key)
]
dated_tools = [item for item in dated_tools if item[1] is not None]
dated_tools.sort(key=lambda item: item[1], reverse=True)
selected: List[dict] = []
for tool, parsed_date in dated_tools:
if tool.get("slug") in excluded:
continue
entry = tool.copy()
entry["parsed_date"] = parsed_date
selected.append(entry)
if len(selected) >= limit:
break
return selected
def _render_recent_section(recently_added: Sequence[dict], recently_updated: Sequence[dict]) -> str:
def render_list(tools: Sequence[dict]) -> str:
if not tools:
return "<li>No entries available.</li>"
items = []
for tool in tools:
slug = tool.get("slug", "")
url = tool.get("url", "#")
filename = tool.get("filename", "")
parsed_date = tool.get("parsed_date")
if isinstance(parsed_date, datetime):
formatted_date = _format_display_date(parsed_date)
else:
formatted_date = ""
# Create colophon link for the date
colophon_url = f"https://tools.simonwillison.net/colophon#{filename}" if filename else "#"
date_html = (
f'<span class="recent-date"> — <a href="{colophon_url}">{formatted_date}</a></span>'
if formatted_date
else ""
)
items.append(
f'<li><a href="{url}">{slug}</a>{date_html}</li>'
)
return "\n".join(items)
section_html = f"""
<div class="recent-container">
<div class="recent-column">
<h2>Recently added</h2>
<ul class="recent-list">
{render_list(recently_added)}
</ul>
</div>
<div class="recent-column">
<h2>Recently updated</h2>
<ul class="recent-list">
{render_list(recently_updated)}
</ul>
</div>
</div>
"""
return section_html
def build_index() -> None:
if not README_PATH.exists():
raise FileNotFoundError("README.md not found")
markdown_content = README_PATH.read_text("utf-8")
md = markdown.Markdown(extensions=["extra"])
body_html = md.convert(markdown_content)
tools = _load_tools()
recently_added = _select_recent(tools, key="created", limit=5)
added_slugs = [tool.get("slug") for tool in recently_added]
tools_with_updates = [tool for tool in tools if _has_distinct_update(tool)]
recently_updated = _select_recent(
tools_with_updates, key="updated", limit=5, exclude_slugs=added_slugs
)
recent_section_html = _render_recent_section(recently_added, recently_updated)
# Inject the recent section between the comment markers
start_marker = '<!-- recently starts -->'
end_marker = '<!-- recently stops -->'
if start_marker in body_html and end_marker in body_html:
# Replace content between markers
start_idx = body_html.find(start_marker)
end_idx = body_html.find(end_marker)
if start_idx < end_idx:
body_html = (
body_html[:start_idx + len(start_marker)] +
'\n' + recent_section_html +
body_html[end_idx:]
)
else:
# Fallback: inject before Image and media heading if markers not found
injection_marker = '<h2 id="image-and-media">Image and media</h2>'
if injection_marker in body_html:
body_html = body_html.replace(
injection_marker, recent_section_html + injection_marker, 1
)
else:
body_html = recent_section_html + body_html
full_html = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>tools.simonwillison.net</title>
<style>
body {{
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
line-height: 1.6;
max-width: 980px;
margin: 0 auto;
padding: 20px;
color: #24292e;
}}
h1 {{
border-bottom: 1px solid #eaecef;
padding-bottom: 0.3em;
}}
h2 {{
border-bottom: 1px solid #eaecef;
padding-bottom: 0.3em;
margin-top: 24px;
}}
a {{
color: #0366d6;
text-decoration: none;
}}
a:hover {{
text-decoration: underline;
}}
code {{
background-color: rgba(27,31,35,0.05);
border-radius: 3px;
padding: 0.2em 0.4em;
}}
.recent-container {{
display: flex;
gap: 24px;
flex-wrap: wrap;
margin-bottom: 24px;
}}
.recent-column {{
flex: 1 1 300px;
}}
.recent-column h2 {{
margin-top: 0;
}}
.recent-list {{
list-style: none;
margin: 0;
padding: 0;
}}
.recent-list li {{
margin-bottom: 0.5em;
}}
.recent-date {{
color: #6a737d;
}}
</style>
</head>
<body>
{body_html}
</body>
</html>
"""
OUTPUT_PATH.write_text(full_html, "utf-8")
print("index.html created successfully")
if __name__ == "__main__":
build_index()