"""Small pieces the view pages share: a page heading with a breadcrumb, and rows of things that are not trails.""" import re 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}

    """ # The site sets no tables. A set of things (files, terms, changes, places) is shown as rows in the # language of a trail's row: a name, a sentence where there is one, and a line of short facts. # A cell longer than this (in characters of text) is a sentence of the row, not a fact in its line. LONG_CELL = 60 def _text_len(html): return len(re.sub(r"<[^>]+>", "", str(html)).strip()) def plain_entry(name_html, *, text=(), facts=(), small=(), tag="", level=3, heading=True): """One row of something that is not a trail: name_html as a row's name, `tag` (HTML) above it as a route tag is, `text` (HTML paragraphs) as a row's sentence, `facts` (HTML) on a line as a row's facts are, `small` (HTML) as small print.""" kicker = f'

    {tag}

    ' if tag else "" h = f"h{level}" if heading else "p" paras = "".join(f'

    {t}

    ' for t in text if str(t).strip()) line = "".join(f'{f}' for f in facts if str(f).strip()) line = f'

    {line}

    ' if line else "" fine = " · ".join(str(x) for x in small if str(x).strip()) fine = f'

    {fine}

    ' if fine else "" return (f'
  • {kicker}' f'<{h} class="entry__name">{name_html}{paras}{line}{fine}
  • ') def plain_list(items_html, label=""): aria = f' aria-label="{esc(label)}"' if label else "" return Markup(f'
      {"".join(items_html)}
    ') def rows_list(headers, rows, caption=""): """A set of things as rows: the first cell of each is its name; a long cell is its sentence; the short ones are its line of facts, each with its column's title.""" items = [] for row in rows: cells = [v if isinstance(v, Markup) else esc(v) for v in row] text, facts = [], [] for label, cell in zip(headers[1:], cells[1:]): if not _text_len(cell): continue if _text_len(cell) > LONG_CELL: text.append(cell) else: facts.append(f'{esc(label)} {cell}') items.append(plain_entry(cells[0], text=text, facts=facts, heading=False)) return plain_list(items, caption) def table(headers, rows, *, caption="", numeric=(), row_heads=False, compare=False): """A set of things, shown as rows (rows_list): headers are the titles of the cells, rows lists of HTML cells (Markup, or text that is escaped). The other arguments are kept for the callers.""" return rows_list(headers, rows, caption) _TABLE = re.compile(r'(?:
    \s*)?]*>(.*?)(?:\s*
    )?', re.S) _ROW = re.compile(r"]*>(.*?)", re.S) _CELL = re.compile(r"<(th|td)\b[^>]*>(.*?)", re.S) _CAPTION = re.compile(r"]*>(.*?)", re.S) def untable(html): """html with every table in it set as rows (rows_list). For the pages written from Markdown and by hand, whose tables are plain: one head row, then body rows.""" def one(m): inner = m.group(1) cap = _CAPTION.search(inner) rows = [[c[1].strip() for c in _CELL.findall(r)] for r in _ROW.findall(inner)] rows = [r for r in rows if r] if not rows: return "" head, body = rows[0], rows[1:] caption = re.sub(r"<[^>]+>", "", cap.group(1)) if cap else "" return str(rows_list(head, [[Markup(c) for c in r] for r in body], caption)) return _TABLE.sub(one, str(html)) 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 ""