Skip to content

Tangling

tangle

Tangling a literate .qvr.md module into QVR source.

A literate module is a Markdown document whose fenced qvr blocks, in document order, are its code: the document tangles to one module, as a literate program tangles to the program it describes. Its prose is metadata: the tangled source keeps every code line on the line it has in the document and replaces every other line by an empty one, so a diagnostic names the document's own line and an edit to the prose changes no declaration and no content hash.

The blocks that tangle are the module's code blocks: unmarked ones and those marked cumulative or qiec-cumulative. A block marked false, standalone, or qiec is a separate fragment, an example fence is a test, and a --8<-- include shows a corpus file; none of these is code of the module.

LITERATE_SUFFIX module-attribute

LITERATE_SUFFIX = '.qvr.md'

The file suffix of a literate module.

is_literate_path

is_literate_path(path: str | Path) -> bool

Whether a path names a literate module.

PARAMETER DESCRIPTION
path

A file path.

TYPE: str | Path

RETURNS DESCRIPTION
bool

True when the file name ends in .qvr.md.

Source code in src/quivers/dsl/literate/tangle.py
def is_literate_path(path: str | Path) -> bool:
    """Whether a path names a literate module.

    Parameters
    ----------
    path : str | Path
        A file path.

    Returns
    -------
    bool
        True when the file name ends in ``.qvr.md``.
    """
    return Path(path).name.endswith(LITERATE_SUFFIX)

module_stem

module_stem(path: str | Path) -> str

The module name of a .qvr or .qvr.md file.

PARAMETER DESCRIPTION
path

The file.

TYPE: str | Path

RETURNS DESCRIPTION
str

The file name without .qvr.md or .qvr.

Source code in src/quivers/dsl/literate/tangle.py
def module_stem(path: str | Path) -> str:
    """The module name of a ``.qvr`` or ``.qvr.md`` file.

    Parameters
    ----------
    path : str | Path
        The file.

    Returns
    -------
    str
        The file name without ``.qvr.md`` or ``.qvr``.
    """
    name = Path(path).name
    if name.endswith(LITERATE_SUFFIX):
        return name[: -len(LITERATE_SUFFIX)]
    return Path(name).stem

code_blocks

code_blocks(text: str) -> tuple[FencedBlock, ...]

The blocks a literate document tangles.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

RETURNS DESCRIPTION
tuple[FencedBlock, ...]

Its code blocks, in document order.

Source code in src/quivers/dsl/literate/tangle.py
def code_blocks(text: str) -> tuple[FencedBlock, ...]:
    """The blocks a literate document tangles.

    Parameters
    ----------
    text : str
        The document.

    Returns
    -------
    tuple[FencedBlock, ...]
        Its code blocks, in document order.
    """
    return tuple(
        block
        for block in scan_blocks(text, literate=True)
        if block.mode in CUMULATIVE_MODES and not block.include
    )

tangle

tangle(text: str) -> str

The QVR source a literate document tangles to.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

RETURNS DESCRIPTION
str

One line per document line: a code block's line with the fence's indentation removed, and an empty line for every other line, so the source's line numbers are the document's.

Source code in src/quivers/dsl/literate/tangle.py
def tangle(text: str) -> str:
    """The QVR source a literate document tangles to.

    Parameters
    ----------
    text : str
        The document.

    Returns
    -------
    str
        One line per document line: a code block's line with the fence's
        indentation removed, and an empty line for every other line, so the
        source's line numbers are the document's.
    """
    lines = text.split("\n")
    tangled = [""] * len(lines)
    for block in code_blocks(text):
        first = block.first_body_line - 1
        for offset, line in enumerate(block.body.split("\n")[:-1]):
            tangled[first + offset] = line
    source = "\n".join(line.rstrip() for line in tangled)
    return source if source.endswith("\n") else source + "\n"

read_module_text

read_module_text(path: str | Path) -> str

The QVR source of a module file, tangling a literate one.

PARAMETER DESCRIPTION
path

A .qvr or .qvr.md file.

TYPE: str | Path

RETURNS DESCRIPTION
str

The file's text, or the source a .qvr.md file tangles to.

RAISES DESCRIPTION
OSError

If the file cannot be read.

Source code in src/quivers/dsl/literate/tangle.py
def read_module_text(path: str | Path) -> str:
    """The QVR source of a module file, tangling a literate one.

    Parameters
    ----------
    path : str | Path
        A ``.qvr`` or ``.qvr.md`` file.

    Returns
    -------
    str
        The file's text, or the source a ``.qvr.md`` file tangles to.

    Raises
    ------
    OSError
        If the file cannot be read.
    """
    text = Path(path).read_text(encoding="utf-8")
    return tangle(text) if is_literate_path(path) else text