"""Pieces shared by the home page and the trail pages: record summaries and small components.""" import re from . import words from .markup import Markup, esc def trail_path(rec): return f"/trails/{rec['id']}/" def kind(rec): return rec["derived"]["kind"] def is_route(rec): return kind(rec) == "route" def has_line(rec): return rec["derived"]["has_line"] # A line whose measured length is further than this from the published length # (either way, in percent) needs a length_gap_note (docs/schema.md). LENGTH_LIMIT = 15.0 def line_differs(rec): """True when the record has a track, the track was measured, and the measured length differs from the published length by more than 15%. The one test of a partial track used by every page.""" v = rec.get("verification", {}) diff = v.get("difference_percent") return bool(has_line(rec) and v.get("verified") and diff is not None and abs(diff) > LENGTH_LIMIT) def line_short(rec): """line_differs, and the measured track is the shorter: the track is partial.""" return line_differs(rec) and rec["verification"]["difference_percent"] < 0 def partial_short_text(rec): """The short form for lists: "Track measures 1.2 mi (2.0 km) of 684 mi (1,100 km) published", in the reader's unit order (words.length). Markup.""" v = rec["verification"] word = "of" if v["difference_percent"] < 0 else "against" return Markup(f"Track measures {words.length(v['measured_km'], measured=True)} {word} " f"{words.length(rec['length_km'], mi=rec['derived']['length_mi'])} published") def name(rec): """The record's name as HTML text, marked with its language when it is not English (web/site.toml [name_lang], copied to derived.name_lang by pages.py). Every place a page shows a record's name as text goes through here.""" lang = rec.get("derived", {}).get("name_lang", "") text = esc(rec["name"]) return Markup(f'{text}') if lang else text def countries(rec): """Every country the record touches: its own, and the country of each of its regions. In "US", "CA" order.""" found = {rec["country"]} | {words.region_country(c) for c in rec.get("regions", [])} return [c for c in ("US", "CA") if c in found] + sorted(found - {"US", "CA", ""}) _OSM_CREDIT = re.compile(r"(? "2009"; "" when the designation has no year in brackets.""" m = re.search(r"\((\d{4})\)", rec.get("designation", "")) return m.group(1) if m else "" def short_source(rec): """A short name for where the included track came from, for lists.""" src = rec.get("source", {}) if src.get("osm_only"): name = "OpenStreetMap" else: name = re.split(r" \(|, | with ", src.get("line_source", ""))[0] if "OpenStreetMap" in src.get("attribution", "") and "OpenStreetMap" not in name: name += " and OpenStreetMap" return name def licence_label(rec): src = rec.get("source", {}) text = words.label(words.LICENCES, src.get("licence", "")) if "ODbL" in src.get("attribution", "") and src.get("licence") != "ODbL-1.0": text += " and ODbL 1.0" return text def rich(text): """Escape record text, setting `backticked` spans (Overpass queries and the like) as code.""" parts = esc(text).split("`") if len(parts) % 2 == 0: # unbalanced backticks: leave the text as it is return esc(text) return Markup("".join(f"{p}" if i % 2 else p for i, p in enumerate(parts))) def absent_reason_short(rec): """line_absent_reason without the opening "No line is included." or "No track is included." (the status already says so).""" reason = rec.get("source", {}).get("line_absent_reason", "") return re.sub(r"^No (?:line|track) is included\.\s*", "", reason) or reason def badges(rec, *, route_tag=True): """The badges a hiker needs on a row or a trail page: Route (linked to what a route is) and National Scenic Trail. An official trail has no badge for its kind. route_tag=False leaves the Route badge out (the Routes list, where every row is one).""" out = [] if route_tag and is_route(rec): out.append('Route') if rec.get("tier") == "national": out.append('National Scenic Trail') return Markup("".join(out)) def policy_chip(value): lab, short = words.label(words.POLICIES, value), words.meaning(words.POLICIES, value) return Markup(f'' f'Track policy: {esc(lab)}') def logo_img(rec, alt, eager=False): """The logo : the PNG thumbnail at its real size when there is one (olt/thumbs.py), else the file.""" logo = rec["derived"].get("logo_img") src, dims = (logo["src"], f' width="{logo["width"]}" height="{logo["height"]}"') if logo \ else (shield_url(rec), "") loading = "" if eager else ' loading="lazy"' return f'{esc(alt)}' # The site's own mark (assets/favicon.svg, its mountain) for a plate with no logo and no abbreviation to show. PLATE_MARK = Markup('' '') def plate(rec, *, size="", link=False, eager=False, alt=None): """The logo on a light plate, or a dashed plate when there is no logo file: the trail's abbreviation if it has one, else the site's mark. link: the logo links to the full-size file. eager: not lazy-loaded (the record page header). alt: the image's text; by default " logo" on a sized plate and "" on a list plate. A linked image with alt="" gets its link text from a visually hidden span instead. """ url = shield_url(rec) cls = f"plate plate--{size}" if size else "plate" if url: if alt is None: alt = f"{rec['name']} logo" if size else "" img = logo_img(rec, alt, eager) if link: label = "" if alt else f'Full-size logo: {name(rec)}' img = f'{img}{label}' return Markup(f'
{img}
') inner = esc(rec.get("short_name", "")) or PLATE_MARK return Markup(f'') # Map credits. The base map is OpenFreeMap on every map. The trail maps add # relief and contours drawn from the Terrain Tiles elevation data: their credit # is the one tools/elevation.py gives (ATTRIBUTION; test_pages checks the two # agree), plus Mexico, which the maps near the border show. BASE_MAP_CREDIT = Markup("OpenFreeMap, © OpenMapTiles, data © OpenStreetMap contributors.") ELEVATION_CREDIT = ( "Terrain Tiles, accessed from https://registry.opendata.aws/terrain-tiles. " "United States 3DEP (formerly NED) and global GMTED2010 and SRTM terrain data " "courtesy of the U.S. Geological Survey. Canada terrain data contains " "information licensed under the Open Government Licence - Canada. ArcticDEM " "terrain data DEM(s) were created from DigitalGlobe, Inc., imagery and funded " "under National Science Foundation awards 1043681, 1559691, and 1542736. " "Global ETOPO1 terrain data U.S. National Oceanic and Atmospheric " "Administration." ) MAP_ELEVATION_CREDIT = ELEVATION_CREDIT + " Mexico terrain data source: INEGI, Continental relief, 2016." # The trail maps' own caption line: the full credit of everything they can draw. TRAIL_MAP_CREDIT = Markup(f"Base map: {BASE_MAP_CREDIT} Relief and contours: {esc(MAP_ELEVATION_CREDIT)}") # The site footer's short form; the elevation figures on every list come from the same data. FOOTER_MAP_CREDIT = Markup( f"Base maps: {BASE_MAP_CREDIT} Elevation, relief and contours: Terrain Tiles, with data courtesy of " "the U.S. Geological Survey, NOAA, ArcticDEM and INEGI; Canada terrain data under the Open Government " "Licence - Canada. The full credit is under each trail map.") def save_button(rec, *, row=True): """The control that saves a trail to the visitor's saved trails, or takes it off. A real ') return Markup(f'') SWATCH_LINE = Markup('') SWATCH_ROUTE = Markup('') SWATCH_ALT = Markup('') SWATCH_NONE = Markup('') # A trail with no track, sketched from its start (and end) points only. SWATCH_SKETCH = Markup('') ICON_DOWNLOAD = Markup('') ICON_EXTERNAL = Markup('') # The mark of a saved trail. Its fill is set by style.css (outline when not # saved, filled when saved), so one drawing serves every state. ICON_STAR = Markup('') # The three themes, for the theme menu in the header: auto (half dark, as the # device decides), light (a sun) and dark (a moon). ICON_THEME = { "auto": Markup(''), "light": Markup(''), "dark": Markup(''), }