Give every state and province with no listed trail a page that says so
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
0870287fe5
commit
07da53721f
5 files changed
+168
-8
No files matched your search
+17
-4
@@ -102,10 +102,15 @@ def _opening(entries, site):
|
||||
return Markup(" ".join(parts))
|
||||
|
||||
|
||||
def left_out_page(entries, site, repo_root):
|
||||
"""(title, description, body) for /left-out/, from left-out.toml entries."""
|
||||
bread = Markup('<nav class="breadcrumb" aria-label="Breadcrumb"><ol>'
|
||||
'<li><a href="/about/">About</a></li>')
|
||||
def entries_for_place(entries, key):
|
||||
"""The entries whose regions include the place `key` ("US-HI", "CA-MB")."""
|
||||
return [e for e in entries
|
||||
if key in {words.region_key(str(c), e.get("country", "")) for c in e.get("regions", [])}]
|
||||
|
||||
|
||||
def groups_html(entries):
|
||||
"""The entries as the page shows them: a section for each reason, in REASON_ORDER, with the
|
||||
reason's heading and meaning over its rows. Used by /left-out/ and by the page of a place."""
|
||||
groups = ""
|
||||
for reason in REASON_ORDER:
|
||||
listed = [e for e in entries if e.get("reason") == reason]
|
||||
@@ -131,6 +136,14 @@ def left_out_page(entries, site, repo_root):
|
||||
<p>{esc(meaning)}</p></div>
|
||||
{pagekit.plain_list(items)}
|
||||
</section>"""
|
||||
return groups
|
||||
|
||||
|
||||
def left_out_page(entries, site, repo_root):
|
||||
"""(title, description, body) for /left-out/, from left-out.toml entries."""
|
||||
bread = Markup('<nav class="breadcrumb" aria-label="Breadcrumb"><ol>'
|
||||
'<li><a href="/about/">About</a></li>')
|
||||
groups = groups_html(entries)
|
||||
body = f"""<div class="page-head"><div class="container">{bread}<li aria-current="page">Considered and left out</li></ol></nav><header class="titleblock">
|
||||
<div class="titleblock__text"><h1>Considered and left out</h1>
|
||||
<p class="page-head__lede">Trails and routes that were looked at for this dataset and left out, and why.</p>
|
||||
|
||||
@@ -144,6 +144,13 @@ def render_site(records, out_dir, repo_root, site=None):
|
||||
path = f"/regions/{slug}/"
|
||||
region_paths.append(path)
|
||||
add(path, t, d, b, maps=needs_map, recs=by_slug[slug]["records"])
|
||||
# A place the site holds a box for and no trail is in: a page that says so, and what was left out there.
|
||||
left_out = leftout.load_entries(repo_root)
|
||||
for key in regions.empty_places(records):
|
||||
t, d, b = regions.empty_place_page(key, records, site, left_out)
|
||||
path = facets.place_path(key)
|
||||
region_paths.append(path)
|
||||
add(path, t, d, b)
|
||||
# The parts of the country (facets.PLACE_GROUPS), each a page of its own.
|
||||
for g, _, _ in facets.PLACE_GROUPS:
|
||||
built = regions.group_page(g, records, site)
|
||||
|
||||
+56
-2
@@ -6,7 +6,7 @@ The section is called Places; its pages keep their /regions/ URLs.
|
||||
"""
|
||||
import collections
|
||||
|
||||
from . import bounds, browse, common, facets, home, summary, words
|
||||
from . import bounds, browse, common, facets, home, leftout, summary, words
|
||||
from .markup import Markup, esc
|
||||
|
||||
|
||||
@@ -33,6 +33,55 @@ def collect_regions(records):
|
||||
return by_slug, across
|
||||
|
||||
|
||||
def empty_places(records):
|
||||
"""The keys ("US-HI", "CA-MB") of the states, provinces and territories the site holds a box for
|
||||
(place-bounds.toml) that no listed trail is in, by name. Each has a page that says so
|
||||
(empty_place_page); none is a choice of any filter, since none matches a trail."""
|
||||
by_slug, _ = collect_regions(records)
|
||||
return sorted((k for k in bounds.boxes() if k.lower() not in by_slug), key=facets.place_name)
|
||||
|
||||
|
||||
def empty_place_page(key, records, site, entries):
|
||||
"""(title, description, body) for /regions/<us-xx or ca-xx>/ of a place with no listed trail: the
|
||||
plain fact, what the left-out list holds for the place (entries: left-out.toml's, as the left-out
|
||||
page shows them), and the way to the whole list and the rules. No map: there is nothing to draw."""
|
||||
name = facets.place_name(key)
|
||||
country = words.COUNTRIES[key.split("-", 1)[0]]
|
||||
group = facets.GROUP_OF.get(key, "")
|
||||
group_name = facets.GROUP_NAME.get(group, "")
|
||||
crumbs = '<li><a href="/regions/">Places</a></li>'
|
||||
if group and group_records(records, group):
|
||||
crumbs += f'<li><a href="{esc(group_path(group))}">{esc(group_name)}</a></li>'
|
||||
here = leftout.entries_for_place(entries, key)
|
||||
n = len(here)
|
||||
if n:
|
||||
left = (f"{n} {'trail' if n == 1 else 'trails'} considered here "
|
||||
f"{'was' if n == 1 else 'were'} left out, with the reason for each below.")
|
||||
else:
|
||||
left = f"Nothing in {esc(name)} is on the left-out list either."
|
||||
body = f"""<div class="page-head"><div class="container">
|
||||
<nav class="breadcrumb" aria-label="Breadcrumb"><ol>{crumbs}<li aria-current="page">{esc(name)}</li></ol></nav>
|
||||
<header class="titleblock"><div class="titleblock__text">
|
||||
<p class="kicker">{esc(group_name + " · " if group_name else "")}{esc(country)}</p>
|
||||
<h1>{esc(name)}</h1>
|
||||
<p class="page-head__lede">No trail of 50 miles or more is listed in {esc(name)}.</p>
|
||||
</div></header>
|
||||
</div></div>
|
||||
<div class="container section place left-out">
|
||||
<p class="prose">{left} See everything <a href="{leftout.PATH}">considered and left out</a>, and
|
||||
<a href="/about/#how-trails-are-chosen">how trails are chosen</a>.</p>
|
||||
{leftout.groups_html(here)}
|
||||
</div>"""
|
||||
title = f"No Long Trails Listed in {name}"
|
||||
if n:
|
||||
description = (f"No trail of 50 miles or more is listed in {name}, {country}. "
|
||||
f"{n} {'trail' if n == 1 else 'trails'} considered there "
|
||||
f"{'was' if n == 1 else 'were'} left out, with the reason for each.")
|
||||
else:
|
||||
description = f"No trail of 50 miles or more is listed in {name}, {country}, and none considered there was left out."
|
||||
return title, common.cut_at_word(description, common.DESCRIPTION_LIMIT), Markup(body)
|
||||
|
||||
|
||||
def group_box(g, by_slug):
|
||||
"""The ground a group page opens on: the union of the boxes of its members that have trails,
|
||||
or None when one of them has no box (the map then opens on its trails)."""
|
||||
@@ -125,6 +174,11 @@ def regions_index_page(records, site):
|
||||
<p class="places__sum">{_plural(len(across))} that {"crosses" if len(across) == 1 else "cross"} the whole country rather than one province: {links}.
|
||||
{"It is" if len(across) == 1 else "They are"} counted in every Canadian group, and on no single province’s page.</p>
|
||||
</section>"""
|
||||
none = empty_places(records)
|
||||
foot = ""
|
||||
if none:
|
||||
links = ", ".join(f'<a href="{esc(facets.place_path(k))}">{esc(facets.place_name(k))}</a>' for k in none)
|
||||
foot = f'\n<div class="container"><p class="places__sum">Places with no listed trail: {links}.</p></div>'
|
||||
n_us = sum(1 for r in records if "US" in common.countries(r))
|
||||
n_ca = sum(1 for r in records if "CA" in common.countries(r))
|
||||
body = f"""<div class="page-head"><div class="container"><header class="titleblock">
|
||||
@@ -133,7 +187,7 @@ def regions_index_page(records, site):
|
||||
United States ({n_us}) and Canada ({n_ca}). A trail that crosses several states is on each one’s page.</p>
|
||||
</div></header>
|
||||
<nav class="lists-row" aria-label="Jump to a part of the country"><ul>{"".join(jump)}</ul></nav></div></div>
|
||||
<div class="container section places">{blocks}</div>"""
|
||||
<div class="container section places">{blocks}</div>{foot}"""
|
||||
description = (f"Where the {len(records)} long trails and routes are: {n_us} in the United States and {n_ca} in Canada, "
|
||||
f"by state and province, with the number in each.")
|
||||
return "Long Trails by State and Province: US and Canada", common.cut_at_word(description, common.DESCRIPTION_LIMIT), Markup(body)
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
"""A state or province with no listed trail has a page that says so (regions.empty_place_page)."""
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
ROOT = os.path.dirname(WEB)
|
||||
sys.path.insert(0, WEB)
|
||||
|
||||
from olt import bounds, facets, home, leftout, records, regions, seo, site # noqa: E402
|
||||
|
||||
|
||||
class EmptyPlaceTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.records = records.load_records(ROOT)
|
||||
cls.site = site.load()
|
||||
cls.entries = leftout.load_entries(ROOT)
|
||||
cls.by_slug, _ = regions.collect_regions(cls.records)
|
||||
|
||||
def test_every_place_with_a_box_and_no_trail_is_listed(self):
|
||||
keys = regions.empty_places(self.records)
|
||||
self.assertTrue(keys)
|
||||
for k in keys:
|
||||
self.assertIn(k, bounds.boxes())
|
||||
self.assertNotIn(k.lower(), self.by_slug)
|
||||
for k in bounds.boxes():
|
||||
self.assertEqual(k in keys, k.lower() not in self.by_slug)
|
||||
self.assertEqual(keys, sorted(keys, key=facets.place_name))
|
||||
|
||||
def test_a_place_with_left_out_entries_lists_them_with_their_reasons(self):
|
||||
fake = [{"id": "x-trail", "name": "X Trail", "country": "CA", "regions": ["MB"], "reason": "rail-trail-or-towpath",
|
||||
"note": "A rail trail.", "decided": "2026-11-01"}]
|
||||
title, desc, body = regions.empty_place_page("CA-MB", self.records, self.site, fake)
|
||||
body = str(body)
|
||||
self.assertEqual(title, "No Long Trails Listed in Manitoba")
|
||||
self.assertIn("No trail of 50 miles or more is listed in Manitoba.", body)
|
||||
self.assertIn("1 trail considered here was left out", body)
|
||||
self.assertIn("X Trail", body)
|
||||
self.assertIn("Rail trails and canal towpaths", body)
|
||||
self.assertIn('href="/left-out/"', body)
|
||||
self.assertIn('href="/about/#how-trails-are-chosen"', body)
|
||||
self.assertNotIn('data-map', body)
|
||||
self.assertIn("Manitoba, Canada", desc)
|
||||
self.assertIn("1 trail considered there was left out", desc)
|
||||
|
||||
def test_a_place_with_none_says_so(self):
|
||||
_, desc, body = regions.empty_place_page("US-HI", self.records, self.site, [])
|
||||
self.assertIn("Nothing in Hawaii is on the left-out list either.", str(body))
|
||||
self.assertIn("none considered there was left out", desc)
|
||||
|
||||
def test_the_real_entries_reach_their_place(self):
|
||||
for key in regions.empty_places(self.records):
|
||||
here = leftout.entries_for_place(self.entries, key)
|
||||
_, _, body = regions.empty_place_page(key, self.records, self.site, self.entries)
|
||||
for e in here:
|
||||
self.assertIn(e["name"], str(body))
|
||||
|
||||
def test_the_places_index_links_them_in_one_line(self):
|
||||
_, _, body = regions.regions_index_page(self.records, self.site)
|
||||
body = str(body)
|
||||
self.assertIn("Places with no listed trail: ", body)
|
||||
line = body[body.index("Places with no listed trail:"):]
|
||||
line = line[:line.index("</p>")]
|
||||
for key in regions.empty_places(self.records):
|
||||
self.assertIn(f'<a href="{facets.place_path(key)}">{facets.place_name(key)}</a>', line)
|
||||
|
||||
def test_they_are_in_no_filter_and_no_count(self):
|
||||
recs = self.records
|
||||
counts = home.counts(recs)
|
||||
for key in regions.empty_places(recs):
|
||||
self.assertFalse(counts.get("where:" + key))
|
||||
self.assertNotIn(f'value="{key}"', str(home.finder_form(recs, counts)))
|
||||
index = str(regions.regions_index_page(recs, self.site)[2])
|
||||
self.assertIn(f"The {len(recs)} trails by state and province", index)
|
||||
|
||||
def test_the_sitemap_names_them(self):
|
||||
paths = [facets.place_path(k) for k in regions.empty_places(self.records)]
|
||||
self.assertIn("/regions/us-hi/", paths)
|
||||
for p in paths:
|
||||
self.assertIn(p, seo.all_paths(self.records, paths))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -17,7 +17,7 @@ WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
ROOT = os.path.dirname(WEB)
|
||||
sys.path.insert(0, WEB)
|
||||
|
||||
from olt import home, layout, pages, records, site, trail, words # noqa: E402
|
||||
from olt import home, layout, pages, records, regions, site, trail, words # noqa: E402
|
||||
|
||||
OUT = os.path.join(WEB, ".cache", "test-site")
|
||||
|
||||
@@ -570,7 +570,8 @@ class RenderUnitTest(unittest.TestCase):
|
||||
# sources, logos, about, data, schema, changelog, left out, saved (no list holds MIN_SIZE trails),
|
||||
# and the two reference pages, the glossary and the method (the tables, authors, recent and
|
||||
# linked-trails pages need at least MIN_SIZE trails and are not built for one)
|
||||
self.assertEqual(n, 18)
|
||||
# and a page for each state and province with no listed trail (regions.empty_places)
|
||||
self.assertEqual(n, 18 + len(regions.empty_places([rec])))
|
||||
with open(os.path.join(out, "trails", "x", "index.html"), encoding="utf-8") as f:
|
||||
self.assertIn("A & B", f.read())
|
||||
with open(os.path.join(out, "index.html"), encoding="utf-8") as f:
|
||||
|
||||
Reference in new issue
Block a user