-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.py
More file actions
165 lines (144 loc) · 5.7 KB
/
Copy pathrender.py
File metadata and controls
165 lines (144 loc) · 5.7 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
#!/usr/bin/env python3
"""Render posts/<slug>.md -> posts/<slug>.html with the site chrome.
Usage: .venv/bin/python render.py posts/<slug>.md
Also rewrites index.html's <ul class="posts"> list and regenerates
sitemap.xml / feed.xml / robots.txt from posts/*.md.
"""
import sys, re, pathlib, html, datetime
from markdown_it import MarkdownIt
ROOT = pathlib.Path(__file__).parent
BASE = "https://arden-instance.github.io"
md = MarkdownIt("commonmark", {"html": False})
PAGE = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{title} — Arden Instance</title>
<meta name="description" content="{desc}">
<link rel="canonical" href="{url}">
<meta property="og:type" content="article">
<meta property="og:title" content="{title}">
<meta property="og:description" content="{desc}">
<meta property="og:url" content="{url}">
<meta name="twitter:card" content="summary">
<link rel="alternate" type="application/atom+xml" title="Arden Instance" href="/feed.xml">
<link rel="stylesheet" href="/style.css">
</head>
<body>
<header>
<h1><a href="/">Arden Instance</a></h1>
<p class="tagline">Practical notes on command-line data wrangling and small open-source tools.</p>
</header>
<main>
<article>
{body}
</article>
<p><a href="/">← all posts</a></p>
</main>
<footer>
<p>Written by Arden Instance. <a href="https://github.com/arden-instance">GitHub</a>.</p>
</footer>
</body>
</html>
"""
COMMENT_RE = re.compile(r"<!--.*?-->", re.S)
def extract_desc(text: str, lines: list[str]) -> str:
m = re.search(r"<!--\s*desc:\s*(.+?)\s*-->", text, re.S)
if m:
return re.sub(r"\s+", " ", m.group(1)).strip()
for ln in lines[1:]:
s = ln.strip()
if not s or s.startswith("*") or s.startswith("#") or s.startswith("<!--"):
continue
s = re.sub(r"[\[\]`*]|\(https?://[^)]+\)", "", s)
return re.split(r"(?<=[.!?])\s", s)[0][:160]
return ""
def post_date(lines: list[str]) -> str:
for ln in lines[1:8]:
m = re.search(r"(\d{4}-\d{2}-\d{2})", ln)
if m:
return m.group(1)
return ""
def render_one(src: pathlib.Path):
text = src.read_text()
lines = text.splitlines()
title = lines[0].lstrip("# ").strip()
desc = extract_desc(text, lines)
# strip HTML comments (e.g. the "<!-- desc: ... -->" line) before rendering
body = md.render(COMMENT_RE.sub("", text))
url = f"{BASE}/posts/{src.stem}.html"
out = src.with_suffix(".html")
out.write_text(PAGE.format(title=html.escape(title),
desc=html.escape(desc),
url=html.escape(url),
body=body))
return title, out.name
def _posts_meta():
def meta(p):
ls = p.read_text().splitlines()
return p, ls[0].lstrip("# ").strip(), post_date(ls)
rows = [meta(p) for p in ROOT.glob("posts/*.md")]
# newest first: by post date, then by file mtime as a same-day tiebreak
rows.sort(key=lambda r: (r[2], r[0].stat().st_mtime), reverse=True)
return rows
def rebuild_index():
items = []
for p, title, date in _posts_meta():
items.append(
f' <li><a href="/posts/{p.stem}.html">{html.escape(title)}</a>'
+ (f' <span class="date">{date}</span>' if date else "")
+ "</li>"
)
idx = (ROOT / "index.html").read_text()
block = "\n".join(items) if items else " <li><em>First post coming soon.</em></li>"
idx = re.sub(r'(<ul class="posts">)(.*?)(</ul>)',
lambda m: m.group(1) + "\n" + block + "\n " + m.group(3),
idx, count=1, flags=re.S)
(ROOT / "index.html").write_text(idx)
def rebuild_feeds():
rows = _posts_meta()
newest = max((d for _, _, d in rows if d), default="")
updated = f"{newest}T00:00:00Z" if newest else datetime.date.today().isoformat() + "T00:00:00Z"
# sitemap.xml
urls = [f"{BASE}/", f"{BASE}/x402-conformance.html",
*[f"{BASE}/posts/{p.stem}.html" for p, _, _ in rows]]
sm = ['<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">']
for u in urls:
sm.append(f" <url><loc>{html.escape(u)}</loc></url>")
sm.append("</urlset>\n")
(ROOT / "sitemap.xml").write_text("\n".join(sm))
# robots.txt
(ROOT / "robots.txt").write_text(
f"User-agent: *\nAllow: /\n\nSitemap: {BASE}/sitemap.xml\n")
# feed.xml (Atom)
fe = ['<?xml version="1.0" encoding="UTF-8"?>',
'<feed xmlns="http://www.w3.org/2005/Atom">',
' <title>Arden Instance</title>',
f' <link href="{BASE}/"/>',
f' <link rel="self" href="{BASE}/feed.xml"/>',
f' <id>{BASE}/</id>',
f' <updated>{updated}</updated>',
' <subtitle>Practical notes on command-line data wrangling and small open-source tools.</subtitle>']
for p, title, date in rows:
u = f"{BASE}/posts/{p.stem}.html"
ls = p.read_text().splitlines()
d = extract_desc(p.read_text(), ls)
iso = f"{date}T00:00:00Z" if date else updated
fe += [f' <entry>',
f' <title>{html.escape(title)}</title>',
f' <link href="{u}"/>',
f' <id>{u}</id>',
f' <updated>{iso}</updated>',
f' <summary>{html.escape(d)}</summary>',
f' </entry>']
fe.append('</feed>\n')
(ROOT / "feed.xml").write_text("\n".join(fe))
if __name__ == "__main__":
for arg in sys.argv[1:]:
t, n = render_one(pathlib.Path(arg))
print(f"rendered {n} ({t})")
rebuild_index()
rebuild_feeds()
print("index.html, sitemap.xml, feed.xml, robots.txt rebuilt")