Add park, range and wilderness pages
This commit is contained in:
1 parent
f08c661c16
commit
16c5275c94
6 files changed
+359
-5
No files matched your search
@@ -30,6 +30,7 @@ NAV = (
|
|||||||
# Pages with no header item of their own, and the item they sit under. A trail's page is part of
|
# Pages with no header item of their own, and the item they sit under. A trail's page is part of
|
||||||
# Trails; the dataset pages are part of About and are linked from the About hub and the footer.
|
# Trails; the dataset pages are part of About and are linked from the About hub and the footer.
|
||||||
NAV_PARENT = {"/trails/": "/",
|
NAV_PARENT = {"/trails/": "/",
|
||||||
|
"/parks/": "/regions/", "/ranges/": "/regions/", "/wilderness/": "/regions/",
|
||||||
"/sources/": "/about/", "/logos/": "/about/", "/data/": "/about/",
|
"/sources/": "/about/", "/logos/": "/about/", "/data/": "/about/",
|
||||||
"/left-out/": "/about/", "/method/": "/about/", "/glossary/": "/about/"}
|
"/left-out/": "/about/", "/method/": "/about/", "/glossary/": "/about/"}
|
||||||
# Under Lists: the lists (Routes among them), the tables and rankings, the authors and the views of
|
# Under Lists: the lists (Routes among them), the tables and rankings, the authors and the views of
|
||||||
|
|||||||
+13
-1
@@ -31,11 +31,17 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from .markup import esc
|
from .markup import esc
|
||||||
from . import authors, browse, cards, datafiles, docs, facets, feed, home, layout, leftout, pagekit, placefacets, reference, regions, saved, seo, site as site_settings, sketch, standing, thumbs, trail, views
|
from . import authors, browse, cards, datafiles, docs, facets, feed, home, layout, leftout, pagekit, placefacets, placepages, reference, regions, saved, seo, site as site_settings, sketch, standing, thumbs, trail, views
|
||||||
|
|
||||||
WEB_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
WEB_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
ASSETS_DIR = os.path.join(WEB_DIR, "assets")
|
ASSETS_DIR = os.path.join(WEB_DIR, "assets")
|
||||||
|
|
||||||
|
# Builders of further place pages, run after the state and group pages. Each takes
|
||||||
|
# (records, site, repo_root) and returns pages as (path, title, description, body,
|
||||||
|
# needs_map, recs); each page is added like a state page and reaches the sitemap
|
||||||
|
# through region_paths.
|
||||||
|
EXTRA_PAGE_BUILDERS = (placepages.build,)
|
||||||
|
|
||||||
|
|
||||||
def copy_assets(out_dir, assets_dir=ASSETS_DIR):
|
def copy_assets(out_dir, assets_dir=ASSETS_DIR):
|
||||||
"""Copy web/assets/ to <out>/assets/, replacing what was there. The fingerprinted files
|
"""Copy web/assets/ to <out>/assets/, replacing what was there. The fingerprinted files
|
||||||
@@ -159,6 +165,12 @@ def render_site(records, out_dir, repo_root, site=None):
|
|||||||
path = regions.group_path(g)
|
path = regions.group_path(g)
|
||||||
region_paths.append(path)
|
region_paths.append(path)
|
||||||
add(path, t, d, b, maps=needs_map, recs=recs)
|
add(path, t, d, b, maps=needs_map, recs=recs)
|
||||||
|
# Further place pages (parks, ranges and wilderness areas, olt/placepages.py).
|
||||||
|
for builder in EXTRA_PAGE_BUILDERS:
|
||||||
|
for built in builder(records, site, repo_root):
|
||||||
|
region_paths.append(built.path)
|
||||||
|
add(built.path, built.title, built.description, built.body,
|
||||||
|
maps=built.needs_map, recs=built.recs)
|
||||||
# Pages for one kind of trail in one place (olt/placefacets.py).
|
# Pages for one kind of trail in one place (olt/placefacets.py).
|
||||||
for p in facet_plan:
|
for p in facet_plan:
|
||||||
t, d, b, needs_map = placefacets.page(p, records, site, facet_plan)
|
t, d, b, needs_map = placefacets.page(p, records, site, facet_plan)
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
"""Pages for national parks, mountain ranges and wilderness areas:
|
||||||
|
/parks/<slug>/, /ranges/<slug>/, /wilderness/<slug>/
|
||||||
|
|
||||||
|
The page itself is regions.place_page, the same one a state page uses; this module
|
||||||
|
holds only what is different here, as a regions.PageKind each: plain A to Z rows,
|
||||||
|
the states-and-provinces Where filter, the map opened on the registry's box, and
|
||||||
|
no whole-trail figure given as the place's (the summary's place has no state keys).
|
||||||
|
olt/pages.py runs build() through its EXTRA_PAGE_BUILDERS. A place is here only
|
||||||
|
with three or more trails (olt/places.py collect)."""
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
|
from . import common, places, regions, words
|
||||||
|
from .markup import esc
|
||||||
|
|
||||||
|
Built = namedtuple("Built", ("path", "title", "description", "body", "needs_map", "recs"))
|
||||||
|
|
||||||
|
|
||||||
|
def _in_name(info):
|
||||||
|
"""The name as it reads after "in": a range or a wilderness area takes "the"
|
||||||
|
("in the Wind River Range"), a park does not ("in Yosemite National Park")."""
|
||||||
|
name = info["name"]
|
||||||
|
if info["kind"] in ("range", "wilderness") and not name.lower().startswith("the "):
|
||||||
|
return f"the {name}"
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
|
def _title_place(info):
|
||||||
|
"""The name in a page's title: _in_name with its one state spelled out for a
|
||||||
|
range or wilderness area (places.title_name), "the Wind River Range, Wyoming"."""
|
||||||
|
text = places.title_name(info)
|
||||||
|
if info["kind"] in ("range", "wilderness") and not text.lower().startswith("the "):
|
||||||
|
return f"the {text}"
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def _kicker(info):
|
||||||
|
noun = places.KINDS[info["kind"]].noun
|
||||||
|
names = places.state_names(info)
|
||||||
|
return f"{noun} · {words.join_names(names)}" if names else noun
|
||||||
|
|
||||||
|
|
||||||
|
def _crumbs(info):
|
||||||
|
return '<li><a href="/">Trails</a></li><li><a href="/regions/">Places</a></li>'
|
||||||
|
|
||||||
|
|
||||||
|
def _lede(info, recs, n_gpx):
|
||||||
|
return f"{regions._plural(len(recs))} in {esc(_in_name(info))}, {n_gpx} with a GPX here."
|
||||||
|
|
||||||
|
|
||||||
|
def _title_options(info, recs, site):
|
||||||
|
base = f"Long Hikes in {_title_place(info)}"
|
||||||
|
return [f"{base}: {regions._plural(len(recs), 'Trail')}", base]
|
||||||
|
|
||||||
|
|
||||||
|
def _description(info, recs):
|
||||||
|
n_gpx = regions._gpx_count(recs)
|
||||||
|
routes = sum(1 for r in recs if common.is_route(r))
|
||||||
|
what = regions._plural(len(recs), "trail") + (" and routes" if 0 < routes < len(recs) else "")
|
||||||
|
if routes == len(recs):
|
||||||
|
what = regions._plural(len(recs), "route")
|
||||||
|
what = what[:1].upper() + what[1:]
|
||||||
|
gpx = (f" {regions._with_gpx(n_gpx, len(recs))[:1].upper()}{regions._with_gpx(n_gpx, len(recs))[1:]}."
|
||||||
|
if n_gpx else "")
|
||||||
|
by_length = sorted(recs, key=lambda r: (r["length_km"], r["id"]))
|
||||||
|
# No state keys: a trail that runs on out of the place is never sized as the place's.
|
||||||
|
first = (f"{what} in {_title_place(info)}, from {common.plain_name(by_length[0])}, "
|
||||||
|
f"{regions.trail_miles(by_length[0], ())}, to {common.plain_name(by_length[-1])}, "
|
||||||
|
f"{regions.trail_miles(by_length[-1], ())}.")
|
||||||
|
return regions._fit([first + gpx, first, f"{what} in {_title_place(info)}.{gpx}"])
|
||||||
|
|
||||||
|
|
||||||
|
def _nav(info, records, recs, facet_pages):
|
||||||
|
return (regions._lists_nav(_in_name(info), recs, records)
|
||||||
|
+ regions._facet_nav(_in_name(info), facet_pages))
|
||||||
|
|
||||||
|
|
||||||
|
def _kind():
|
||||||
|
return regions.PageKind(
|
||||||
|
kicker=_kicker, crumbs=_crumbs, lede=_lede, where="all", wholly=False,
|
||||||
|
facts_place=lambda info: (_in_name(info), ()), title_options=_title_options,
|
||||||
|
description=_description, box=lambda info: info.get("box"), nav=_nav,
|
||||||
|
in_name=_in_name)
|
||||||
|
|
||||||
|
|
||||||
|
PARK = _kind()
|
||||||
|
RANGE = _kind()
|
||||||
|
WILDERNESS = _kind()
|
||||||
|
PAGE_KINDS = {"park": PARK, "range": RANGE, "wilderness": WILDERNESS}
|
||||||
|
|
||||||
|
|
||||||
|
def build(records, site, repo_root):
|
||||||
|
"""[Built] for every place with a page, parks then ranges then wilderness areas,
|
||||||
|
each A to Z. The plug-in point's shape (olt/pages.py EXTRA_PAGE_BUILDERS)."""
|
||||||
|
infos = places.collect(records, places.load(repo_root))
|
||||||
|
out = []
|
||||||
|
for info in sorted(infos.values(),
|
||||||
|
key=lambda i: (places.KIND_ORDER[i["kind"]], i["name"].lower())):
|
||||||
|
title, description, body, needs_map = regions.place_page(
|
||||||
|
info, records, site, PAGE_KINDS[info["kind"]])
|
||||||
|
out.append(Built(info["path"], title, description, body, needs_map, info["records"]))
|
||||||
|
return out
|
||||||
+7
-3
@@ -261,7 +261,9 @@ class PageKind:
|
|||||||
that test (no keys: no whole-trail figure is given as the place's).
|
that test (no keys: no whole-trail figure is given as the place's).
|
||||||
title_options(info, recs, site) -> titles, fullest first, for common.fit_title.
|
title_options(info, recs, site) -> titles, fullest first, for common.fit_title.
|
||||||
description(info, recs) -> str. box(info) -> [west, south, east, north] or None.
|
description(info, recs) -> str. box(info) -> [west, south, east, north] or None.
|
||||||
nav(info, records, recs, facet_pages) -> HTML after the rows and map."""
|
nav(info, records, recs, facet_pages) -> HTML after the rows and map.
|
||||||
|
in_name(info) -> str, where present: the name as it reads after "in" ("the Wind
|
||||||
|
River Range"); the plain name where the kind leaves it out."""
|
||||||
kicker: object
|
kicker: object
|
||||||
crumbs: object
|
crumbs: object
|
||||||
lede: object
|
lede: object
|
||||||
@@ -272,6 +274,7 @@ class PageKind:
|
|||||||
description: object
|
description: object
|
||||||
box: object
|
box: object
|
||||||
nav: object
|
nav: object
|
||||||
|
in_name: object = None
|
||||||
|
|
||||||
|
|
||||||
def _state_kicker(info):
|
def _state_kicker(info):
|
||||||
@@ -381,6 +384,7 @@ def place_page(info, records, site, kind, *, facet_pages=(), place_links=""):
|
|||||||
nav = kind.nav(info, records, recs, facet_pages)
|
nav = kind.nav(info, records, recs, facet_pages)
|
||||||
place = kind.facts_place(info)
|
place = kind.facts_place(info)
|
||||||
wholly = wholly_in(recs, place[1]) if kind.wholly else None
|
wholly = wholly_in(recs, place[1]) if kind.wholly else None
|
||||||
|
in_name = kind.in_name(info) if kind.in_name else info["name"]
|
||||||
body = f"""<div class="page-head"><div class="container">
|
body = f"""<div class="page-head"><div class="container">
|
||||||
<nav class="breadcrumb" aria-label="Breadcrumb"><ol>{crumbs}<li aria-current="page">{esc(info["name"])}</li></ol></nav>
|
<nav class="breadcrumb" aria-label="Breadcrumb"><ol>{crumbs}<li aria-current="page">{esc(info["name"])}</li></ol></nav>
|
||||||
<header class="titleblock"><div class="titleblock__text">
|
<header class="titleblock"><div class="titleblock__text">
|
||||||
@@ -390,9 +394,9 @@ def place_page(info, records, site, kind, *, facet_pages=(), place_links=""):
|
|||||||
</div></header>
|
</div></header>
|
||||||
</div></div>
|
</div></div>
|
||||||
<div class="container section place">
|
<div class="container section place">
|
||||||
{home.browse_view(home.listing(recs, top=2, where=kind.where, wholly=wholly), home.overview_map(recs, f"Map of the trails in {info['name']}", place=kind.box(info)),
|
{home.browse_view(home.listing(recs, top=2, where=kind.where, wholly=wholly), home.overview_map(recs, f"Map of the trails in {in_name}", place=kind.box(info)),
|
||||||
about=f"{summary.facts(recs, records, place=place)}{summary.details(recs)}",
|
about=f"{summary.facts(recs, records, place=place)}{summary.details(recs)}",
|
||||||
about_title=f"About the trails in {info['name']}")}
|
about_title=f"About the trails in {in_name}")}
|
||||||
{nav}{place_links}
|
{nav}{place_links}
|
||||||
</div>"""
|
</div>"""
|
||||||
title = common.fit_title(kind.title_options(info, recs, site), site)
|
title = common.fit_title(kind.title_options(info, recs, site), site)
|
||||||
|
|||||||
@@ -268,7 +268,9 @@ class BuiltVocabularyTest(unittest.TestCase):
|
|||||||
|
|
||||||
def browsing_pages(self):
|
def browsing_pages(self):
|
||||||
rels = ["index.html", "regions/index.html", "lists/index.html", "routes/index.html", "404.html"]
|
rels = ["index.html", "regions/index.html", "lists/index.html", "routes/index.html", "404.html"]
|
||||||
for d in ("regions", "lists"):
|
for d in ("regions", "lists", "parks", "ranges", "wilderness"):
|
||||||
|
if not os.path.isdir(os.path.join(OUT, d)):
|
||||||
|
continue
|
||||||
for name in sorted(os.listdir(os.path.join(OUT, d))):
|
for name in sorted(os.listdir(os.path.join(OUT, d))):
|
||||||
if os.path.isfile(os.path.join(OUT, d, name, "index.html")):
|
if os.path.isfile(os.path.join(OUT, d, name, "index.html")):
|
||||||
rels.append(f"{d}/{name}/index.html")
|
rels.append(f"{d}/{name}/index.html")
|
||||||
|
|||||||
@@ -0,0 +1,234 @@
|
|||||||
|
"""Park, range and wilderness pages (olt/placepages.py, through regions.place_page),
|
||||||
|
and their place in the built site: sitemap, structured data, navigation.
|
||||||
|
|
||||||
|
Fixtures only: small records with [[place]] entries and a small places.toml in a
|
||||||
|
temporary directory, never sample data in the real records."""
|
||||||
|
import html
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
sys.path.insert(0, WEB)
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
|
from olt import layout, pages, placepages, places, regions, seo, site # noqa: E402
|
||||||
|
from test_facets import BANNED, site_words # noqa: E402
|
||||||
|
from test_hike import fake # noqa: E402
|
||||||
|
|
||||||
|
S = site.Site("Long Hikes", "T", base_url="https://example.org", repo_url="https://github.com/o/r")
|
||||||
|
BOX = [-110.0, 42.5, -108.5, 43.5]
|
||||||
|
|
||||||
|
REGISTRY = [
|
||||||
|
{"kind": "range", "slug": "wind-river-range", "name": "Wind River Range", "where": ["WY"],
|
||||||
|
"source": "gmba-v2", "source_id": "1", "source_version": "2026-11-20", "box": BOX},
|
||||||
|
{"kind": "park", "slug": "yosemite", "name": "Yosemite National Park", "where": ["CA"],
|
||||||
|
"source": "nps", "source_id": "YOSE", "source_version": "2026-11-20",
|
||||||
|
"box": [-119.9, 37.5, -119.2, 38.2]},
|
||||||
|
{"kind": "park", "slug": "small", "name": "Small Park", "where": ["WY"],
|
||||||
|
"source": "nps", "source_id": "SMAL", "source_version": "2026-11-20", "box": BOX},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def write_registry(root, entries=REGISTRY):
|
||||||
|
lines = []
|
||||||
|
for e in entries:
|
||||||
|
box = ", ".join(str(v) for v in e["box"])
|
||||||
|
where = ", ".join(f'"{c}"' for c in e["where"])
|
||||||
|
within = f'\nwithin = [{", ".join(chr(34) + s + chr(34) for s in e["within"])}]' if e.get("within") else ""
|
||||||
|
lines.append(f'[[place]]\nslug = "{e["slug"]}"\nkind = "{e["kind"]}"\nname = "{e["name"]}"\n'
|
||||||
|
f'where = [{where}]{within}\nsource = "{e["source"]}"\nsource_id = "{e["source_id"]}"\n'
|
||||||
|
f'source_version = "{e["source_version"]}"\nbox = [{box}]\n')
|
||||||
|
with open(os.path.join(root, "places.toml"), "w", encoding="utf-8") as f:
|
||||||
|
f.write("\n".join(lines))
|
||||||
|
|
||||||
|
|
||||||
|
def rec(i, name, pls, *, km=120, regs=("WY",), country="US", line=True, inside_km=123.456, date=""):
|
||||||
|
r = fake(i, km=km, regions=list(regs), country=country, line=line,
|
||||||
|
hike={"shape": "point-to-point", "terrain": ["alpine"],
|
||||||
|
"summary": f"A walk called {name}.", "days": [7, 14]})
|
||||||
|
r["name"] = name
|
||||||
|
r["place"] = [{"kind": k, "slug": s, "basis": "track", "km": inside_km} for k, s in pls]
|
||||||
|
r["derived"]["bbox"] = [-109.5, 42.8, -109.0, 43.2]
|
||||||
|
if line:
|
||||||
|
r["derived"]["files"] = dict(r["derived"].get("files", {}), line="x.gpx")
|
||||||
|
if date:
|
||||||
|
r["history"] = [{"date": date}]
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def wind_records(n=10):
|
||||||
|
"""n trails in the Wind River Range, names out of order, half over the state line."""
|
||||||
|
out = []
|
||||||
|
for i in range(n, 0, -1):
|
||||||
|
out.append(rec(100 + i, f"Wind Trail {chr(64 + i)}", [("range", "wind-river-range")],
|
||||||
|
km=90 + i * 10, regs=("WY",) if i % 2 else ("CO",),
|
||||||
|
date="2026-11-18" if i == 3 else ""))
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def yosemite_records(n=5):
|
||||||
|
return [rec(200 + i, f"Yosemite Trail {chr(64 + i)}", [("park", "yosemite")],
|
||||||
|
km=80 + i * 15, regs=("CA",)) for i in range(n, 0, -1)]
|
||||||
|
|
||||||
|
|
||||||
|
def build(records):
|
||||||
|
with tempfile.TemporaryDirectory() as d:
|
||||||
|
write_registry(d)
|
||||||
|
return {b.path: b for b in placepages.build(records, S, d)}
|
||||||
|
|
||||||
|
|
||||||
|
def text(fragment):
|
||||||
|
return re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " ", str(fragment))))
|
||||||
|
|
||||||
|
|
||||||
|
class PlacePageTest(unittest.TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.records = wind_records() + yosemite_records()
|
||||||
|
cls.built = build(cls.records)
|
||||||
|
|
||||||
|
def test_a_place_with_three_trails_gets_a_page_and_one_with_two_does_not(self):
|
||||||
|
two = [rec(1, "One", [("park", "small")]), rec(2, "Two", [("park", "small")])]
|
||||||
|
self.assertEqual(build(two), {})
|
||||||
|
three = two + [rec(3, "Three", [("park", "small")])]
|
||||||
|
self.assertIn("/parks/small/", build(three))
|
||||||
|
self.assertIn("/parks/yosemite/", self.built)
|
||||||
|
self.assertIn("/ranges/wind-river-range/", self.built)
|
||||||
|
|
||||||
|
def test_the_page_has_the_filter_bar_rows_and_map_before_the_summary(self):
|
||||||
|
body = str(self.built["/ranges/wind-river-range/"].body)
|
||||||
|
# browse_view wraps the listing (filter bar, then rows) in browse__list
|
||||||
|
# beside browse__map, as on a state page; the summary follows both.
|
||||||
|
self.assertLess(body.index('class="finder__bar"'), body.index('<li class="entry"'))
|
||||||
|
self.assertLess(body.index('<li class="entry"'), body.index('class="browse-about"'))
|
||||||
|
self.assertLess(body.index('class="browse__map"'), body.index('class="browse-about"'))
|
||||||
|
self.assertIn('id="where"', body) # the Where filter: states and provinces, as elsewhere
|
||||||
|
|
||||||
|
def test_the_count_in_the_lede_the_title_and_the_rows_agree(self):
|
||||||
|
b = self.built["/ranges/wind-river-range/"]
|
||||||
|
body = str(b.body)
|
||||||
|
self.assertIn("10 Trails", b.title)
|
||||||
|
self.assertIn("10 trails in the Wind River Range", text(body))
|
||||||
|
self.assertEqual(body.count('<li class="entry"'), 10)
|
||||||
|
self.assertEqual(len(b.recs), 10)
|
||||||
|
|
||||||
|
def test_the_map_opens_on_the_registry_box(self):
|
||||||
|
body = str(self.built["/ranges/wind-river-range/"].body)
|
||||||
|
want = 'data-bbox="' + ",".join(f"{v:.4f}" for v in BOX) + '"'
|
||||||
|
self.assertIn(want, body)
|
||||||
|
|
||||||
|
def test_trails_are_in_plain_a_to_z_order(self):
|
||||||
|
body = str(self.built["/ranges/wind-river-range/"].body)
|
||||||
|
names = re.findall(r'data-name="([^"]*)"', body)
|
||||||
|
self.assertEqual(names, sorted(names))
|
||||||
|
self.assertEqual(names[0], "wind trail a")
|
||||||
|
self.assertNotIn("data-here", body) # no wholly-in lead, as the state pages have
|
||||||
|
|
||||||
|
def test_no_banned_word_and_no_internal_km(self):
|
||||||
|
for path_, b in self.built.items():
|
||||||
|
doc = layout.layout(b.title, b.body, path=path_, description=b.description, site=S,
|
||||||
|
jsonld=seo.collection_jsonld(path_, S, title=b.title,
|
||||||
|
description=b.description, recs=b.recs))
|
||||||
|
for bad in ("123.456", "123.4", "123.5", "76.7", "76.8"):
|
||||||
|
self.assertNotIn(bad, doc, (path_, bad))
|
||||||
|
words_ = site_words(doc)
|
||||||
|
for pattern in BANNED:
|
||||||
|
self.assertIsNone(re.search(pattern, words_, re.I), (path_, pattern))
|
||||||
|
|
||||||
|
def test_the_title_follows_the_search_phrasing(self):
|
||||||
|
self.assertEqual(self.built["/ranges/wind-river-range/"].title,
|
||||||
|
"Long Hikes in the Wind River Range, Wyoming: 10 Trails")
|
||||||
|
self.assertEqual(self.built["/parks/yosemite/"].title,
|
||||||
|
"Long Hikes in Yosemite National Park: 5 Trails")
|
||||||
|
body = str(self.built["/ranges/wind-river-range/"].body)
|
||||||
|
self.assertIn("<h1>Wind River Range</h1>", body)
|
||||||
|
self.assertIn('<p class="kicker">Mountain range · Wyoming</p>', body)
|
||||||
|
park = str(self.built["/parks/yosemite/"].body)
|
||||||
|
self.assertIn("<h1>Yosemite National Park</h1>", park)
|
||||||
|
self.assertIn("5 trails in Yosemite National Park", text(park))
|
||||||
|
|
||||||
|
def test_the_breadcrumb_is_trails_places_name(self):
|
||||||
|
body = str(self.built["/ranges/wind-river-range/"].body)
|
||||||
|
crumbs = body[body.index('class="breadcrumb"'):body.index("</ol>")]
|
||||||
|
self.assertEqual(re.findall(r">([^<]+)</a>", crumbs), ["Trails", "Places"])
|
||||||
|
self.assertIn('<li aria-current="page">Wind River Range</li>', crumbs)
|
||||||
|
self.assertEqual(seo.visible_breadcrumbs(body),
|
||||||
|
[("Trails", "/"), ("Places", "/regions/"), ("Wind River Range", "")])
|
||||||
|
|
||||||
|
def test_the_structured_data_is_a_collection_of_the_trails(self):
|
||||||
|
b = self.built["/parks/yosemite/"]
|
||||||
|
nodes = seo.collection_jsonld(b.path, S, title=b.title, description=b.description, recs=b.recs)
|
||||||
|
page = nodes[0]
|
||||||
|
self.assertEqual(page["@type"], "CollectionPage")
|
||||||
|
listing = page["mainEntity"]
|
||||||
|
self.assertEqual(listing["@type"], "ItemList")
|
||||||
|
self.assertEqual(listing["numberOfItems"], 5)
|
||||||
|
self.assertEqual([i["url"] for i in listing["itemListElement"]],
|
||||||
|
[f"https://example.org/trails/{r['id']}/" for r in b.recs])
|
||||||
|
|
||||||
|
|
||||||
|
class BuiltPlacePagesTest(unittest.TestCase):
|
||||||
|
"""One synthetic render_site: the pages written, in the sitemap, Places current."""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.tmp = tempfile.TemporaryDirectory()
|
||||||
|
repo, out = os.path.join(cls.tmp.name, "repo"), os.path.join(cls.tmp.name, "out")
|
||||||
|
cls.out = out
|
||||||
|
cls.records = wind_records() + yosemite_records()
|
||||||
|
for r in cls.records:
|
||||||
|
os.makedirs(os.path.join(repo, r["id"]))
|
||||||
|
with open(os.path.join(repo, r["id"], "trail.toml"), "w", encoding="utf-8") as f:
|
||||||
|
f.write(f'id = "{r["id"]}"\n')
|
||||||
|
write_registry(repo)
|
||||||
|
pages.render_site(cls.records, out, repo, site=S)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
cls.tmp.cleanup()
|
||||||
|
|
||||||
|
def read(self, *parts):
|
||||||
|
with open(os.path.join(self.out, *parts), encoding="utf-8") as f:
|
||||||
|
return f.read()
|
||||||
|
|
||||||
|
def test_the_pages_are_written(self):
|
||||||
|
self.assertIn("Wind River Range", self.read("ranges", "wind-river-range", "index.html"))
|
||||||
|
self.assertIn("Yosemite National Park", self.read("parks", "yosemite", "index.html"))
|
||||||
|
self.assertFalse(os.path.exists(os.path.join(self.out, "parks", "small")))
|
||||||
|
|
||||||
|
def test_the_page_is_in_the_sitemap_with_the_newest_trail_date(self):
|
||||||
|
xml = self.read("sitemap.xml")
|
||||||
|
for p in ("/ranges/wind-river-range/", "/parks/yosemite/"):
|
||||||
|
self.assertIn(f"<loc>https://example.org{p}</loc>", xml, p)
|
||||||
|
row = next(ln for ln in xml.splitlines() if "/ranges/wind-river-range/" in ln)
|
||||||
|
self.assertIn("<lastmod>2026-11-18</lastmod>", row) # the newest of its trails
|
||||||
|
self.assertNotIn("/parks/small/", xml)
|
||||||
|
|
||||||
|
def test_places_is_the_current_nav_item_on_a_place_page(self):
|
||||||
|
doc = self.read("parks", "yosemite", "index.html")
|
||||||
|
nav = re.search(r'<nav class="site-nav"[^>]*>(.*?)</nav>', doc, re.S).group(1)
|
||||||
|
self.assertIn('<a href="/regions/" aria-current="true">Places</a>', nav)
|
||||||
|
|
||||||
|
def test_the_built_page_carries_the_collection_structured_data(self):
|
||||||
|
doc = self.read("ranges", "wind-river-range", "index.html")
|
||||||
|
m = re.search(r'<script type="application/ld\+json">(.*?)</script>', doc, re.S)
|
||||||
|
data = json.loads(m.group(1))
|
||||||
|
nodes = data.get("@graph", [data])
|
||||||
|
coll = next(n for n in nodes if n.get("@type") == "CollectionPage")
|
||||||
|
self.assertEqual(coll["mainEntity"]["numberOfItems"], 10)
|
||||||
|
crumbs = next(n for n in nodes if n.get("@type") == "BreadcrumbList")
|
||||||
|
self.assertEqual([i["name"] for i in crumbs["itemListElement"]],
|
||||||
|
["Trails", "Places", "Wind River Range"])
|
||||||
|
|
||||||
|
def test_the_state_pages_of_the_same_build_are_unchanged_in_shape(self):
|
||||||
|
# the golden test pins the exact bytes; here the same render still has them
|
||||||
|
doc = self.read("regions", "us-wy", "index.html")
|
||||||
|
self.assertIn("<h1>Wyoming</h1>", doc)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in new issue
Block a user