Python module docs

Render Python modules, {{< doc ... >}} shortcodes, and pyproject.toml site defaults for Octavo.

Use render_mod or mod_page to turn a Python module's docstring, # - key: val frontmatter comments, top-level Markdown strings, and public classes/callables into Markdown in AST definition order. Use expand_docs to expand {{< doc sym [level=N] >}} shortcodes in hand-written .md or .qmd pages, and pkg_pages to discover uncovered public modules in a package for automatic API documentation.

py_frontmatter('"Mod."\n# - order: 6\n# - title: Python docs\nx = 1\n')

Frontmatter and {python} fences

Extract # - key: val module metadata and prepare ```{python} blocks for static documentation rendering.

py_frontmatter

def py_frontmatter(
    src:str, # Python module source text
)->dict: # Parsed YAML dict from leading `# - key: val` comments after the module docstring

Parse # - key: val frontmatter comments immediately after a module's docstring.

```{python}
assert py_frontmatter('"Doc."

- order: 2

- title: Core

') == {'order': 2, 'title': 'Core'} assert py_frontmatter('x = 1 ') == {} ```

py_fences

def py_fences(
    txt:str, # Markdown or docstring text to scan
)->list[tuple[str, int]]: # `(code, line_offset)` pairs for each top-level `{python}` fence (`line_offset` is 0-based body start in `txt`)

Extract executable {python} fenced code blocks and their 0-based body line offsets from Markdown txt.

```{python}
assert py_fences('Hi
x = 1

') == [('x = 1', 3)] ```

clean_doc_md

def clean_doc_md(
    txt:str, # Markdown or rendered docstring text
)->str: # Text with hidden `{python}` blocks dropped, `#| ` directives stripped, and `{python}` rendered as `python`

Prepare Markdown txt with {python} fences for static docs, dropping #| hide blocks and stripping #| directives.

```{python}
src = 'Intro
y = 2

' assert clean_doc_md(src) == 'Intro

y = 2
```'
    ```

## Rendering modules and shortcodes

Turn Python modules and `{{< doc ... >}}` shortcodes into Markdown pages with YAML frontmatter.

### DocRenderer

```python
def DocRenderer(
    sym, name:str | None=None, title_level:int=3, maxline:int=110
):

Markdown renderer for expand_docs and render_mod with a heading.

render_mod

def render_mod(
    mod:module, # Imported Python module to document
    level:int=2, # Base heading level for top-level symbols (class methods use `sym_level + 1`)
    renderer:type=DocRenderer, # Renderer class or callable taking `(sym, title_level=level)`
    skip_first:bool=False, # Omit paragraph 1 of the module docstring (when already in YAML `description:`)
    src:str=None, # Optional module source text (defaults to reading `mod.__file__`)
)->str:

Render a module's docstring, top-level Markdown strings, and public classes/callables in definition order.

mod_page

def mod_page(
    mod_nm:str, # Dotted module name to import and render
    path:str | pathlib._local.Path=None, # Optional `.py` path (defaults to `mod.__file__`)
    level:int=2, # Heading level for top-level module symbols (class methods use `level + 1`)
    renderer:type=DocRenderer, # Renderer class or callable taking `(sym, title_level=level)`
)->str:

Render a standalone Markdown page (YAML frontmatter + module docs) for mod_nm.

expand_docs

def expand_docs(
    text:str, # Markdown or Quarto source text
    renderer:type=DocRenderer, # Renderer class or callable taking `(sym, title_level=level)`
)->str:

Expand {{< doc sym [level=N] >}} shortcodes in text, skipping code blocks and inline code.

Package discovery and pyproject.toml defaults

Discover uncovered hand-written .py modules, determine their sidebar placement (api/ vs root for flat nbdev projects), and derive site defaults from pyproject.toml.

docs_base_url

def docs_base_url(
    root:str | pathlib._local.Path, # Project root directory
    data:dict=None, # Parsed `pyproject.toml` mapping (read from `root` when omitted)
)->str: # Published docs base URL ending with `/`, or `""` when unknown

Return the published docs base URL from [project.urls].Documentation or the GitHub Pages URL inferred from the repo slug.

pkg_pages

def pkg_pages(
    root:str | pathlib._local.Path, # Project root directory
    pkg_path:str | pathlib._local.Path=None, # Package directory (inferred from `pyproject.toml` when omitted)
    docs_dir:str | pathlib._local.Path=None, # Docs or notebooks directory to check for existing/covered pages
    excl:set | list=(), # Relative paths or directory names to exclude
    renderer:type=DocRenderer, # Renderer passed to `mod_page`
    render:bool=True, # Import and render discovered modules? False returns AST-only `mod_pages` and `mod_urls`
)->tuple[list[tuple[str, pathlib._local.Path, pathlib._local.Path, str]], dict[str, tuple[str, str]], dict[str, str]]: # `(auto_pages, mod_pages, mod_urls)`

Discover uncovered hand-written .py modules and render them as standalone Markdown pages.

Skips _*.py, skill.py, guidance.py, # AUTOGENERATED! files, modules without both a module docstring and # - ... frontmatter (or with # - publish: false), modules without public API, modules covered by a hand-written page, and excluded paths. Places generated pages under api/ unless docs_dir has visible root #| default_exp notebooks and no api/ subfolder. Returns (auto_pages, mod_pages, mod_urls) where auto_pages holds (mod_nm, py_path, rel_md, md_text), mod_pages maps mod_nm -> (html_rel, py_rel), and mod_urls maps mod_nm -> md_rel.

pkg_mod_urls

def pkg_mod_urls(
    root:str | pathlib._local.Path, # Project root directory
    pkg_path:str | pathlib._local.Path=None, # Package directory (inferred from `pyproject.toml` when omitted)
    docs_dir:str | pathlib._local.Path=None, # Docs or notebooks directory to check for existing/covered pages
    excl:set | list=(), # Relative paths or directory names to exclude
)->dict[str, str]: # Mapping of public module name to relative Markdown doc URL without importing modules

Discover uncovered and covered .py module doc URLs via AST without importing any package code.

project_defaults

def project_defaults(
    root:str | pathlib._local.Path, # Project root containing `pyproject.toml`
    data:dict=None, # Optional parsed `pyproject.toml` mapping
)->dict: # Default Octavo site settings (snake-case) derived from `pyproject.toml`

Derive default Octavo site settings (title, description, site_url, tools, exclude) from pyproject.toml.