- Emblems for 81 trails, up from 41. Each is the trail's own crest, badge or roundel alone, cropped out of a larger logo where need be and made transparent where that could be done cleanly. Records without one say where an emblem was looked for. The validator checks the logo state. - Every record has a `[[history]]` list: what changed or was asked for, on what basis, who asked and whether it was applied or declined. `tools/check_history.py` fails a changed fact with no history entry and prints earlier entries about a changed field. It runs in CI before the site deploys. Each trail page shows the history. - A new page, "Considered and left out", built from `left-out.toml`: 35 trails and routes with the reason, the deciding fact and its source. - The Hoosier Heritage Trail has its ends, its free map set and its emblem from its association (issue 2). Any trail may list its holder's own guide or map set. - A stated permission is quoted in the download credit box. - `tools/gpx_track.py` turns a GPX file from a holder or a hiker into a record's track files. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
75 lines
2.7 KiB
Python
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/", "/left-out/"]
|
|
paths += [common.trail_path(r) for r in sorted(records, key=lambda r: r["id"])]
|
|
return paths
|