, header and navigation, footer.
To add a page (see also docs in pages.py):
from olt.layout import layout, page_head, write_page
from olt.markup import esc
body = page_head("Places", lede="Trails by state and province.")
body += f'
...{esc(some_text)}...
'
html = layout("Places", body, path="/regions/", description="Long trails by place.")
write_page(out_dir, "/regions/", html)
`path` is the page's own site path; it sets the canonical link (when
site.toml has base_url) and marks the current item in the navigation.
Everything else on the page comes from style.css and app.js; no inline
scripts or styles (the Content-Security-Policy forbids them).
"""
import json
import os
from . import common, fingerprint, seo, site as site_settings
from .markup import Markup, attrs, esc, json_block
NAV = (
("Trails", "/"),
("Places", "/regions/"),
("Lists", "/lists/"),
("About", "/about/"),
)
# Pages with no header item of their own, and the item they sit under. A trail's page is part of
# Trails; the dataset pages are part of About and are linked from the About hub and the footer.
NAV_PARENT = {"/trails/": "/",
"/sources/": "/about/", "/logos/": "/about/", "/data/": "/about/",
"/left-out/": "/about/", "/method/": "/about/", "/glossary/": "/about/"}
# Under Lists: the lists (Routes among them), the tables and rankings, the authors and the views of
# what is new. A table that collections.toml names has an address of its own; olt/pages.py adds it
# to UNDER_LISTS as it writes the page.
LISTS_PREFIXES = ("/lists/", "/routes/", "/numbers/", "/authors/", "/recent/", "/connected/")
UNDER_LISTS = set()
FEED_PATH = "/feed.xml" # the Atom feed of the changelog (olt/feed.py)
# The fonts every page draws above the fold: the body face and the three weights of the
# condensed sans (labels, tiles, headings). A weight that arrives after the first paint swaps
# in with different letter widths, and the page shifts as the lines re-wrap (the home page's
# row of lists moved by 0.15 before the 500 and 600 were here).
FONT_PRELOADS = (
"/assets/fonts/source-serif-4-latin.woff2",
"/assets/fonts/ibm-plex-sans-condensed-400-latin.woff2",
"/assets/fonts/ibm-plex-sans-condensed-500-latin.woff2",
"/assets/fonts/ibm-plex-sans-condensed-600-latin.woff2",
)
CONTOURS = "/assets/contours.svg"
MAPLIBRE_CSS = "/assets/vendor/maplibre-gl/maplibre-gl.css"
MAPLIBRE_JS = "/assets/vendor/maplibre-gl/maplibre-gl.js"
# The outdoor map of the trail pages: its style, named on each trail map figure
# (data-outdoor) for app.js to fetch, and the contour add-on (maplibre-contour),
# loaded with MapLibre on those pages only (layout(outdoor=True)).
OUTDOOR_STYLE = "/assets/map/outdoor.json"
CONTOUR_JS = "/assets/vendor/maplibre-contour/maplibre-contour.js"
def _current(path, href):
"""True when the nav item href is the section the page at path belongs to."""
if href == "/lists/":
return path.startswith(LISTS_PREFIXES) or path in UNDER_LISTS
if any(path.startswith(p) and parent == href for p, parent in NAV_PARENT.items()):
return True
return path == "/" if href == "/" else path.startswith(href)
SAVED_PATH = "/saved/"
THEMES = (("auto", "Auto"), ("light", "Light"), ("dark", "Dark"))
def prefs():
"""The reader's two display choices, at the right of the header on every page.
Units: a two-way switch, mi or km first (radios named "units", the only
units switch on the site; app.js keeps them in step and fires olt:units).
Theme: a small menu built from , its button showing the theme in
use (its icon, and its name where there is room); the menu holds the three
themes as radios named "theme" (app.js fires olt:theme). The icon and name
in the button follow , which prefs.js sets before first
paint, so the button is right from the start. Both need JavaScript and are
hidden without it (.js-only).
"""
icons = "".join(f'{common.ICON_THEME[k]}' for k, _ in THEMES)
now = "".join(f'{label}' for k, label in THEMES)
hint = {"auto": ', As your device is set'}
options = "".join(
f''
for k, label in THEMES)
return Markup(f"""
"
for text, href in NAV)
# The visitor's saved trails: app.js fills in the count. Saving needs
# JavaScript, so without it the link is not offered.
items += (f'
')
return Markup(f"""""")
def _ld_json_block(obj):
"""One JSON-LD script. "<", ">" and "&" are written as \\u escapes, so no text from a
record can close the script element or start a tag inside it."""
text = json.dumps(obj, ensure_ascii=False, separators=(",", ":"))
text = text.replace("<", "\\u003c").replace(">", "\\u003e").replace("&", "\\u0026")
return Markup(f'')
def structured_data(jsonld, body, path, site, noindex=False):
"""The page's JSON-LD as one ',
]
# The page's one script. A page with a map does not load MapLibre (200 KB of script, 65 KB
# of style) with the page: app.js loads these three files itself, a moment after the page has
# loaded and never on a scroll or a touch, from the addresses it is given here.
libs = {}
if maps:
libs = {"data-maplibre-css": MAPLIBRE_CSS, "data-maplibre-js": MAPLIBRE_JS}
if outdoor:
libs["data-contour-js"] = CONTOUR_JS
libs = "".join(f' {name}="{fingerprint.url(src)}"' for name, src in libs.items())
head.append(f'')
head += [f'' for s in scripts]
data_block = json_block(data, "page-data") if data is not None else ""
return f"""
{chr(10).join(head)}
Skip to content{skip_links(body)}
{header(path, site)}
{body}
{footer(site)}
{data_block}
"""
def page_head(title, *, kicker="", lede="", crumbs=()):
"""A plain page heading block: breadcrumb, kicker,
, lede. For simple pages.
crumbs is a sequence of (text, href) pairs leading to this page.
"""
crumb_html = ""
if crumbs:
items = "".join(f'
' for t, h in crumbs)
crumb_html = (f'')
kicker_html = f'
{esc(kicker)}
' if kicker else ""
lede_html = f'
{esc(lede)}
' if lede else ""
return Markup(f"""
{crumb_html}
{kicker_html}
{esc(title)}
{lede_html}
""")
def write_page(out_dir, path, document):
"""Write a page for a site path: "/" -> index.html, "/a/b/" -> a/b/index.html, "/404.html" -> 404.html."""
rel = path.lstrip("/")
if not rel or rel.endswith("/"):
rel += "index.html"
dest = os.path.join(out_dir, *rel.split("/"))
os.makedirs(os.path.dirname(dest), exist_ok=True)
with open(dest, "w", encoding="utf-8", newline="\n") as f:
f.write(document)
return dest