From e3cb071968c1ba0575e0319fdb5c71d90686dcad Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Fri, 20 Nov 2026 18:43:20 -0700 Subject: [PATCH] Give every place-plus-list page one title pattern, the list's short noun then the place's preposition and name then the count, with the heading in the same words, and pin it with a test in place of the one that allowed 25 titles over --- web/olt/placefacets.py | 35 +++++++++++++++++++++------- web/tests/test_place_facets.py | 42 ++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/web/olt/placefacets.py b/web/olt/placefacets.py index 39b7a33..c14fa9b 100644 --- a/web/olt/placefacets.py +++ b/web/olt/placefacets.py @@ -95,6 +95,15 @@ def key_of(p): return p["slug"] if p["kind"] == "state" else (p["kind"], p["slug"]) +# The short noun a place page's title and heading use for each list, the same whatever the place's name: +# the list's own short noun, but for these. +PLACE_NOUN = {"a-week-or-less": "Trips of a Week or Less"} + + +def place_noun(lst): + return PLACE_NOUN.get(lst.key) or lst.short or lst.noun + + def label(lst): """A short name for the page in a list of links.""" return lst.title @@ -148,15 +157,25 @@ def page(p, records, site, all_pages): phrase = kind.phrase(info) if kind.phrase else f"in {in_name}" title_name = name if state else info["title_name"] prep = "in" if state else places_mod.prep_of(info) - only_routes = all(facets.kind(r) == "route" for r in recs) - count = f"{len(recs)} {'Routes' if only_routes else 'Trails'}" - short = lst.short or lst.noun - # The state goes first, then the long noun; the count is never dropped (a title that still - # does not fit, where the place's own name is long, keeps its count and goes over). - ladder = [f"{lst.noun} {prep} {title_name}: {count}", f"{lst.noun} {prep} {name}: {count}", - f"{short} {prep} {title_name}: {count}", f"{short} {prep} {name}: {count}"] + if state: + only_routes = all(facets.kind(r) == "route" for r in recs) + count = f"{len(recs)} {'Routes' if only_routes else 'Trails'}" + short = lst.short or lst.noun + # The state goes first, then the long noun; the count is never dropped (a title that still + # does not fit, where the place's own name is long, keeps its count and goes over). + ladder = [f"{lst.noun} {prep} {title_name}: {count}", f"{lst.noun} {prep} {name}: {count}", + f"{short} {prep} {title_name}: {count}", f"{short} {prep} {name}: {count}"] + h1 = f"{lst.noun} {phrase}" + else: + # One fixed pattern: : Trails (Routes for + # the routes list). The state follows a place in one state when the title still fits; a long + # place name runs long and keeps its count. The heading is the title. + count = f"{len(recs)} {'Routes' if lst.key == 'routes' else 'Trails'}" + noun = place_noun(lst) + ladder = [f"{noun} {prep} {title_name}: {count}", f"{noun} {prep} {name}: {count}"] title = common.fit_title(ladder, site) - h1 = f"{lst.noun} {phrase}" + if not state: + h1 = title n_gpx = sum(1 for r in recs if facets.gpx_here(r)) gpx = f", {n_gpx} with a GPX here" if n_gpx else "" lede = (f"{esc(lst.blurb)} {len(recs)} of the {len(here)} trails {esc(phrase)}{gpx}. " diff --git a/web/tests/test_place_facets.py b/web/tests/test_place_facets.py index 66e86e3..6a3eaef 100644 --- a/web/tests/test_place_facets.py +++ b/web/tests/test_place_facets.py @@ -212,9 +212,8 @@ class PageTest(unittest.TestCase): self.text = text(self.body) def test_title_h1_and_lede_use_the_list_noun_and_the_places_own_words(self): - noun = self.p["lst"].noun - self.assertRegex(self.title, rf"^{noun} in the Wind River Range(, Wyoming)?: 3 Trails$") - self.assertIn(f"

{noun} in the Wind River Range

", str(self.body)) + self.assertRegex(self.title, r"^Loops in the Wind River Range(, Wyoming)?: 3 Trails$") + self.assertIn(f"

{self.title}

", str(self.body)) self.assertIn("3 of the 7 trails in the Wind River Range", self.text) self.assertLessEqual(len(self.title), common.TITLE_LIMIT) @@ -279,19 +278,34 @@ class RealDataTest(unittest.TestCase): def test_the_number_of_pages_stays_reasonable(self): self.assertLess(len(self.plan), 250) - def test_every_title_fits_and_keeps_its_count_and_is_unique(self): - titles, over = set(), [] + def test_every_title_follows_the_one_pattern_and_the_heading_has_the_same_words(self): + titles = set() for p in self.plan: - t = placefacets.page(p, self.records, S, self.plan)[0] - self.assertRegex(t, r": \d+ (Trails|Routes)$", t) - titles.add(t) - if len(t) > common.TITLE_LIMIT: - over.append(t) + title, _, body, _ = placefacets.page(p, self.records, S, self.plan) + info = p["info"] + noun = placefacets.place_noun(p["lst"]) + count = f"{len(p['recs'])} {'Routes' if p['lst'].key == 'routes' else 'Trails'}" + prep = places.prep_of(info) + with_state = f"{noun} {prep} {info['title_name']}: {count}" + without = f"{noun} {prep} {info['name']}: {count}" + expect = with_state if len(with_state) <= common.title_room(S) else without + self.assertEqual(title, expect, p["path"]) + self.assertIn(f"

{title}

", str(body), p["path"]) + titles.add(title) self.assertEqual(len(titles), len(self.plan)) - # only a long place name pushes a title over, and then the shortest noun still keeps the count - self.assertLess(len(over), 25, over) - for t in over: - self.assertGreaterEqual(len(t.rsplit(": ", 1)[0].split(" in ", 1)[-1].split(" on ", 1)[-1]), 25, t) + + def test_the_nouns_are_the_fixed_short_ones(self): + got = {p["lst"].key: placefacets.place_noun(p["lst"]) for p in self.plan} + want = {"under-100-miles": "Trails Under 100 Miles", "100-to-200-miles": "100 to 200 Mile Trails", + "200-to-500-miles": "200 to 500 Mile Trails", "500-to-1000-miles": "500 to 1,000 Mile Trails", + "loops": "Loops", "routes": "Routes", "a-week-or-less": "Trips of a Week or Less", + "off-trail": "Cross-Country Hikes", "with-gpx": "Trails with a GPX"} + for key, noun in want.items(): + if key in got: + self.assertEqual(got[key], noun) + for key, noun in got.items(): + if key.startswith("terrain-"): + self.assertTrue(noun.endswith(" Trails"), noun) if __name__ == "__main__":