"""Sitemap, robots.txt, and the JSON-LD (schema.org) that describes each page. Everything here is read from the records and from web/site.toml. No trail is named in this file: a new record gets its markup from its own fields. Types, and why (checked against schema.org and Google Search Central, 2026-11): Trail page ItemPage, whose mainEntity is a Place that is also a TouristAttraction. schema.org has no type for a trail (schema.org/Trail is a 404), and Google has no trail or place feature, so this markup is for search and assistants that read entities, not for a rich result. A trail is a place a visitor goes to, so Place is true of it; TouristAttraction is a subtype of Place that "in principle any Thing can be". A route is the same kind of place; the page says it is a route by its author in additionalProperty. A route's author is free text in the record (a person, two people, an association), so it is never typed as Person or Organization. Lists, places CollectionPage whose mainEntity is an ItemList of the trails. Every page BreadcrumbList (a Google feature), read from the breadcrumb the page shows, so the two cannot differ. Home WebSite and Organization. A one-person open data project is not a legal entity, but Organization is the type Google documents for the publisher of a site; it carries only what site.toml holds. No SearchAction: Google retired the sitelinks search box. /data/ Dataset (a Google feature), with its downloads. dateModified is the newest date the record shows for itself (modified()), never the build date, so a page's date moves only when its content does. """ import html import os import re from html.parser import HTMLParser from . import common, docs, facets, hike, words CC0 = "https://creativecommons.org/publicdomain/zero/1.0/" # Media types of the files under /data/, which docs.BULK_FILES describes. MEDIA_TYPES = { "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 distribution(): """[(name, media type, what it holds)] for each bulk file, as the data page lists them.""" return [(name, MEDIA_TYPES[name], what) for name, what, _ in docs.BULK_FILES] # What the dataset records for each trail (docs/schema.md), as variableMeasured. VARIABLES = ( ("length_km", "End-to-end length of the trail's canonical path, as its holder or author states it", "KMT"), ("system_km", "The larger figure the holder publishes for the whole system or a planned total, where there is one", "KMT"), ("built_km", "The built length of an unfinished trail, as its holder states it", "KMT"), ("measured_km", "Length measured from the track, in kilometres", "KMT"), ("difference_percent", "Measured length against the published length, in per cent", "P1"), ("ascent_m", "Total climbing along the track, computed from an elevation model", "MTR"), ("days", "Usual time to walk the whole trail, as a source states it", "DAY"), ("regions", "States and provinces crossed", ""), ("terrain", "Main kinds of country", ""), ("shape", "End to end, loop or network", ""), ("source.licence", "Licence of the track, as its source states it", ""), ("verification.date", "Date the track was last checked", ""), ) # Dates ----------------------------------------------------------------------- def record_dates(rec): """Every date a record gives for its own checks and changes, as YYYY-MM-DD strings.""" found = [rec.get("verification", {}).get("date"), hike.table(rec).get("checked"), rec.get("source", {}).get("source_checked")] found += [e.get("date") for e in rec.get("history", []) if isinstance(e, dict)] return [str(d) for d in found if d and re.fullmatch(r"\d{4}-\d{2}-\d{2}", str(d))] def modified(rec): """The newest of the record's check dates and its newest history entry, or "".""" return max(record_dates(rec), default="") def checked(rec): """The newest date the record itself was checked on: its verification, its [hike] facts or its source read. This is the date a trail page shows as "Last checked" and the page's dateModified. A history entry is a change or a request, not a check, so it is left out.""" found = [rec.get("verification", {}).get("date"), hike.table(rec).get("checked"), rec.get("source", {}).get("source_checked")] return max((str(d) for d in found if d and re.fullmatch(r"\d{4}-\d{2}-\d{2}", str(d))), default="") def newest(recs): """The newest modified() among records: the date of a page that lists them.""" return max((modified(r) for r in recs), default="") def earliest(recs): """The oldest date any record gives: when the first record was made.""" return min((d for r in recs for d in record_dates(r)), default="") # JSON-LD --------------------------------------------------------------------- def url_of(site, path): """Absolute URL of a site path, or the path itself when site.toml has no base_url.""" return site.canonical(path) or path def country_names(records): """The countries the records cover, the United States first, as a phrase for a sentence: "the United States and Canada".""" codes = sorted({c for r in records for c in common.countries(r)}, key=lambda c: (c != "US", c)) return words.join_names(("the " if c == "US" else "") + words.COUNTRIES[c] for c in codes if c in words.COUNTRIES) def breadcrumb_id(site, path): return url_of(site, path) + "#breadcrumb" def website_ref(site): return {"@type": "WebSite", "@id": url_of(site, "/") + "#website", "name": site.name, "url": url_of(site, "/")} def _clean(obj): """Drop empty values, so a node holds only what the record has.""" if isinstance(obj, dict): out = {k: _clean(v) for k, v in obj.items()} return {k: v for k, v in out.items() if v not in ("", None, [], {})} if isinstance(obj, list): return [v for v in (_clean(x) for x in obj) if v not in ("", None, [], {})] return obj def _pv(name, value, unit_code="", unit_text="", **extra): node = {"@type": "PropertyValue", "name": name, "value": value} if unit_code: node["unitCode"] = unit_code if unit_text: node["unitText"] = unit_text node.update(extra) return node class _Crumbs(HTMLParser): """The (text, href) items of the first