diff --git a/meshterm/ui/markdown.py b/meshterm/ui/markdown.py index d39816e..c78d26f 100644 --- a/meshterm/ui/markdown.py +++ b/meshterm/ui/markdown.py @@ -23,6 +23,8 @@ imported from somewhere else: * Lists hang on a bullet in a two-column grid, so a wrapped item aligns under its own text and a nested list steps in under its parent. Quotes and fenced code run behind a rail down the left, drawn on *every* line including the wrapped ones. +* A fence tagged ``qr`` draws nothing of itself: its body is a URL, and the page shows + the app's scannable code for it (:func:`~meshterm.ui.qr.qr_text`). * Emphasis, code, links and struck-out runs take the ``md.*`` styles, which — like every style name here — mean something on both platforms (see :mod:`meshterm.ui.theme`). A link shows its target after the text unless the text already says it; nothing here @@ -248,6 +250,8 @@ def _block(node: SyntaxTreeNode, *, indent: int, depth: int) -> list[RenderableT if kind == "blockquote": quoted = Group(*_blocks(node.children, indent=0, depth=depth)) return [_pad(_Railed(Styled(quoted, "md.quote"), style="md.bullet"), indent)] + if kind == "fence" and node.info.strip() == "qr": + return [_pad(_qr(node.content.strip()), indent)] if kind in ("fence", "code_block"): code = Text(node.content.rstrip("\n"), style="md.code") return [_pad(_Railed(code, style="md.rail"), indent)] @@ -276,6 +280,29 @@ def _blocks( return rendered +def _qr(data: str) -> RenderableType: + """A ``qr`` fence: the URL inside it, drawn as the app's scannable code. + + The code is generated as the page opens rather than pasted into the ``.md`` as + half-block art. Art cannot survive markdown — a row that happens to open on a light + module opens on a *space*, and every parser in the world eats it, shearing that row + one module to the left — and a code nobody can scan is worse than no code. Drawing it + live also means the link is written once, in the fence, and the code can never drift + from it. + + Args: + data: The fence's body — the URL to encode. + + Returns: + The code, from :func:`~meshterm.ui.qr.qr_text` — the same black-on-white widget + the share popups draw, so a QR looks the same everywhere in MeshTerm and reads + the same whatever palette the terminal is running. + """ + from .qr import qr_text + + return qr_text(data) + + def _pad(renderable: RenderableType, indent: int) -> RenderableType: """Hang ``renderable`` at ``indent`` cells (unchanged when it is flush).""" if not indent: diff --git a/tests/test_markdown.py b/tests/test_markdown.py index 88d91fd..408551c 100644 --- a/tests/test_markdown.py +++ b/tests/test_markdown.py @@ -191,6 +191,26 @@ def test_a_fenced_block_keeps_its_own_lines() -> None: assert [line.rstrip() for line in lines] == [" │ one", " │ two"] +def test_a_qr_fence_draws_the_code_rather_than_the_url_inside_it() -> None: + """The one fence that isn't quoted: its body is a link, and the page shows the code. + + Drawn live from the fence, never pasted in as half-block art — art loses a row whose + first module is light, because that row starts with a space and markdown eats it. + """ + source = """```qr +https://example.com/x +``` +""" + lines = [line for line in _lines(source) if line.strip()] + + assert lines, "the fence rendered nothing" + assert not any("example.com" in line for line in lines), "the URL was printed as text" + assert all(set(line.strip()) <= set("█▀▄ ") for line in lines), lines + # Two of the three finder squares sit at either end of the code's first module row. + top = lines[0].strip() + assert top.startswith("█▀▀▀▀▀█") and top.endswith("█▀▀▀▀▀█"), top + + def test_a_rule_runs_the_full_width_of_the_page() -> None: """``---`` is a page rule, not an indented one — it separates, so it spans.""" line = next(line for line in _lines("a\n\n---\n\nb\n", 40) if "─" in line)