, 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("Regions", kicker="Browse", lede="Trails by state, province and territory.")
body += f'
...{esc(some_text)}...
'
html = layout("Regions", body, path="/regions/", description="Long trails by region.")
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 site as site_settings
from .markup import Markup, attrs, esc, json_block
NAV = (
("Trails", "/"),
("Regions", "/regions/"),
("Routes", "/routes/"),
("Sources", "/sources/"),
("Logos", "/logos/"),
("About", "/about/"),
("Data", "/data/"),
)
FONT_PRELOADS = (
"/assets/fonts/source-serif-4-latin.woff2",
"/assets/fonts/ibm-plex-sans-condensed-400-latin.woff2",
)
MAPLIBRE_CSS = "/assets/vendor/maplibre-gl/maplibre-gl.css"
MAPLIBRE_JS = "/assets/vendor/maplibre-gl/maplibre-gl.js"
def _current(path, href):
"""True when the nav item href is the section the page at path belongs to."""
if href == "/":
return path == "/" or path.startswith("/trails/")
return path.startswith(href)
def header(path, site):
items = "".join(
f'
')
return Markup(f"""""")
def _ld_json_block(obj):
text = json.dumps(obj, ensure_ascii=False, separators=(",", ":"))
text = text.replace("<", "\\u003c").replace(">", "\\u003e").replace("&", "\\u0026")
return Markup(f'')
def layout(title, body, *, path, description="", maps=False, scripts=(), data=None, site=None, jsonld=None, og_type="website"):
"""Return a complete HTML document.
title the page's own title ("Arizona National Scenic Trail"); the site
name is appended. Empty for the home page: ": ".
body the page content (HTML, Markup or str), placed inside .
path the page's site path, e.g. "/regions/" (canonical link, nav state).
description meta description; defaults to the site tagline.
maps True to load MapLibre (pages with a .mapfig[data-map]).
scripts extra same-origin script URLs, loaded with defer after app.js.
data optional object, embedded as ',
]
# app.js first: deferred scripts run in order, and the finder and the rest of the
# page must not wait for MapLibre (app.js starts the maps when MapLibre arrives).
head.append('')
if maps:
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
{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