Put the closest in length first among the other trails of the same state on a trail page

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-19 13:27:01 -07:00
1 parent 07da53721f
commit 8f58103a45
2 files changed
+54 -2

No files matched your search

+9 -2
View File
@@ -4,6 +4,7 @@ The main sections say what the record says, in plain words. Every field of
the record, with its exact value, is in the table at the end of the page
(raw_record); the main flow carries no coded values.
"""
import math
import os
import re
import urllib.parse
@@ -1773,13 +1774,19 @@ def _onward_list(title, recs, more_href, more_text, heading_id, meta=None):
f'<h3 id="{heading_id}">{esc(title)}</h3><ul class="onward__list">{items}</ul>{more}</section>')
def nearest_length(rec, other):
"""A sort key for the trails of a place on a trail's page: the closest in length first (by ratio, so
300 and 600 miles are as far apart as 30 and 60), then by name."""
return (abs(math.log(other["length_km"] / rec["length_km"])), facets.sort_name(other))
RELATED_WHY = {"name": "same name", "named": "named here", "names": "mentions this"}
def onward(rec, records):
"""Ways onward at the foot of the page, all worked out from the records (olt/related.py):
trails tied to this one by name or by what the descriptions say; other trails in the
same state or province (up to three of them); trails like this one in length, terrain
same state or province (up to three of them), the closest in length first; trails like this one in length, terrain
and shape; and for a route, other routes by the same author."""
if not records:
return ""
@@ -1793,7 +1800,7 @@ def onward(rec, records):
for i, key in enumerate(k for k in facets.place_keys(rec) if k != facets.ACROSS_CANADA):
if i >= 3:
break
here = sorted((r for r in others if key in facets.place_keys(r)), key=lambda r: -r["length_km"])
here = sorted((r for r in others if key in facets.place_keys(r)), key=lambda r: nearest_length(rec, r))
if not here:
continue
name = facets.place_name(key)
+45
View File
@@ -0,0 +1,45 @@
"""On a trail page, the other trails of the same state come closest in length first."""
import math
import os
import re
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 facets, records, trail # noqa: E402
class OnwardOrderTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.recs = records.load_records(ROOT)
cls.by_id = {r["id"]: r for r in cls.recs}
def part(self, rec, key):
html = str(trail.onward(rec, self.recs))
start = html.index(f'id="onward-{key.lower()}"')
return html[start:html.index("</section>", start)]
def test_the_sierra_high_route_leads_with_trails_near_its_length(self):
rec = self.by_id["sierra-high-route"]
ids = re.findall(r'href="/trails/([^/"]+)/"', self.part(rec, "US-CA"))
self.assertEqual(len(ids), trail.ONWARD_MAX)
gaps = [abs(math.log(self.by_id[i]["length_km"] / rec["length_km"])) for i in ids]
self.assertEqual(gaps, sorted(gaps))
self.assertNotIn("great-western-loop", ids) # the 6,875 mile loop is no longer first
everyone = sorted((r for r in self.recs if r["id"] != rec["id"] and "US-CA" in facets.place_keys(r)),
key=lambda r: trail.nearest_length(rec, r))
self.assertEqual(ids, [r["id"] for r in everyone[:trail.ONWARD_MAX]])
def test_how_many_are_shown_is_unchanged(self):
for rec in self.recs[:40]:
html = str(trail.onward(rec, self.recs))
for ul in re.findall(r'<ul class="onward__list">(.*?)</ul>', html):
self.assertLessEqual(ul.count("<li>"), trail.ONWARD_MAX)
if __name__ == "__main__":
unittest.main()