Files
mirage/generate_index.py
2025-12-02 01:30:46 -05:00

62 lines
1.6 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import pathlib
import html
BASE = pathlib.Path("/srv/www")
URL_LIST = BASE / "mirrors.txt"
OUTDIR = BASE / "mirrors"
INDEX = BASE / "index.html"
entries = []
if URL_LIST.exists():
for line in URL_LIST.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
parts = line.split(None, 1)
if len(parts) != 2:
continue
slug, url = parts
mirror_dir = OUTDIR / slug
if not mirror_dir.exists():
# not mirrored yet, but still list it
status = " (not downloaded yet)"
else:
status = ""
entries.append((slug, url, status))
items_html = []
for slug, url, status in entries:
slug_esc = html.escape(slug)
url_esc = html.escape(url)
status_esc = html.escape(status)
# Link goes to the directory; nginx autoindex or an index file will handle it
items_html.append(
f'<li><a href="mirrors/{slug_esc}/">{slug_esc}</a>'
f' <code>{url_esc}</code>{status_esc}</li>'
)
html_doc = f"""<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My Tutorial Mirrors</title>
<style>
body {{ font-family: sans-serif; max-width: 800px; margin: 2rem auto; }}
h1 {{ margin-bottom: 0.5rem; }}
code {{ font-size: 0.9em; }}
</style>
</head>
<body>
<h1>Nytegear Mirrors</h1>
<p>This page is generated automatically from <code>mirrors.txt</code>.</p>
<ul>
{''.join(items_html)}
</ul>
</body>
</html>
"""
INDEX.write_text(html_doc, encoding="utf-8")