Module

rendering.template_functions.autodoc

Autodoc template functions for API documentation.

Provides normalized access to DocElement metadata across all extractor types (Python, CLI, OpenAPI). Templates should use these functions instead of directly accessing element.metadata.

View Dataclasses (for theme developers):

  • MemberView: Normalized Python method/function view
  • ParamView: Normalized parameter view
  • CommandView: Normalized CLI command view
  • OptionView: Normalized CLI option/argument view

Filters:

  • element | members: Get normalized Python members (methods/functions)
  • element | commands: Get normalized CLI commands
  • element | options: Get normalized CLI options/arguments

Legacy Functions:

  • get_params(element): Get normalized parameters list
  • get_return_info(element): Get normalized return type info
  • param_count(element): Count of parameters (excluding self/cls)
  • return_type(element): Return type string or 'None'
  • get_element_stats(element): Get display stats for element children

Ergonomic Helpers (Tier 3 - Portable Context Globals):

  • children_by_type(children, element_type): Filter children by type
  • public_only(members): Filter to public members (no underscore prefix)
  • private_only(members): Filter to private members (underscore prefix)

These helper functions are registered as both Jinja filters and globals, making them portable across any Python-based template engine.

Classes

DocElementLike 7
Protocol for objects with DocElement-like interface for autodoc.

Protocol for objects with DocElement-like interface for autodoc.

Methods

typed_metadata 0 Any
Typed metadata object.
property
def typed_metadata(self) -> Any
Returns
Any
description 0 str
Element description.
property
def description(self) -> str
Returns
str
href 0 str | None
URL to the element.
property
def href(self) -> str | None
Returns
str | None
name 0 str
Element name.
property
def name(self) -> str
Returns
str
deprecated 0 str | None
Deprecation message if deprecated.
property
def deprecated(self) -> str | None
Returns
str | None
metadata 0 dict[str, Any]
Raw metadata dictionary.
property
def metadata(self) -> dict[str, Any]
Returns
dict[str, Any]
children 0 list[Any]
Child elements.
property
def children(self) -> list[Any]
Returns
list[Any]
ParamView 7
Normalized parameter view for templates. Provides consistent access to parameter data for Python f…

Normalized parameter view for templates.

Provides consistent access to parameter data for Python functions, CLI commands, and other elements with parameters.

Attributes

Name Type Description
name str

Parameter name

type str

Type annotation (e.g., "str", "int | None")

default str | None

Default value as string (e.g., "None", "'hello'")

required bool

Whether parameter is required

description str

Parameter description from docstring

kind str

Parameter kind (positional, keyword, var_positional, var_keyword)

Methods

from_dict 1 ParamView
Create from normalized parameter dict.
classmethod
def from_dict(cls, param: dict[str, Any]) -> ParamView
Parameters
Name Type Description
param
Returns
ParamView
MemberView 17
Normalized Python member view for templates. Provides consistent access to method/function data re…

Normalized Python member view for templates.

Provides consistent access to method/function data regardless of how it's stored in the DocElement.

Attributes

Name Type Description
name str

Member name

signature str

Full signature string

description str

Description/docstring

return_type str

Return type annotation

return_description str

Return value description

params tuple[ParamView, ...]

Tuple of ParamView objects

is_async bool

Whether function is async

is_property bool

Whether function is a property

is_classmethod bool

Whether function is a classmethod

is_staticmethod bool

Whether function is a staticmethod

is_abstract bool

Whether function is abstract

is_deprecated bool

Whether function is deprecated

is_private bool

Whether name starts with underscore

href str

Link to member page (or anchor)

decorators tuple[str, ...]

Tuple of decorator names

typed_metadata Any

Full PythonFunctionMetadata (for advanced use)

Methods

from_doc_element 1 MemberView
Create from DocElement.
classmethod
def from_doc_element(cls, el: DocElementLike) -> MemberView
Parameters
Name Type Description
el
Returns
MemberView
OptionView 13
Normalized CLI option/argument view for templates. Provides consistent access to CLI option data.

Normalized CLI option/argument view for templates.

Provides consistent access to CLI option data.

Attributes

Name Type Description
name str

Option name (e.g., "verbose")

flags tuple[str, ...]

Option flags as tuple (e.g., ("-v", "--verbose"))

flags_str str

Flags as comma-separated string

type str

Type name (e.g., "STRING", "INT", "BOOL")

description str

Help text

default Any

Default value

required bool

Whether option is required

is_flag bool

Whether option is a boolean flag

is_argument bool

Whether this is a positional argument (not an option)

multiple bool

Whether option accepts multiple values

