Python module docs
Render Python modules, {{< doc ... >}} shortcodes, and live API signatures for fastship.
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 py_page_specs 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')
docs_base_url
def docs_base_url(
root:str | pathlib._local.Path, # Project root directory
data:dict=None, # Optional parsed `pyproject.toml` mapping
ship:dict=None, # Optional normalized fastship settings dict
)->str: # Published docs base URL ending with `/`, or `""` when unknown
Return the published docs base URL from site-url, [project.urls].Documentation, or inferred GitHub Pages URL.
project_defaults
def project_defaults(
root:str | pathlib._local.Path, # Project root containing `pyproject.toml` or `ship.toml`
data:dict=None, # Optional parsed `pyproject.toml` mapping
ship:dict=None, # Optional normalized fastship settings dict
)->dict: # Default site settings (snake-case) derived from native project metadata
Derive default site settings (title, description, site_url, tools) from native manifests and git remote.
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, source_link:bool=True, lookup=None
):
Markdown renderer for Python module pages, {{< doc ... >}} shortcodes, and show_doc.
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`)
lookup=None, # Optional `DocLookup` instance reused for source links
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`)
lookup=None, # Optional `DocLookup` instance reused for source links
)->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
lookup=None, # Optional `DocLookup` instance reused for source links
)->str:
Expand {{< doc sym [level=N] >}} shortcodes in text, skipping code blocks and inline code.
Package discovery
Discover uncovered hand-written .py modules for generated api/ pages.
py_page_specs
def py_page_specs(
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
data:dict=None, # Optional parsed `pyproject.toml` mapping
)->tuple[list[tuple[str, pathlib._local.Path, pathlib._local.Path, dict]], dict[str, tuple[str, str]], dict[str, str]]: # `(specs, mod_pages, mod_urls)`
Discover uncovered and covered .py modules and their page frontmatter via AST without importing package code.