Files
longhikes/web/olt/site.py
T
swaitsandClaude Fable 5.2 5b80fccd1e
Mirror to forge / mirror (push) Canceled after 0s
Validate / validate (push) Canceled after 0s
Validate / history (push) Canceled after 0s
Validate / site (push) Canceled after 0s
Validate / deploy (push) Canceled after 0s
Say what each page is in the words people search with, and mark up the facts
A whole-site search audit found clean foundations and three gaps: titles
without the words people search for, no structured data on trail pages,
and trails that did not link to each other.

- Trail titles carry the name hikers use, the abbreviation, the length
  and what the page offers; "GPX" only where the page has one. A new
  optional `common_name` field holds the name hikers use (13 records).
- Descriptions are built from each record, at most 155 characters.
- Place and list pages are titled for what they are; heading levels no
  longer jump.
- Trail pages link to similar and related trails and to the lists they
  belong to. Every trail has at least three links from other trails.
- JSON-LD on every page: a Place for each trail, breadcrumbs, item lists
  on list and place pages, WebSite and Organization on the home page, a
  fuller Dataset on the data page.
- A social card per trail, a changelog feed, `lastmod` in the sitemap from
  record dates, `noindex` on the 404 and on raw per-trail files, a
  favicon and an `llms.txt` listing the data files and licences.
- The "Considered and left out" page has its spacing fixed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 14:57:30 -07:00

116 lines
4.7 KiB
Python

"""Site-wide settings from web/site.toml (name, tagline, home_title, base_url, repo_url, repo_branch, contact_url, name_lang)."""
import functools
import os
import tomllib
import urllib.parse
WEB_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SITE_TOML = os.path.join(WEB_DIR, "site.toml")
class Site:
"""The settings every page needs. Build one with load()."""
def __init__(self, name, tagline, base_url="", repo_url="", repo_branch="main",
contact_url="", name_lang=None, home_title=""):
if not name:
raise ValueError("site.toml: name must not be empty")
self.name = name
self.tagline = tagline
# The home page's own title, before the name is added (about 50 characters).
self.home_title = home_title.strip()
self.base_url = base_url.rstrip("/")
self.repo_url = repo_url.rstrip("/")
self.repo_branch = (repo_branch or "main").strip() or "main"
self.contact_url = contact_url.strip()
# record id -> language tag of its name ("fr"), for names that are not English
self.name_lang = dict(name_lang or {})
def canonical(self, path):
"""Absolute URL of a site path ("/trails/arizona/"), or "" when base_url is unset."""
return self.base_url + path if self.base_url else ""
@property
def issues_url(self):
"""Where to report an error, or "" when repo_url is unset."""
return self.repo_url + "/issues" if self.repo_url else ""
@property
def is_github(self):
"""True when repo_url is a github.com repository (the only host whose
edit/history/issue-form URL shapes the site builds)."""
return (urllib.parse.urlsplit(self.repo_url).hostname or "").lower() == "github.com"
def _github_issue_form_url(self, template, params=()):
query = urllib.parse.urlencode([("template", template), *params])
return f"{self.repo_url}/issues/new?{query}"
def correction_url(self, trail_id="", name=""):
"""Report-a-problem URL: the GitHub correction form prefilled with the
trail (fields are prefilled by their form id), else the issues list,
else "" when repo_url is unset."""
if not self.repo_url:
return ""
if not self.is_github:
return self.issues_url
params = []
if trail_id:
title = f"Correction: {name} ({trail_id})" if name else f"Correction: {trail_id}"
params = [("title", title), ("trail", trail_id)]
return self._github_issue_form_url("correction.yml", params)
@property
def new_trail_url(self):
"""Suggest-a-trail URL: the GitHub new-trail form, else the issues list, else ""."""
if not self.repo_url:
return ""
if not self.is_github:
return self.issues_url
return self._github_issue_form_url("new-trail.yml")
@property
def removal_url(self):
"""Track/logo removal URL: the GitHub removal form, else the issues list, else ""."""
if not self.repo_url:
return ""
if not self.is_github:
return self.issues_url
return self._github_issue_form_url("removal.yml")
def edit_url(self, trail_id):
"""GitHub edit URL for <id>/trail.toml, or "" for any other host / no repo."""
if not self.is_github:
return ""
branch = urllib.parse.quote(self.repo_branch, safe="/")
return f"{self.repo_url}/edit/{branch}/{urllib.parse.quote(trail_id, safe='')}/trail.toml"
def history_url(self, trail_id):
"""GitHub commit-history URL for <id>/, or "" for any other host / no repo."""
if not self.is_github:
return ""
branch = urllib.parse.quote(self.repo_branch, safe="/")
return f"{self.repo_url}/commits/{branch}/{urllib.parse.quote(trail_id, safe='')}"
def blob_url(self, path):
"""Repository URL of a file at repo_branch, or "" when repo_url is unset."""
if not self.repo_url:
return ""
branch = urllib.parse.quote(self.repo_branch, safe="/")
return f"{self.repo_url}/blob/{branch}/{path.lstrip('/')}"
def from_file(path):
with open(path, "rb") as f:
data = tomllib.load(f)
return Site(name=data.get("name", ""), tagline=data.get("tagline", ""),
base_url=data.get("base_url", ""), repo_url=data.get("repo_url", ""),
repo_branch=data.get("repo_branch", "main"),
contact_url=data.get("contact_url", ""), name_lang=data.get("name_lang", {}),
home_title=data.get("home_title", ""))
@functools.cache
def load():
"""The settings in web/site.toml (read once per process)."""
return from_file(SITE_TOML)