envvar str | None

Environment variable name

typed_metadata Any

Full CLIOptionMetadata (for advanced use)

Methods

from_doc_element 1 OptionView
Create from DocElement.
classmethod
def from_doc_element(cls, el: DocElementLike) -> OptionView
Parameters
Name Type Description
el
Returns
OptionView
CommandView 11
Normalized CLI command view for templates. Provides consistent access to CLI command data.

Normalized CLI command view for templates.

Provides consistent access to CLI command data.

Attributes

Name Type Description
name str

Command name

description str

Command description/help text

usage str

Usage string (if available)

href str

Link to command page

is_group bool

Whether this is a command group

is_hidden bool

Whether command is hidden

option_count int

Number of options

argument_count int

Number of arguments

subcommand_count int

Number of subcommands (if group)

typed_metadata Any

Full CLICommandMetadata (for advanced use)

Methods

from_doc_element 1 CommandView
Create from DocElement.
classmethod
def from_doc_element(cls, el: DocElementLike) -> CommandView
Parameters
Name Type Description
el
Returns
CommandView

Functions

members_filter 1 list[MemberView]
Normalize Python element members for templates. Returns a list of MemberView o…
def members_filter(element: DocElement | None) -> list[MemberView]

Normalize Python element members for templates.

Returns a list of MemberView objects for methods, functions, and properties.

Usage: {% for m in element | members %} {{ m.name }} {% if m.is_async %}async{% end %} {% end %}

Parameters
Name Type Description
element DocElement | None

DocElement containing children (class, module)

Returns
list[MemberView]
commands_filter 1 list[CommandView]
Normalize CLI element commands for templates. Returns a list of CommandView ob…
def commands_filter(element: DocElement | None) -> list[CommandView]

Normalize CLI element commands for templates.

Returns a list of CommandView objects for commands and command groups.

Usage: {% for cmd in element | commands %} {{ cmd.name }} {{ cmd.option_count }} options {% end %}

Parameters
Name Type Description
element DocElement | None

DocElement containing children (CLI group)

Returns
list[CommandView]
options_filter 1 list[OptionView]
Normalize CLI element options for templates. Returns a list of OptionView obje…
def options_filter(element: DocElement | None) -> list[OptionView]

Normalize CLI element options for templates.

Returns a list of OptionView objects for options and arguments.

Usage: {% for opt in element | options %} {{ opt.flags_str }} {{ opt.description }} {% if opt.required %}required{% end %} {% end %}

Parameters
Name Type Description
element DocElement | None

DocElement containing children (CLI command)

Returns
list[OptionView]
member_view_filter 1 MemberView | None
Convert a single DocElement to a MemberView. Use this when iterating over a pr…
def member_view_filter(element: DocElement | None) -> MemberView | None

Convert a single DocElement to a MemberView.

Use this when iterating over a pre-filtered list of DocElements and you want normalized access to each member's properties.

Usage: {% for el in members %} {% let m = el | member_view %} {{ m.name }} {% if m.is_async %}async{% end %} {% end %}

Parameters
Name Type Description
element DocElement | None

DocElement to convert

Returns
MemberView | None
option_view_filter 1 OptionView | None
Convert a single DocElement to an OptionView. Use this when iterating over a p…
def option_view_filter(element: DocElement | None) -> OptionView | None

Convert a single DocElement to an OptionView.

Use this when iterating over a pre-filtered list of DocElements and you want normalized access to each option's properties.

Usage: {% for el in options %} {% let opt = el | option_view %} {{ opt.flags_str }} {% end %}

Parameters
Name Type Description
element DocElement | None

DocElement to convert

Returns
OptionView | None
command_view_filter 1 CommandView | None
Convert a single DocElement to a CommandView. Use this when iterating over a p…
def command_view_filter(element: DocElement | None) -> CommandView | None

Convert a single DocElement to a CommandView.

Use this when iterating over a pre-filtered list of DocElements and you want normalized access to each command's properties.

Usage: {% for el in commands %} {% let cmd = el | command_view %} {{ cmd.name }} {% end %}

Parameters
Name Type Description
element DocElement | None

DocElement to convert

Returns
CommandView | None
is_autodoc_page 1 bool
Check if a page is autodoc-generated (template helper). This is a template-fri…
def is_autodoc_page(page: Any) -> bool

Check if a page is autodoc-generated (template helper).

This is a template-friendly wrapper around bengal.utils.autodoc.is_autodoc_page that can be used in Jinja templates.

Parameters
Name Type Description
page Any

Page object to check

