Skip to content

Fenced blocks

blocks

The fenced QVR blocks of a Markdown document.

A fenced block opens with a line of three backticks and qvr (or R, the closest lexer GitHub offers, which the root README uses for QVR), possibly indented inside a tabbed section, and closes with three backticks at the same indentation. An HTML comment on the line before the opening fence sets the block's compile mode:

  • <!-- compile: false, because REASON -->: an illustrative fragment, not code. The reason says why the fragment cannot be a module (a lone statement shown outside the declaration that would hold it, say); the documentation gate requires one on every such fragment.
  • <!-- compile: cumulative --> and <!-- compile: qiec-cumulative -->: the block continues every earlier cumulative block of the document.
  • <!-- compile: qiec -->, <!-- compile: standalone -->: a complete module on its own.
  • <!-- compile: refused "TEXT" -->: a complete module that is refused, shown to illustrate a diagnostic. The documentation gate compiles it and requires an error whose message contains TEXT.
  • <!-- compile: file PATH -->: the file PATH (sigs.qvr, worlds/boolean.qvr) of the document's package. A document's file blocks are the modules of one package, and each is checked as a module of that package, so the blocks may import each other, declare holes, and link them.
  • no marker: a complete module on its own in an ordinary document; in a literate .qvr.md module, part of the module the document tangles to, so the mode is cumulative.

