Skip to content

docs_text

A page of the docs source as plain markdown, for a reader that is not the docs site (SensAI's docs agent reads the docs this way).

The pages in DOCS_SOURCE_DIR are written for the docs site, and read as plain text a few lines would mislead: the pictures are image files that do not ship, and some lines are site markup. This removes it:

  • a <figure> keeps its caption, as a line starting Figure:, and loses the image; any other image line, and any <video> block, is removed
  • HTML comments (notes for the people who maintain the docs) are removed
  • a page's front matter (the --- block of site settings at its top) and its <style> and <script> blocks are removed
  • a row of linked images (the badges at the top of the home page) is removed
  • attribute lists after a link ({ .md-button }) and icon codes (:material-open-in-new:) are removed

Code blocks are left alone. The code the guides show is already in them: scripts/gen_docs_source.py replaces each --8<-- include with the lines it names when it writes the docs source. It uses only the standard library.

clean

Python
clean(text: str) -> str

The text with the site markup removed outside its code blocks, and the blank lines left behind collapsed.

Source code in src/mujoco_mojo/utils/docs_text.py
Python
def clean(text: str) -> str:
    """The text with the site markup removed outside its code blocks, and the blank lines left behind collapsed."""
    text = _FRONT_MATTER.sub("", text, count=1)
    prose: list[str] = []
    result: list[str] = []
    in_code = False

    def flush() -> None:
        result.extend(_clean_prose(prose))
        prose.clear()

    for line in text.splitlines():
        if _FENCE.match(line):
            if not in_code:
                flush()
            result.append(line)
            in_code = not in_code
        elif in_code:
            result.append(line)
        else:
            prose.append(line)
    flush()

    # a removed comment or image leaves its blank lines behind: keep one, outside code
    collapsed: list[str] = []
    in_code = False
    for line in result:
        if _FENCE.match(line):
            in_code = not in_code
        if not in_code and not line.strip() and collapsed and not collapsed[-1].strip():
            continue
        collapsed.append(line.rstrip() if not in_code else line)
    return "\n".join(collapsed).strip("\n") + "\n"

plain_text

Python
plain_text(page: Path) -> str

The page at page as plain markdown.

Source code in src/mujoco_mojo/utils/docs_text.py
Python
def plain_text(page: Path) -> str:
    """The page at `page` as plain markdown."""
    return clean(page.read_text(encoding="utf-8"))