"""Small pieces the new view pages share: a page heading with a breadcrumb, a table, a ranked row.""" from . import words from .markup import Markup, esc def head(h1, lede_html, crumbs, *, kicker=""): """The page-head block. crumbs: [(text, href)] leading to this page; lede_html is HTML.""" items = "".join(f'
  • {esc(t)}
  • ' for t, h in crumbs) kick = f'

    {esc(kicker)}

    ' if kicker else "" return f"""
    {kick}

    {esc(h1)}

    {lede_html}

    """ # A table with at least this many columns is set as one block per row on a phone (style.css). STACK_COLUMNS = 4 def table(headers, rows, *, caption="", numeric=(), row_heads=False, compare=False): """A table in the site's table wrapper. headers: column titles; rows: lists of HTML cells (Markup, or text that is escaped); numeric: column numbers set at the end of the cell; row_heads: the first cell of each row is a row heading. Every cell carries its label (data-label: its column's title, or in a compare table its row's heading) so that a phone can set the same markup as blocks, with no sideways scrolling (style.css): one block per row where the table has STACK_COLUMNS columns or more, and compare: a table of figures down the side and one trail in each column, with a fixed layout of equal columns, and on a phone one block per trail, headed by the trail's name.""" def cell(i, value, label, head=False): text = value if isinstance(value, Markup) else esc(value) cls = ' class="num"' if i in numeric else "" if head: return f'{text}' return f'{text}' cap = f'{esc(caption)}' if caption else "" num = ' class="num"' head = "".join(f'{esc(h)}' for i, h in enumerate(headers)) body = "".join( "" + "".join(cell(i, v, row[0] if compare else headers[i], head=row_heads and i == 0) for i, v in enumerate(row)) + "" for row in rows) mode = "columns" if compare else "rows" if len(headers) >= STACK_COLUMNS else "" stack = f' data-stack="{mode}"' if mode else "" return Markup(f'
    {cap}{head}{body}
    ') def trail_link(rec): from . import common return Markup(f'{common.name(rec)}') def ft_per_mile(m_per_km): """A climb rate as feet per mile and metres per kilometre, in the same unit pair as lengths.""" ft_mi = m_per_km * words.M_PER_FT ** -1 * words.KM_PER_MI return Markup(f'{esc(words.fmt_int(ft_mi))} ft per mile' f'{esc(words.fmt_int(m_per_km))} m per km') def append_nav(body, nav_html): """body with nav_html placed at the end of its last container (the page's closing ).""" text = str(body) i = text.rfind("") return Markup(text[:i] + nav_html + text[i:]) if i >= 0 else Markup(text + nav_html) def more_nav(path, extras): """A line of links to the other table, change and reference pages: extras is [(group, title, blurb, path, n)] as pages.py builds it for /lists/.""" links = " · ".join(f'{esc(t)}' for _, t, _, p, _ in extras if p != path) return (f'') if links else ""