Files
longhikes/web/olt/thumbs.py
T
swaitsandClaude Fable 5.2 abfbeb00bd
Validate / validate (push) Canceled after 0s
Add the Long Hikes static site (longhikes.org)
web/ is a static site generator for the dataset, in Python's standard
library only. `python3 web/build.py --out _site` writes a page for each
of the 138 records, a finder on the home page with a map of the 85
lines, region and route indexes, and the repository's documents as
pages. Each line is offered as the record's GeoJSON and as a GPX
simplified to 5 m, with two archives of everything.

The site's name and address are set in web/site.toml: Long Hikes, at
https://longhikes.org. The dataset keeps its own name.

netlify.toml and web/static/_headers configure the Netlify site
(content types, download dispositions, caching, a content security
policy with no inline script). It is deployed with the Netlify CLI
from a local build, because Netlify cannot build from this forge.

MapLibre GL JS and the two typefaces are vendored under web/assets/;
map tiles come from OpenFreeMap. Tests: python3 -m unittest discover
-s web/tests (164).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-13 12:20:32 -07:00

55 lines
2.2 KiB
Python

"""PNG logo thumbnails for the list pages.
web/thumbs/<id>.png is made by web/tools/make_thumbs.py and committed. The pages
step copies it to trails/<id>/shield-thumb.png, and the list pages show that
copy at its real pixel size (read from the PNG header; no image library). A
PNG logo with no thumbnail falls back to the full-size file, with a warning.
SVG logos are not touched.
"""
import os
import shutil
import struct
import sys
WEB_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
THUMB_DIR = os.path.join(WEB_DIR, "thumbs")
THUMB_NAME = "shield-thumb.png"
PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
def png_size(path):
"""(width, height) from the IHDR chunk at the start of a PNG file."""
with open(path, "rb") as f:
head = f.read(24)
if len(head) < 24 or head[:8] != PNG_SIGNATURE or head[12:16] != b"IHDR":
raise ValueError(f"{path} is not a PNG file")
return struct.unpack(">II", head[16:24])
def prepare(rec, out_dir, repo_root, thumb_dir=THUMB_DIR):
"""Copy the record's PNG thumbnail into its output directory and set
rec["derived"]["logo_img"] = {"src", "width", "height"}, the image the list
pages show. Without a PNG logo the key is absent and any old copy is removed.
"""
tid = rec["id"]
tdir = os.path.join(out_dir, "trails", tid)
dest = os.path.join(tdir, THUMB_NAME)
rec["derived"].pop("logo_img", None)
shield = rec["derived"]["files"].get("shield", "")
if not shield.endswith(".png"):
if os.path.isfile(dest):
os.remove(dest)
return
thumb = os.path.join(thumb_dir, f"{tid}.png")
if os.path.isfile(thumb):
shutil.copyfile(thumb, dest)
path, src = dest, f"/trails/{tid}/{THUMB_NAME}"
else:
print(f"warning: {tid}: no thumbnail at {os.path.relpath(thumb, WEB_DIR)}; "
f"the full-size logo is shown (run web/tools/make_thumbs.py)", file=sys.stderr)
if os.path.isfile(dest):
os.remove(dest)
path, src = os.path.join(repo_root, shield), f"/trails/{tid}/{os.path.basename(shield)}"
width, height = png_size(path)
rec["derived"]["logo_img"] = {"src": src, "width": width, "height": height}