Returns
bool
register 2 None
Register functions with template environment.
def register(env: TemplateEnvironment, site: SiteLike) -> None
Parameters
Name Type Description
env TemplateEnvironment
site SiteLike
get_params 2 list[dict[str, Any]]
Get normalized parameters for any DocElement with parameters. **Returns a list…
def get_params(element: DocElement, exclude_self: bool = True) -> list[dict[str, Any]]

Get normalized parameters for any DocElement with parameters.

Returns a list of dicts with consistent keys:

  • name: Parameter name
  • type: Type annotation (or None)
  • default: Default value (or None)
  • required: Whether required
  • description: Description text

Usage in templates: {% for param in element | get_params %} {{ param.name }}: {{ param.type }} {% endfor %}

Parameters
Name Type Description
element DocElement

DocElement (function, method, CLI command, OpenAPI endpoint)

exclude_self bool

Exclude 'self' and 'cls' parameters (default True)

Default:True
Returns
list[dict[str, Any]]
param_count 2 int
Get count of parameters for an element. **Usage in templates:** {{ element | p…
def param_count(element: DocElement, exclude_self: bool = True) -> int

Get count of parameters for an element.

Usage in templates: {{ element | param_count }} parameters

Parameters
Name Type Description
element DocElement

DocElement with parameters

exclude_self bool

Exclude 'self' and 'cls' (default True)

Default:True
Returns
int
return_type 1 str
Get return type string for an element. **Usage in templates:** Returns: {{ ele…
def return_type(element: DocElement) -> str

Get return type string for an element.

Usage in templates: Returns: {{ element | return_type }}

Parameters
Name Type Description
element DocElement

DocElement (function, method, etc.)

Returns
str
get_return_info 1 dict[str, Any]
Get normalized return info for an element. **Returns a dict with:** - type: Re…
def get_return_info(element: DocElement) -> dict[str, Any]

Get normalized return info for an element.

Returns a dict with:

  • type: Return type string (or None)
  • description: Return description (or None)

Usage in templates: {% set ret = element | get_return_info %} {% if ret.type and ret.type != 'None' %} Returns: {{ ret.type }} {% if ret.description %} — {{ ret.description }}{% endif %} {% endif %}

Parameters
Name Type Description
element DocElement

DocElement (function, method, etc.)

Returns
dict[str, Any]
get_element_stats 1 list[dict[str, Any]]
Extract display stats from a DocElement based on its children types. Counts ch…
def get_element_stats(element: DocElement) -> list[dict[str, Any]]

Extract display stats from a DocElement based on its children types.

Counts children by element_type and returns a list of stats suitable for rendering in templates.

Usage in templates: {% set stats = element | get_element_stats %} {% if stats %}

{% for stat in stats %} {{ stat.value }} {{ stat.label }} {% endfor %}
{% endif %}

Parameters
Name Type Description
element DocElement

DocElement with children to count

Returns
list[dict[str, Any]]
children_by_type 2 list[Any]
Filter children by element_type. **This replaces verbose Jinja filter chains l…
def children_by_type(children: list[Any], element_type: str) -> list[Any]

Filter children by element_type.

This replaces verbose Jinja filter chains like: {% set methods = children | selectattr('element_type', 'eq', 'method') | list %}

With a simple function call: {% set methods = children_by_type(children, 'method') %}

Note: This function is portable across template engines because it's pure Python and can be injected as a context global in any renderer.

Parameters
Name Type Description
children list[Any]

List of child elements (DocElement or similar)

element_type str

Type to filter (method, function, class, attribute, etc.)

Returns
list[Any]
public_only 1 list[Any]
Filter to members not starting with underscore. **This replaces verbose Jinja …
def public_only(members: list[Any]) -> list[Any]

Filter to members not starting with underscore.

This replaces verbose Jinja filter chains like: {% set public = members | rejectattr('name', 'startswith', '_') | list %}

With a simple function call: {% set public = public_only(members) %}

Note: This function is portable across template engines because it's pure Python and can be injected as a context global in any renderer.

Parameters
Name Type Description
members list[Any]

List of elements with a 'name' attribute

Returns
list[Any]
private_only 1 list[Any]
Filter to members starting with underscore (internal). **This replaces verbose…
def private_only(members: list[Any]) -> list[Any]

Filter to members starting with underscore (internal).

This replaces verbose Jinja filter chains like: {% set private = members | selectattr('name', 'startswith', '_') | list %}

With a simple function call: {% set private = private_only(members) %}

Note: This function is portable across template engines because it's pure Python and can be injected as a context global in any renderer.

Parameters
Name Type Description
members list[Any]

List of elements with a 'name' attribute

Returns
list[Any]