Put the filters on every list and place page, and count their numbers from the page
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

The search and filters bar is on every list, place and the Routes
page with ten or more trails, scoped to that page's set and closed to
start. The number beside each choice is recounted from the page's rows
on every change; a choice that would show nothing is hidden; a filter
that cannot narrow the page is left out. The open panel sits in the
flow and pushes the rows down. Each part of the country has a page of
its own, and the links to the home page "with the filters" are gone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-17 12:46:08 -07:00
1 parent f467fcb560
commit 1ed0a68991
12 files changed
+906 -212

No files matched your search

+23 -3
View File
@@ -166,7 +166,7 @@ class ExtraPagesTest(unittest.TestCase):
home_doc = read_out("index.html")
by_slug, across = regions_mod.collect_regions(self.records)
# each state or province option's count is the number of trails on its place page
found = re.findall(r'<option value="((?:US|CA)-[A-Z]{2})">[^<(]+\((\d+)\)</option>', home_doc)
found = re.findall(r'<option value="((?:US|CA)-[A-Z]{2})" data-label="[^"]*">[^<(]+\((\d+)\)</option>', home_doc)
self.assertEqual({v for v, _ in found}, {i["key"] for i in by_slug.values()})
for value, n in found:
page = read_out(f"regions/{value.lower()}/index.html")
@@ -174,10 +174,10 @@ class ExtraPagesTest(unittest.TestCase):
# and the rows that say they are there
self.assertEqual(int(n), len(re.findall(rf'data-where="[^"]*\b{value}\b', home_doc)), value)
for country, label in (("US", "United States"), ("CA", "Canada")):
n = int(re.search(rf'<option value="{country}">{label} \((\d+)\)</option>', home_doc).group(1))
n = int(re.search(rf'<option value="{country}" data-label="{label}">{label} \((\d+)\)</option>', home_doc).group(1))
self.assertEqual(n, sum(1 for r in self.records if country in common.countries(r)), country)
for g, name, _ in facets.PLACE_GROUPS:
m = re.search(rf'<option value="g:{g}">All of {re.escape(name)} \((\d+)\)</option>', home_doc)
m = re.search(rf'<option value="g:{g}" data-label="All of {re.escape(name)}">All of {re.escape(name)} \((\d+)\)</option>', home_doc)
n = sum(1 for r in self.records if g in facets.place_groups(r))
self.assertEqual(int(m.group(1)) if m else 0, n, g)
# No trail spans a whole country now that the Trans Canada Trail is gone;
@@ -433,5 +433,25 @@ class SeoTest(unittest.TestCase):
self.assertIn("Sitemap: https://example.org/sitemap.xml", f.read())
class NoFilterInstructionsTest(unittest.TestCase):
"""The pages that show a set of trails have the filter bar themselves: no page sends a visitor to
the home page "with the filters", and each part of the country has its own page."""
def test_no_page_in_the_build_says_with_the_filters(self):
for base, _, names in os.walk(OUT):
for n in names:
if n.endswith(".html"):
with open(os.path.join(base, n), encoding="utf-8") as f:
text = f.read()
self.assertNotIn("with the filters", text, os.path.join(base, n))
self.assertNotIn('href="/?where=', text, os.path.join(base, n))
def test_each_part_of_the_country_has_a_page_with_the_bar(self):
doc = read_out("regions/central-canada/index.html")
self.assertIn('id="finder-form"', doc)
self.assertIn('href="/regions/central-canada/"', read_out("regions/index.html"))
if __name__ == "__main__":
unittest.main()
+5 -3
View File
@@ -208,7 +208,7 @@ class MissingValuesTest(unittest.TestCase):
def test_script_leaves_out_missing_values(self):
with open(os.path.join(WEB, "assets", "app.js"), encoding="utf-8") as f:
js = f.read()
self.assertIn("if (s.month && words(d.months).indexOf(s.month) === -1) return false;", js)
self.assertIn("if (skip !== 'month' && s.month && !has(d.months, s.month)) return false;", js)
self.assertIn("note.hidden = !on[note.dataset.missing]", js)
def test_clearing_returns_the_lengths_to_any(self):
@@ -216,14 +216,16 @@ class MissingValuesTest(unittest.TestCase):
# "Any" is the last option of the longest-length list, so both the page and
# the script (which rebuilds the lists in the reader's units) must mark it;
# in the shortest-length list it is first.
form = home.finder_form(self.recs(3, 8))
# (lengths that differ: a Length filter is written only where it can narrow the trails)
form = home.finder_form(self.recs(3, 8) + [fake(300, km=900)])
longest = re.search(r'<select[^>]* id="max"[^>]*>(.*?)</select>', form, re.S).group(1)
self.assertEqual(re.findall(r'<option value="([^"]*)" selected', longest), [""])
shortest = re.search(r'<select[^>]* id="min"[^>]*>(.*?)</select>', form, re.S).group(1)
self.assertTrue(shortest.startswith('<option value="">Any</option>'))
with open(os.path.join(WEB, "assets", "app.js"), encoding="utf-8") as f:
js = f.read()
self.assertIn("if (value === '') o.defaultSelected = true;", js)
self.assertIn("if (p.o.def) opt.defaultSelected = true;", js)
self.assertIn("{ value: '', label: 'Any', counted: false, group: '', def: true }", js)
# Words that must not appear on a browsing page (home, places, lists, Routes, and a
+336
View File
@@ -0,0 +1,336 @@
"""The filter bar on every page that shows a set of trails, and the numbers beside its choices.
The bar (olt/home.py listing(), finder_form()) is the home page's, on each list, place and the Routes
page whose set has at least home.FILTER_MIN trails, built from that page's own trails. app.js counts
the choices again from the rows on the page every time anything changes (FacetCount, between the
facet-counts markers); this file runs that counting under node, on the rows the home page writes,
and checks it against counts made here straight from the records. Pages are rendered one at a time;
the site is not built.
"""
import collections
import html
import json
import os
import re
import shutil
import subprocess
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)
sys.path.insert(0, os.path.join(ROOT, "tools"))
from olt import browse, facets, home, layout, leftout, regions, site, standing, views, words # noqa: E402
from olt import records as recordsmod # noqa: E402
SITE = site.load()
def read(*parts):
with open(os.path.join(*parts), encoding="utf-8") as f:
return f.read()
def form_of(body):
body = str(body)
if 'id="finder-form"' not in body:
return ""
return body[body.index('<form class="filters"'):body.index("</form>")]
def facets_of(body):
return re.findall(r'data-facet="(\w+)"', form_of(body))
def entry_ids(body):
return re.findall(r'<li class="entry" data-id="([^"]+)"', str(body))
class Pages:
_recs = None
@classmethod
def recs(cls):
if Pages._recs is None:
Pages._recs = recordsmod.load_records(ROOT)
return Pages._recs
class WhichPagesHaveTheBarTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.records = Pages.recs()
cls.shown = dict((lst.key, recs) for lst, recs in browse.shown(cls.records))
def list_body(self, key):
lst = browse.by_key(key)
return regions.list_page(lst, self.shown[key], len(self.records), SITE, records=self.records)[2]
def test_a_list_page_a_place_page_a_part_of_the_country_and_routes_have_it(self):
self.assertGreaterEqual(len(self.shown["loops"]), home.FILTER_MIN)
self.assertTrue(form_of(self.list_body("loops")))
by_slug, _ = regions.collect_regions(self.records)
self.assertGreaterEqual(len(by_slug["ca-on"]["records"]), home.FILTER_MIN)
self.assertTrue(form_of(regions.region_page("ca-on", self.records, SITE)[2]))
self.assertTrue(form_of(regions.group_page("central-canada", self.records, SITE)[2]))
self.assertTrue(form_of(regions.routes_page(self.records, SITE)[2]))
def test_a_set_under_the_threshold_has_the_plain_rows(self):
small = [k for k, recs in self.shown.items() if len(recs) < home.FILTER_MIN and k != "routes"]
self.assertTrue(small)
for key in small:
body = self.list_body(key)
self.assertFalse(form_of(body), key)
self.assertNotIn("data-finder", str(body), key)
def test_the_tables_rankings_newest_authors_and_left_out_pages_do_not(self):
bodies = {}
for spec in standing.RANKINGS:
built = standing.ranking_page(spec, self.records, SITE)
if built:
bodies[spec[0]] = built[2]
from olt import datafiles
for c in datafiles.collections(ROOT):
built = standing.collection_page(c, self.records, SITE)
if built:
bodies[c["name"]] = built[2]
built = views.recent_page(self.records, SITE)
if built:
bodies["recent"] = built[2]
from olt import authors
groups = authors.author_pages(self.records, ROOT)
bodies["authors"] = authors.index_page(self.records, SITE, ROOT)[2]
big = max(groups, key=lambda g: len(g["routes"]))
bodies["author"] = authors.author_page(big, self.records, SITE, groups)[2]
bodies["left-out"] = leftout.left_out_page(leftout.load_entries(ROOT), SITE, ROOT)[2]
self.assertGreater(len(bodies), 4)
for name, body in bodies.items():
self.assertNotIn('id="finder-form"', str(body), name)
self.assertNotIn("data-finder", str(body), name)
def test_the_saved_page_has_no_bar(self):
from olt import saved
self.assertNotIn('id="finder-form"', str(saved.saved_page(self.records, SITE)[2]))
def test_one_bar_one_script(self):
# every page's bar is the same form, which app.js finds by the one id
body = str(self.list_body("loops"))
self.assertEqual(body.count('id="finder-form"'), 1)
self.assertIn('<div class="results" data-finder', body)
js = read(WEB, "assets", "app.js")
self.assertIn("document.querySelector('.results[data-finder]')", js)
self.assertEqual(js.count("function initFinder("), 1)
class FacetsFitThePageTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.records = Pages.recs()
cls.shown = dict((lst.key, recs) for lst, recs in browse.shown(cls.records))
def test_loops_have_no_shape_filter_and_routes_no_kind(self):
lst = browse.by_key("loops")
body = regions.list_page(lst, self.shown["loops"], len(self.records), SITE, records=self.records)[2]
self.assertNotIn("shape", facets_of(body))
self.assertIn("where", facets_of(body))
self.assertNotIn("kind", facets_of(regions.routes_page(self.records, SITE)[2]))
def test_a_states_page_has_no_where(self):
body = regions.region_page("ca-on", self.records, SITE)[2]
self.assertNotIn("where", facets_of(body))
self.assertNotIn('name="where"', form_of(body))
def test_a_part_of_the_country_offers_itself_first_then_only_its_own_places(self):
body = regions.group_page("central-canada", self.records, SITE)[2]
recs = regions.group_records(self.records, "central-canada")
where = re.search(r'<select class="select" id="where" name="where">(.*?)</select>', form_of(body), re.S).group(1)
options = re.findall(r'<option value="([^"]*)" data-label="([^"]*)">([^<]*)</option>', where)
self.assertEqual(options[0], ("", "All of Central Canada", f"All of Central Canada ({len(recs)})"))
self.assertNotIn("<optgroup", where)
allowed = {"CA-ON", "CA-QC", facets.ACROSS_CANADA}
self.assertTrue(options[1:])
for value, label, text in options[1:]:
self.assertIn(value, allowed)
n = sum(1 for r in recs if value in facets.where_tokens(r))
self.assertEqual(text, f"{label} ({n})")
def test_a_facet_that_cannot_narrow_is_left_out(self):
recs = [r for r in self.records if facets.shape(r) == "loop"]
form = home.finder_form(recs)
self.assertNotIn('data-facet="shape"', form)
# and one choice that holds every trail is no choice
only_us = [r for r in self.records if common_countries(r) == ["US"]][:20]
self.assertNotIn('value="US"', home.finder_form(only_us))
def test_no_choice_is_written_that_would_show_nothing(self):
body = regions.group_page("central-canada", self.records, SITE)[2]
recs = regions.group_records(self.records, "central-canada")
form = form_of(body)
self.assertNotRegex(form, r'class="check__count">0<')
have = {t for r in recs for t in facets.terrain(r)}
written = set(re.findall(r'name="terrain" value="([^"]+)"', form))
self.assertTrue(written)
self.assertTrue(written <= have, written - have)
self.assertNotIn("desert", written)
def common_countries(rec):
from olt import common
return common.countries(rec)
class AListPageCarriesItsOwnTrailsOnlyTest(unittest.TestCase):
def test_rows_and_map_are_the_lists_own(self):
records = Pages.recs()
lst = browse.by_key("loops")
recs = browse.members(lst, records)
body = str(regions.list_page(lst, recs, len(records), SITE, records=records)[2])
own = {r["id"] for r in recs}
self.assertEqual(set(entry_ids(body)), own)
self.assertNotIn("data-every-trail", body)
ids = re.search(r'data-map="overview"[^>]* data-ids="([^"]*)"', body).group(1).split()
self.assertTrue(set(ids) <= own)
def test_the_canonical_address_is_the_plain_page(self):
doc = layout.layout("Loops", "<p>x</p>", path="/lists/loops/", site=SITE)
canon = re.search(r'<link rel="canonical" href="([^"]+)"', doc)
if canon:
self.assertTrue(canon.group(1).endswith("/lists/loops/"))
self.assertNotIn("?", canon.group(1))
class NoFilterLinksTest(unittest.TestCase):
def test_no_page_sends_a_visitor_to_the_home_page_with_a_filter_set(self):
for name in os.listdir(os.path.join(WEB, "olt")):
if name.endswith(".py"):
text = read(WEB, "olt", name)
self.assertNotIn("with the filters", text, name)
self.assertNotIn('href="/?where', text, name)
self.assertNotIn('href="/?kind', text, name)
def test_the_places_index_links_each_part_to_its_page(self):
body = str(regions.regions_index_page(Pages.recs(), SITE)[2])
self.assertIn('<a href="/regions/central-canada/">All 15 trails in Central Canada</a>'.replace(
"15", str(len(regions.group_records(Pages.recs(), "central-canada")))), body)
# Counting, as app.js does it, run under node ------------------------------------------------------
NODE = shutil.which("node")
def facet_count_source():
js = read(WEB, "assets", "app.js")
start = js.index("/* facet-counts:begin")
end = js.index("/* facet-counts:end */")
return js[start:end]
def rows_of(body):
"""The rows' data, as app.js reads it from each row's data-* attributes."""
out = []
for attrs in re.findall(r'<li class="entry"([^>]*)>', str(body)):
d = {k: html.unescape(v) for k, v in re.findall(r' data-([a-z]+)="([^"]*)"', attrs)}
split = lambda v: [x for x in v.split(" ") if x] # noqa: E731
out.append({"id": d["id"], "search": d["search"], "where": split(d["where"]), "km": float(d["km"]),
"time": d["time"], "months": split(d["months"]), "terrain": split(d["terrain"]),
"kind": d["kind"], "walking": d["walking"], "shape": d["shape"], "gpx": d["gpx"]})
return out
def state(**kw):
s = {"words": [], "where": "", "min": 0, "max": None, "month": "", "kind": "",
"time": [], "terrain": [], "walking": [], "shape": [], "gpx": []}
s.update(kw)
return s
def run_counts(rows, s, steps=()):
"""FacetCount.count(rows, s, steps) under node. max None stands for no longest length."""
prog = facet_count_source() + """
var input = JSON.parse(require('fs').readFileSync(0, 'utf8'));
if (input.s.max === null) input.s.max = Infinity;
process.stdout.write(JSON.stringify(FacetCount.count(input.rows, input.s, input.steps)));
"""
out = subprocess.run([NODE, "-e", prog], input=json.dumps({"rows": rows, "s": s, "steps": list(steps)}),
capture_output=True, text=True, check=True)
return json.loads(out.stdout)
@unittest.skipUnless(NODE, "node is not installed")
class CountsAreTrueTest(unittest.TestCase):
"""The maintainer's case: the home page at ?where=g:central-canada&time=week."""
@classmethod
def setUpClass(cls):
cls.records = Pages.recs()
cls.rows = rows_of(home.render(cls.records, SITE, ROOT))
cls.cc = [r for r in cls.records if "central-canada" in facets.place_groups(r)]
def test_the_home_page_rows_are_every_trail(self):
self.assertEqual(len(self.rows), len(self.records))
def test_with_central_canada_chosen_time_and_terrain_count_its_trails_only(self):
c = run_counts(self.rows, state(where="g:central-canada"))
self.assertEqual(c["total"], len(self.cc))
self.assertEqual(c["time"], dict(collections.Counter(facets.time_band(r) for r in self.cc)))
self.assertEqual(sum(c["time"].values()), len(self.cc))
self.assertEqual(c["terrain"], dict(collections.Counter(t for r in self.cc for t in facets.terrain(r))))
# every number is of trails that are there: nothing counts zero, and nothing absent is counted
self.assertTrue(all(n > 0 for n in c["terrain"].values()))
self.assertTrue(all(n > 0 for n in c["time"].values()))
def test_with_a_week_or_less_also_chosen(self):
c = run_counts(self.rows, state(where="g:central-canada", time=["week"]))
week = [r for r in self.cc if facets.time_band(r) == "week"]
self.assertEqual(c["total"], len(week))
# Time is counted against Where only, not against its own choice: the bands add up to the place
self.assertEqual(c["time"], dict(collections.Counter(facets.time_band(r) for r in self.cc)))
self.assertEqual(c["base"]["time"], len(self.cc))
# Terrain against both
self.assertEqual(c["terrain"], dict(collections.Counter(t for r in week for t in facets.terrain(r))))
# Where against the time only: "All of Central Canada" says how many of it take a week or less
everywhere_week = [r for r in self.records if facets.time_band(r) == "week"]
self.assertEqual(c["where"]["g:central-canada"], len(week))
self.assertEqual(c["base"]["where"], len(everywhere_week))
self.assertEqual(c["where"]["CA-ON"], sum(1 for r in everywhere_week if "CA-ON" in facets.place_keys(r)))
def test_length_steps_count_from_the_chosen_bounds(self):
steps = [[f"{n}mi", n * words.KM_PER_MI] for n in (100, 200, 500)]
c = run_counts(self.rows, state(where="g:central-canada", max=500 * words.KM_PER_MI), steps)
for value, km in steps:
self.assertEqual(c["min"][value], sum(1 for r in self.cc if km <= r["length_km"] <= 500 * words.KM_PER_MI))
self.assertEqual(c["max"][value], sum(1 for r in self.cc if r["length_km"] <= km))
def test_search_counts_too(self):
c = run_counts(self.rows, state(words=["trail"], kind="route"))
want = [r for r in self.rows if "trail" in r["search"] and r["kind"] == "route"]
self.assertEqual(c["total"], len(want))
self.assertEqual(c["kind"]["trail"], sum(1 for r in self.rows if "trail" in r["search"] and r["kind"] == "trail"))
def test_the_script_takes_out_what_would_show_nothing(self):
js = read(WEB, "assets", "app.js")
self.assertIn("if (o.value && k === 0 && o.value !== chosen) return;", js)
self.assertIn("label.hidden = n === 0 && !b.checked;", js)
self.assertIn("field.hidden = !narrows && !isSet(f, s);", js)
# the counts are made on every change, from the rows on the page
self.assertIn("var c = FacetCount.count(rows, s, lengthSteps());", js)
class PanelInTheFlowTest(unittest.TestCase):
def test_the_open_panel_is_not_laid_over_the_rows(self):
css = read(WEB, "assets", "style.css")
rule = re.search(r"\n\.filters__panels \{[^}]*\}", css).group(0)
self.assertNotIn("position: absolute", rule)
self.assertNotRegex(css, r"html\.js \.filters__panels \{[^}]*position: absolute")
def test_the_panel_starts_closed_whatever_the_address_says(self):
js = read(WEB, "assets", "app.js")
self.assertNotIn("if (firstOn", js)
self.assertIn("if (window.location.hash === '#filters') showPanels(true);", js)
if __name__ == "__main__":
unittest.main()
+2 -2
View File
@@ -91,9 +91,9 @@ class HomePageTest(unittest.TestCase):
self.assertLess(grid.index('id="finder-form"'), grid.index('id="entries"'))
self.assertNotIn("filters__maplink", self.body)
def test_the_open_filters_lie_over_the_rows_and_can_be_closed(self):
def test_the_open_filters_sit_between_the_bar_and_the_rows_and_can_be_closed(self):
css = read(WEB, "assets", "style.css")
self.assertRegex(css, r"html\.js \.filters__panels \{[^}]*position: absolute;")
self.assertNotRegex(css, r"\.filters__panels \{[^}]*position: absolute;")
self.assertIn('class="button filters__done"', self.body)
js = read(WEB, "assets", "app.js")
self.assertIn("e.key === 'Escape'", js)
+2 -2
View File
@@ -566,11 +566,11 @@ class RenderUnitTest(unittest.TestCase):
f.write('id = "x"\n')
rec = sample_record(name="A & B")
n = pages.render_site([rec], out, repo, site=site.Site("N", "T"))
# trail page + home + 404 + places hub + 1 place + lists hub + routes,
# trail page + home + 404 + places hub + 1 place + its part of the country + lists hub + routes,
# 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, 17)
self.assertEqual(n, 18)
with open(os.path.join(out, "trails", "x", "index.html"), encoding="utf-8") as f:
self.assertIn("A &amp; B", f.read())
with open(os.path.join(out, "index.html"), encoding="utf-8") as f: