Skip to content

Fences

fences

Shared fenced-code-block primitive for markdown-aware strategies.

Factors out the backreference fence regex that was previously duplicated across :mod:collapse_whitespace, :mod:strip_html_comments and :mod:compact_tables. Capturing the opening backtick run and closing on the exact same length means a 4-backtick fence is not closed by an inner 3-backtick line.

fenced_line_indices(text)

Return indices of lines that fall inside a fenced code block.

Lines are indexed as produced by text.split("\n"). Both fence delimiter lines and the content between them are included.

Source code in packages/axm-smelt/src/axm_smelt/strategies/fences.py
Python
def fenced_line_indices(text: str) -> set[int]:
    """Return indices of lines that fall inside a fenced code block.

    Lines are indexed as produced by ``text.split("\\n")``.  Both fence
    delimiter lines and the content between them are included.
    """
    fenced: set[int] = set()
    for match in FENCED_BLOCK_RE.finditer(text):
        start = text.count("\n", 0, match.start())
        end = text.count("\n", 0, match.end())
        fenced.update(range(start, end + 1))
    return fenced