"""A page for one kind of trail in one state or province: /regions///. For every place and every list that has a `noun` (olt/browse.py), the page exists when the place has at least MIN_SIZE trails in the list, no more than MAX_SHARE of the place's trails are in it (else it would repeat the place page), and its trails are not nearly the same set as another such page of the same place. A new trail or a corrected record moves pages in and out with no change here. """ from . import bounds, browse, common, facets, home, regions, summary from .markup import Markup, esc SAME_SET = 0.8 # two pages of one place whose trails overlap by this share (of the smaller union) are one page def _jaccard(a, b): return len(a & b) / len(a | b) def plan(records): """[{slug, info, lst, recs, path}] for every page, in place then list order.""" shown = {lst.key for lst, _ in browse.shown(records)} by_slug, _ = regions.collect_regions(records) out = [] for slug in sorted(by_slug): info = by_slug[slug] here = info["records"] kept = [] for lst in browse.facet_lists(): if lst.key not in shown: continue recs = browse.members(lst, here) if len(recs) < browse.MIN_SIZE or len(recs) > browse.MAX_SHARE * len(here): continue ids = {r["id"] for r in recs} if any(_jaccard(ids, other) >= SAME_SET for other in kept): continue kept.append(ids) out.append({"slug": slug, "info": info, "lst": lst, "recs": recs, "path": f"/regions/{slug}/{lst.key}/"}) return out def label(lst): """A short name for the page in a list of links.""" return lst.title def pages_by_place(pages): """slug -> [page], in plan order.""" out = {} for p in pages: out.setdefault(p["slug"], []).append(p) return out def pages_by_list(pages): out = {} for p in pages: out.setdefault(p["lst"].key, []).append(p) return out def places_nav(lst, pages): """HTML for a list's page: the places that have a page of their own for it.""" here = pages_by_list(pages).get(lst.key, []) if not here: return "" links = " · ".join(f'{esc(p["info"]["name"])} ({len(p["recs"])})' for p in sorted(here, key=lambda p: p["info"]["name"])) return (f'') def page(p, records, site, all_pages): """(title, description, body, needs_map) for one place-and-list page.""" lst, info, recs = p["lst"], p["info"], p["recs"] here = info["records"] name = info["name"] only_routes = all(facets.kind(r) == "route" for r in recs) count = f"{len(recs)} {'Routes' if only_routes else 'Trails'}" title = common.fit_title([f"{lst.noun} in {name}: {count}", f"{lst.noun} in {name}"], site) h1 = f"{lst.noun} in {name}" 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 in {esc(name)}{gpx}. " f'All the trails in {esc(name)}.') siblings = [q for q in pages_by_place(all_pages)[p["slug"]] if q is not p] others = [q for q in pages_by_list(all_pages)[lst.key] if q is not p] nav = "" if siblings: nav += (f'") if others: nav += (f'") crumbs = (f'
  • Trails
  • Places
  • ' f'
  • {esc(name)}
  • ') body = f"""

    {esc(name)}

    {esc(h1)}

    {lede}

    {home.browse_view(home.listing(recs, default_sort=lst.sort, top=2, where=""), home.overview_map(recs, f"Map of the trails in {name}: {lst.title}", place=bounds.place_box(info["key"])), about=summary.facts(recs, records), about_title=f"About these trails in {name}")} {nav}
    """ by_length = sorted(recs, key=lambda r: (r["length_km"], r["id"])) first = (f"{count} in {name}, from {common.plain_name(by_length[0])}, {regions._miles(by_length[0])} miles, " f"to {common.plain_name(by_length[-1])}, {regions._miles(by_length[-1])} miles.") desc = regions._fit([f"{lst.blurb} {first}", f"{lst.noun} in {name}: {first}"]) return title, desc, Markup(body), home.has_overview_map(recs)