Files
longhikes/web/olt/seo.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

75 lines
2.7 KiB
Python

"""Sitemap, robots.txt and the JSON-LD Dataset description."""
import html
import os
from . import common
# The files under /data/ listed as the dataset's distribution: (name, media type).
DISTRIBUTION = (
("trails.csv", "text/csv"),
("trails.json", "application/json"),
("index.geojson", "application/geo+json"),
("overview.geojson", "application/geo+json"),
("all-gpx.zip", "application/zip"),
("all-geojson.zip", "application/zip"),
)
def dataset_jsonld(records, site):
"""One schema.org Dataset block for the home page and /data/."""
dist = []
for name, fmt in DISTRIBUTION:
url = site.canonical(f"/data/{name}") or f"/data/{name}"
dist.append({"@type": "DataDownload", "name": name, "encodingFormat": fmt, "contentUrl": url})
obj = {
"@context": "https://schema.org",
"@type": "Dataset",
"name": site.name,
"description": site.tagline,
"license": "https://creativecommons.org/publicdomain/zero/1.0/",
"distribution": dist,
"variableMeasured": ["length_km", "country", "regions", "source licence"],
}
if site.base_url:
obj["url"] = site.canonical("/")
return obj
def sitemap_xml(paths, site):
"""sitemap.xml text for site paths; caller checks site.base_url is set."""
urls = "".join(
f" <url><loc>{html.escape(site.canonical(p), quote=True)}</loc></url>\n"
for p in paths)
return ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
f"{urls}</urlset>\n")
def robots_txt(site):
lines = ["User-agent: *", "Allow: /", ""]
if site.base_url:
lines.append(f"Sitemap: {site.canonical('/sitemap.xml')}")
return "\n".join(lines).rstrip() + "\n"
def write_seo_files(out_dir, paths, site):
"""Write robots.txt always and sitemap.xml only with a base_url."""
with open(os.path.join(out_dir, "robots.txt"), "w", encoding="utf-8", newline="\n") as f:
f.write(robots_txt(site))
sitemap = os.path.join(out_dir, "sitemap.xml")
if site.base_url:
with open(sitemap, "w", encoding="utf-8", newline="\n") as f:
f.write(sitemap_xml(paths, site))
elif os.path.isfile(sitemap):
os.remove(sitemap) # a stale sitemap with wrong URLs is worse than none
def all_paths(records, region_paths):
"""Every indexable page path, in nav order then record pages."""
paths = ["/", "/regions/"] + list(region_paths) + [
"/routes/", "/sources/", "/logos/", "/about/", "/data/",
"/data/schema/", "/data/changelog/"]
paths += [common.trail_path(r) for r in sorted(records, key=lambda r: r["id"])]
return paths