Link the place pages from the hub, the states and the trails, and search by place
This commit is contained in:
1 parent
16c5275c94
commit
00c60a4c2c
10 files changed
+387
-35
No files matched your search
+49
-9
@@ -5,7 +5,8 @@ A row's words come in four tiers, strongest first, so that the list can put the
|
||||
0 name the name, its common name, its abbreviation and its id
|
||||
1 author who made a route
|
||||
2 places the full names of the states and provinces it lies in, the countries, the place it
|
||||
starts and the place it ends, and the land unit or managing body its record names
|
||||
starts and the place it ends, the land unit or managing body its record names, and
|
||||
the parks, mountain ranges and wilderness areas it is in (place_names)
|
||||
3 summary the trail's summary
|
||||
|
||||
Each tier is folded (fold) and written once per word (the summary's words of fewer than three letters are
|
||||
@@ -37,9 +38,47 @@ def fold(text):
|
||||
return _NOT_WORD.sub(" ", _APOSTROPHES.sub("", text)).strip()
|
||||
|
||||
|
||||
def place_names(rec):
|
||||
# {record id: [place name]} for every park, range and wilderness area a trail is in
|
||||
# (olt/places.py membership, parents included), set by pages.render_site through
|
||||
# use_place_names: typing "wind river" then finds the trails of the range, whether
|
||||
# or not the place has a page. Like home's author links, it is build state.
|
||||
_PLACE_NAMES = {}
|
||||
|
||||
|
||||
def use_place_names(mapping):
|
||||
_PLACE_NAMES.clear()
|
||||
_PLACE_NAMES.update({k: list(v) for k, v in (mapping or {}).items()})
|
||||
|
||||
|
||||
def _names_from_infos(rec, infos):
|
||||
"""The names of a record's places out of infos: the infos of places.collect (a dict
|
||||
keyed (kind, slug), or a list of them; their records already include the parents),
|
||||
or a registry in the same keying, whose names the record's [[place]] tables pick
|
||||
out, with every `within` parent. [] when infos is neither."""
|
||||
if isinstance(infos, dict) and infos and all(isinstance(k, tuple) for k in infos):
|
||||
if any(isinstance(v, dict) and "records" in v for v in infos.values()):
|
||||
return [v["name"] for v in infos.values()
|
||||
if any(r["id"] == rec["id"] for r in v.get("records") or [])]
|
||||
out, stack = [], [(p.get("kind"), p.get("slug")) for p in rec.get("place") or []
|
||||
if isinstance(p, dict)]
|
||||
seen = set()
|
||||
while stack:
|
||||
key = stack.pop(0)
|
||||
if key in seen or key not in infos:
|
||||
continue
|
||||
seen.add(key)
|
||||
out.append(infos[key].get("name", ""))
|
||||
stack += [("range", s) for s in infos[key].get("within") or []]
|
||||
return out
|
||||
return [i["name"] for i in infos or ()
|
||||
if isinstance(i, dict) and any(r["id"] == rec["id"] for r in i.get("records") or [])]
|
||||
|
||||
|
||||
def place_names(rec, infos=()):
|
||||
"""The names of the places a record carries: states, provinces and countries in full, where it
|
||||
starts and ends, and the land unit or body of its designation."""
|
||||
starts and ends, the land unit or body of its designation, and the parks, mountain
|
||||
ranges and wilderness areas it is in (from infos where given, else the names the
|
||||
build set with use_place_names)."""
|
||||
names = [facets.place_name(k) for k in facets.place_keys(rec)]
|
||||
names += [words.COUNTRIES.get(c, c) for c in common.countries(rec)]
|
||||
h = hike.table(rec)
|
||||
@@ -47,24 +86,25 @@ def place_names(rec):
|
||||
designation = rec.get("designation", "")
|
||||
if designation and not _NO_BODY.match(designation):
|
||||
names.append(designation)
|
||||
names += _names_from_infos(rec, infos) if infos else _PLACE_NAMES.get(rec["id"], [])
|
||||
return [n for n in names if isinstance(n, str) and n.strip()]
|
||||
|
||||
|
||||
def tier_texts(rec):
|
||||
def tier_texts(rec, infos=()):
|
||||
"""The four texts of a record, unfolded."""
|
||||
return [
|
||||
" ".join([rec["name"], rec.get("common_name", ""), rec.get("short_name", ""), rec["id"]]),
|
||||
facets.author(rec),
|
||||
" ".join(place_names(rec)),
|
||||
" ".join(place_names(rec, infos)),
|
||||
hike.table(rec).get("summary", ""),
|
||||
]
|
||||
|
||||
|
||||
def tiers(rec):
|
||||
def tiers(rec, infos=()):
|
||||
"""The four tiers as lists of words, each word in the strongest tier that has it."""
|
||||
seen = set()
|
||||
out = []
|
||||
for i, text in enumerate(tier_texts(rec)):
|
||||
for i, text in enumerate(tier_texts(rec, infos)):
|
||||
mine = []
|
||||
for word in fold(text).split():
|
||||
if i == SUMMARY and len(word) < SUMMARY_MIN:
|
||||
@@ -76,6 +116,6 @@ def tiers(rec):
|
||||
return out
|
||||
|
||||
|
||||
def attribute(rec):
|
||||
def attribute(rec, infos=()):
|
||||
"""The value of a row's data-search: the tiers' words, tiers joined with "|"."""
|
||||
return "|".join(" ".join(t) for t in tiers(rec))
|
||||
return "|".join(" ".join(t) for t in tiers(rec, infos))
|
||||
Reference in new issue
Block a user