A fence written ```qvr example holds the steps of an example (let and expect lines): it is a test of the document's module, not part of its code, and its mode is example. A block whose body is a single --8<-- "docs/examples/....qvr" line includes a corpus file for display.

Every reader of fenced QVR blocks (the literate tangler, qvr migrate, and the documentation gate) scans with scan_blocks, so they agree on which blocks a document has and what each is.

FENCE module-attribute

FENCE = re.compile('^(?P<indent>[ \\t]*)```(?:qvr|R)(?P<example> example)?\\n(?P<body>.*?)^(?P=indent)```', re.M | re.S)

A fenced QVR block: its indentation, whether it is an example, and its body.

MARKER module-attribute

MARKER = re.compile('<!--\\s*compile:\\s*(?:(false|standalone|cumulative|qiec|qiec-cumulative)|file\\s+(?P<path>[A-Za-z0-9_][A-Za-z0-9_./-]*\\.qvr)|refused\\s+"(?P<refusal>[^"]+)")(?:\\s*,\\s*because\\s+(?P<reason>.*?))?\\s*-->')

The compile-mode marker on the line before a fence: a mode, file and the package path of the file the block is, or refused and the text its diagnostic contains, with the reason a false marker gives after because.

INCLUDE module-attribute

INCLUDE = re.compile('--8<-- "(docs/examples/[a-zA-Z0-9_/-]+\\.qvr)"')

A block whose whole body includes a corpus file.

CUMULATIVE_MODES module-attribute

CUMULATIVE_MODES: frozenset[str] = frozenset({'cumulative', 'qiec-cumulative'})

The modes whose blocks continue the document's earlier cumulative blocks.

Mode

Mode = Literal['false', 'standalone', 'cumulative', 'qiec', 'qiec-cumulative', 'example', 'file', 'refused']

A block's compile mode.

FencedBlock didactic-model

FencedBlock(*, line: int, mode: Mode, indent: str, start: int, end: int, body: str, include: str = '', reason: str = '', path: str = '', refusal: str = '')

Bases: Model

One fenced QVR block of a Markdown document.

PARAMETER DESCRIPTION
line

The 1-based line of the opening fence.

TYPE: int

mode

The block's compile mode.

TYPE: Mode

indent

The fence's indentation, stripped from every body line.

TYPE: str

start

The character offset of the body in the document.

TYPE: int

end

The character offset one past the body.

TYPE: int

body

The body with the indentation removed.

TYPE: str

include

The included corpus path, or empty.

TYPE: str DEFAULT: ''

reason

The reason its compile: false marker gives, or empty.

TYPE: str DEFAULT: ''

path

The package path of a file block, or empty.

TYPE: str DEFAULT: ''

refusal

The text a refused block's diagnostic contains, or empty.

TYPE: str DEFAULT: ''

first_body_line property

first_body_line: int

The 1-based document line of the body's first line.

RETURNS DESCRIPTION
int

The line after the opening fence.

dedent

dedent(indent: str, body: str) -> str

Strip a fence's indentation from every line of its body.

PARAMETER DESCRIPTION
indent

The fence's indentation.

TYPE: str

body

The body as written.

TYPE: str

RETURNS DESCRIPTION
str

The body with indent removed from each line that starts with it; other lines (blank ones, or ones less indented) are kept, so a diagnostic keeps an accurate column.

Source code in src/quivers/dsl/literate/blocks.py
def dedent(indent: str, body: str) -> str:
    """Strip a fence's indentation from every line of its body.

    Parameters
    ----------
    indent : str
        The fence's indentation.
    body : str
        The body as written.

    Returns
    -------
    str
        The body with ``indent`` removed from each line that starts with it;
        other lines (blank ones, or ones less indented) are kept, so a
        diagnostic keeps an accurate column.
    """
    if not indent:
        return body
    return "".join(
        line[len(indent) :] if line.startswith(indent) else line
        for line in body.splitlines(keepends=True)
    )

indent_body

indent_body(indent: str, body: str) -> str

Indent every non-blank line of a body by a fence's indentation.

PARAMETER DESCRIPTION
indent

The fence's indentation.

TYPE: str

body

A dedented body.

TYPE: str

RETURNS DESCRIPTION
str

The body as it is written inside the fence.

Source code in src/quivers/dsl/literate/blocks.py
def indent_body(indent: str, body: str) -> str:
    """Indent every non-blank line of a body by a fence's indentation.

    Parameters
    ----------
    indent : str
        The fence's indentation.
    body : str
        A dedented body.

    Returns
    -------
    str
        The body as it is written inside the fence.
    """
    if not indent:
        return body
    return "".join(
        indent + line if line.strip() else line
        for line in body.splitlines(keepends=True)
    )

marker_before

marker_before(text: str, fence_start: int) -> Mode | None

The compile-mode marker on the line before a fence, if any.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

fence_start

The offset of the fence's line.

TYPE: int

RETURNS DESCRIPTION
Mode | None

The marked mode, or None for an unmarked block.

Source code in src/quivers/dsl/literate/blocks.py
def marker_before(text: str, fence_start: int) -> Mode | None:
    """The compile-mode marker on the line before a fence, if any.

    Parameters
    ----------
    text : str
        The document.
    fence_start : int
        The offset of the fence's line.

    Returns
    -------
    Mode | None
        The marked mode, or None for an unmarked block.
    """
    match = _marker_match(text, fence_start)
    if match is None:
        return None
    if match.group("path") is not None:
        return "file"
    if match.group("refusal") is not None:
        return "refused"
    return next(mode for mode in _MARKED if mode == match.group(1))

marker_refusal_before

marker_refusal_before(text: str, fence_start: int) -> str

The diagnostic text a refused marker before a fence names.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

fence_start

The offset of the fence's line.

TYPE: int

RETURNS DESCRIPTION
str

The text, or empty when the block is not marked refused.

Source code in src/quivers/dsl/literate/blocks.py
def marker_refusal_before(text: str, fence_start: int) -> str:
    """The diagnostic text a ``refused`` marker before a fence names.

    Parameters
    ----------
    text : str
        The document.
    fence_start : int
        The offset of the fence's line.

    Returns
    -------
    str
        The text, or empty when the block is not marked ``refused``.
    """
    match = _marker_match(text, fence_start)
    return "" if match is None else match.group("refusal") or ""

marker_path_before

marker_path_before(text: str, fence_start: int) -> str

The package path a file marker before a fence names.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

fence_start

The offset of the fence's line.

TYPE: int

RETURNS DESCRIPTION
str

The path, or empty when the block is not marked file.

Source code in src/quivers/dsl/literate/blocks.py
def marker_path_before(text: str, fence_start: int) -> str:
    """The package path a ``file`` marker before a fence names.

    Parameters
    ----------
    text : str
        The document.
    fence_start : int
        The offset of the fence's line.

    Returns
    -------
    str
        The path, or empty when the block is not marked ``file``.
    """
    match = _marker_match(text, fence_start)
    return "" if match is None else match.group("path") or ""

marker_reason_before

marker_reason_before(text: str, fence_start: int) -> str

The reason a compile: false marker before a fence gives.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

fence_start

The offset of the fence's line.

TYPE: int

RETURNS DESCRIPTION
str

The text after because in a false marker, or empty when the block is unmarked, marked with another mode, or gives no reason.

Source code in src/quivers/dsl/literate/blocks.py
def marker_reason_before(text: str, fence_start: int) -> str:
    """The reason a ``compile: false`` marker before a fence gives.

    Parameters
    ----------
    text : str
        The document.
    fence_start : int
        The offset of the fence's line.

    Returns
    -------
    str
        The text after ``because`` in a ``false`` marker, or empty when the
        block is unmarked, marked with another mode, or gives no reason.
    """
    match = _marker_match(text, fence_start)
    if match is None or match.group(1) != "false":
        return ""
    return (match.group("reason") or "").strip()

scan_blocks

scan_blocks(text: str, *, literate: bool) -> tuple[FencedBlock, ...]

Find the fenced QVR blocks of a Markdown document.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

literate

Whether the document is a literate .qvr.md module, whose unmarked blocks tangle into one module.

TYPE: bool

RETURNS DESCRIPTION
tuple[FencedBlock, ...]

The blocks in document order.

Source code in src/quivers/dsl/literate/blocks.py
def scan_blocks(text: str, *, literate: bool) -> tuple[FencedBlock, ...]:
    """Find the fenced QVR blocks of a Markdown document.

    Parameters
    ----------
    text : str
        The document.
    literate : bool
        Whether the document is a literate ``.qvr.md`` module, whose
        unmarked blocks tangle into one module.

    Returns
    -------
    tuple[FencedBlock, ...]
        The blocks in document order.
    """
    blocks: list[FencedBlock] = []
    for match in FENCE.finditer(text):
        indent, raw = match.group("indent"), match.group("body")
        if match.group("example") is not None:
            mode: Mode = "example"
        else:
            mode = marker_before(text, match.start()) or (
                "cumulative" if literate else "standalone"
            )
        body = dedent(indent, raw)
        include = INCLUDE.fullmatch(body.strip())
        blocks.append(
            FencedBlock(
                line=text.count("\n", 0, match.start()) + 1,
                mode=mode,
                indent=indent,
                start=match.start("body"),
                end=match.end("body"),
                body=body,
                include="" if include is None else include.group(1),
                reason=marker_reason_before(text, match.start())
                if mode == "false"
                else "",
                path=marker_path_before(text, match.start()) if mode == "file" else "",
                refusal=marker_refusal_before(text, match.start())
                if mode == "refused"
                else "",
            )
        )
    return tuple(blocks)