mkdocstrings ¤
mkdocstrings package.
Automatic documentation from sources, for MkDocs.
Classes:
-
AutoDocProcessor–Our "autodoc" Markdown block processor.
-
BaseHandler–The base handler class.
-
CollectionError–An exception raised when some collection of data failed.
-
Handlers–A collection of handlers.
-
HeadingShiftingTreeprocessor–Shift levels of all Markdown headings according to the configured base level.
-
Highlighter–Code highlighter that tries to match the Markdown configuration.
-
IdPrependingTreeprocessor–Prepend the configured prefix to IDs of all HTML elements.
-
Inventory–Inventory of collected and rendered objects.
-
InventoryItem–Inventory item.
-
LoggerAdapter–A logger adapter to prefix messages.
-
MkdocstringsExtension–Our Markdown extension.
-
MkdocstringsInnerExtension–Extension that should always be added to Markdown sub-documents that handlers request (and only them).
-
MkdocstringsPlugin–An
mkdocsplugin. -
ParagraphStrippingTreeprocessor–Unwraps the
<p>element around the whole output. -
PluginConfig–The configuration options of
mkdocstrings, written inmkdocs.yml. -
TemplateLogger–A wrapper class to allow logging in templates.
-
ThemeNotSupported–An exception raised to tell a theme is not supported.
Functions:
-
do_any–Check if at least one of the item in the sequence evaluates to true.
-
get_logger–Return a pre-configured logger.
-
get_template_logger–Return a logger usable in templates.
-
get_template_logger_function–Create a wrapper function that automatically receives the Jinja template context.
-
get_template_path–Return the path to the template currently using the given context.
-
makeExtension–Create the extension instance.
Attributes:
-
CollectorItem–The type of the item returned by the
collectmethod of a handler. -
HandlerConfig–The type of the configuration of a handler.
-
HandlerOptions–The type of the options passed to a handler.
-
TEMPLATES_DIRS(Sequence[Path]) –The directories where the handler templates are located.
CollectorItem module-attribute ¤
CollectorItem = Any
The type of the item returned by the collect method of a handler.
HandlerConfig module-attribute ¤
HandlerConfig = Any
The type of the configuration of a handler.
HandlerOptions module-attribute ¤
HandlerOptions = Any
The type of the options passed to a handler.
TEMPLATES_DIRS module-attribute ¤
The directories where the handler templates are located.
AutoDocProcessor ¤
Bases: BlockProcessor
Our "autodoc" Markdown block processor.
It has a test method that tells if a block matches a criterion, and a run method that processes it.
It also has utility methods allowing to get handlers and their configuration easily, useful when processing a matched block.
Parameters:
-
(md¤Markdown) –A
markdown.Markdowninstance. -
(handlers¤Handlers) –The handlers container.
-
(autorefs¤AutorefsPlugin) –The autorefs plugin instance.
Methods:
Attributes:
Source code in src/mkdocstrings/_internal/extension.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
regex class-attribute instance-attribute ¤
The regular expression to match our autodoc instructions.
run ¤
run(parent: Element, blocks: MutableSequence[str]) -> None
Run code on the matched blocks.
The identifier and configuration lines are retrieved from a matched block and used to collect and render an object.
Parameters:
-
(parent¤Element) –The parent element in the XML tree.
-
(blocks¤MutableSequence[str]) –The rest of the blocks to be processed.
Source code in src/mkdocstrings/_internal/extension.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
test ¤
Match our autodoc instructions.
Parameters:
Returns:
-
bool–Whether this block should be processed or not.
Source code in src/mkdocstrings/_internal/extension.py
85 86 87 88 89 90 91 92 93 94 95 | |
BaseHandler ¤
BaseHandler(
*,
theme: str,
custom_templates: str | None,
mdx: Sequence[str | Extension],
mdx_config: Mapping[str, Any],
)
The base handler class.
Inherit from this class to implement a handler.
You will have to implement the collect and render methods. You can also implement the teardown method, and override the update_env method, to add more filters to the Jinja environment, making them available in your Jinja templates.
To define a fallback theme, add a fallback_theme class-variable. To add custom CSS, add an extra_css variable or create an 'style.css' file beside the templates.
If the given theme is not supported (it does not exist), it will look for a fallback_theme attribute in self to use as a fallback theme.
Other Parameters:
-
theme(str) –The theme to use.
-
custom_templates(str | None) –The path to custom templates.
-
mdx(list[str | Extension]) –A list of Markdown extensions to use.
-
mdx_config(Mapping[str, Mapping[str, Any]]) –Configuration for the Markdown extensions.
Methods:
-
collect–Collect data given an identifier and user configuration.
-
do_convert_markdown–Render Markdown text; for use inside templates.
-
do_heading–Render an HTML heading and register it for the table of contents. For use inside templates.
-
get_aliases–Return the possible aliases for a given identifier.
-
get_extended_templates_dirs–Load template extensions for the given handler, return their templates directories.
-
get_headings–Return and clear the headings gathered so far.
-
get_inventory_urls–Return the URLs (and configuration options) of the inventory files to download.
-
get_options–Get combined options.
-
get_templates_dir–Return the path to the handler's templates directory.
-
load_inventory–Yield items and their URLs from an inventory file streamed from
in_file. -
render–Render a template using provided data and configuration options.
-
render_backlinks–Render backlinks.
-
teardown–Teardown the handler.
-
update_env–Update the Jinja environment.
Attributes:
-
custom_templates–The path to custom templates.
-
domain(str) –The handler's domain, used to register objects in the inventory, for example "py".
-
enable_inventory(bool) –Whether the inventory creation is enabled.
-
env–The Jinja environment.
-
extra_css(str) –Extra CSS.
-
fallback_theme(str) –Fallback theme to use when a template isn't found in the configured theme.
-
md(Markdown) –The Markdown instance.
-
mdx–The Markdown extensions to use.
-
mdx_config–The configuration for the Markdown extensions.
-
name(str) –The handler's name, for example "python".
-
outer_layer(bool) –Whether we're in the outer Markdown conversion layer.
-
theme–The selected theme.
Source code in src/mkdocstrings/_internal/handlers/base.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
custom_templates instance-attribute ¤
custom_templates = custom_templates
The path to custom templates.
domain class-attribute ¤
domain: str
The handler's domain, used to register objects in the inventory, for example "py".
enable_inventory class-attribute ¤
enable_inventory: bool = False
Whether the inventory creation is enabled.
env instance-attribute ¤
env = Environment(
autoescape=True,
loader=FileSystemLoader(paths),
auto_reload=False,
)
The Jinja environment.
fallback_theme class-attribute ¤
fallback_theme: str = ''
Fallback theme to use when a template isn't found in the configured theme.
mdx_config instance-attribute ¤
mdx_config = mdx_config
The configuration for the Markdown extensions.
collect ¤
collect(
identifier: str, options: HandlerOptions
) -> CollectorItem
Collect data given an identifier and user configuration.
In the implementation, you typically call a subprocess that returns JSON, and load that JSON again into a Python dictionary for example, though the implementation is completely free.
Parameters:
-
(identifier¤str) –An identifier for which to collect data. For example, in Python, it would be 'mkdocstrings.handlers' to collect documentation about the handlers module. It can be anything that you can feed to the tool of your choice.
-
(options¤HandlerOptions) –The final configuration options.
Returns:
-
CollectorItem–Anything you want, as long as you can feed it to the handler's
rendermethod.
Source code in src/mkdocstrings/_internal/handlers/base.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
do_convert_markdown ¤
do_convert_markdown(
text: str,
heading_level: int,
html_id: str = "",
*,
strip_paragraph: bool = False,
autoref_hook: AutorefsHookInterface | None = None,
) -> Markup
Render Markdown text; for use inside templates.
Parameters:
-
(text¤str) –The text to convert.
-
(heading_level¤int) –The base heading level to start all Markdown headings from.
-
(html_id¤str, default:'') –The HTML id of the element that's considered the parent of this element.
-
(strip_paragraph¤bool, default:False) –Whether to exclude the
<p>tag from around the whole output.
Returns:
-
Markup–An HTML string.
Source code in src/mkdocstrings/_internal/handlers/base.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
do_heading ¤
do_heading(
content: Markup,
heading_level: int,
*,
role: str | None = None,
hidden: bool = False,
toc_label: str | None = None,
skip_inventory: bool = False,
**attributes: str,
) -> Markup
Render an HTML heading and register it for the table of contents. For use inside templates.
Parameters:
-
(content¤Markup) –The HTML within the heading.
-
(heading_level¤int) –The level of heading (e.g. 3 ->
h3). -
(role¤str | None, default:None) –An optional role for the object bound to this heading.
-
(hidden¤bool, default:False) –If True, only register it for the table of contents, don't render anything.
-
(toc_label¤str | None, default:None) –The title to use in the table of contents ('data-toc-label' attribute).
-
(skip_inventory¤bool, default:False) –Flag element to not be registered in the inventory (by setting a
data-skip-inventoryattribute). -
(**attributes¤str, default:{}) –Any extra HTML attributes of the heading.
Returns:
-
Markup–An HTML string.
Source code in src/mkdocstrings/_internal/handlers/base.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
get_aliases ¤
get_aliases(identifier: str) -> tuple[str, ...]
Return the possible aliases for a given identifier.
Parameters:
Returns:
Source code in src/mkdocstrings/_internal/handlers/base.py
325 326 327 328 329 330 331 332 333 334 | |
get_extended_templates_dirs ¤
Load template extensions for the given handler, return their templates directories.
Parameters:
Returns:
Source code in src/mkdocstrings/_internal/handlers/base.py
313 314 315 316 317 318 319 320 321 322 323 | |
get_headings ¤
Return and clear the headings gathered so far.
Returns:
Source code in src/mkdocstrings/_internal/handlers/base.py
456 457 458 459 460 461 462 463 464 | |
get_inventory_urls ¤
Return the URLs (and configuration options) of the inventory files to download.
Source code in src/mkdocstrings/_internal/handlers/base.py
196 197 198 | |
get_options ¤
get_options(
local_options: Mapping[str, Any],
) -> HandlerOptions
Get combined options.
Override this method to customize how options are combined, for example by merging the global options with the local options. By combining options here, you don't have to do it twice in collect and render.
Parameters:
Returns:
-
HandlerOptions–The combined options.
Source code in src/mkdocstrings/_internal/handlers/base.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
get_templates_dir ¤
Return the path to the handler's templates directory.
Override to customize how the templates directory is found.
Parameters:
Raises:
-
ModuleNotFoundError–When no such handler is installed.
-
FileNotFoundError–When the templates directory cannot be found.
Returns:
-
Path–The templates directory path.
Source code in src/mkdocstrings/_internal/handlers/base.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
load_inventory classmethod ¤
load_inventory(
in_file: BinaryIO,
url: str,
base_url: str | None = None,
**kwargs: Any,
) -> Iterator[tuple[str, str]]
Yield items and their URLs from an inventory file streamed from in_file.
Parameters:
-
(in_file¤BinaryIO) –The binary file-like object to read the inventory from.
-
(url¤str) –The URL that this file is being streamed from (used to guess
base_url). -
(base_url¤str | None, default:None) –The URL that this inventory's sub-paths are relative to.
-
(**kwargs¤Any, default:{}) –Ignore additional arguments passed from the config.
Yields:
Source code in src/mkdocstrings/_internal/handlers/base.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
render ¤
render(
data: CollectorItem,
options: HandlerOptions,
*,
locale: str | None = None,
) -> str
Render a template using provided data and configuration options.
Parameters:
-
(data¤CollectorItem) –The collected data to render.
-
(options¤HandlerOptions) –The final configuration options.
-
(locale¤str | None, default:None) –The locale to use for translations, if any.
Returns:
-
str–The rendered template as HTML.
Source code in src/mkdocstrings/_internal/handlers/base.py
253 254 255 256 257 258 259 260 261 262 263 264 | |
render_backlinks ¤
render_backlinks(
backlinks: Mapping[str, Iterable[Backlink]],
*,
locale: str | None = None,
) -> str
Render backlinks.
Parameters:
-
(backlinks¤Mapping[str, Iterable[Backlink]]) –A mapping of identifiers to backlinks.
-
(locale¤str | None, default:None) –The locale to use for translations, if any.
Returns:
-
str–The rendered backlinks as HTML.
Source code in src/mkdocstrings/_internal/handlers/base.py
266 267 268 269 270 271 272 273 274 275 276 | |
teardown ¤
teardown() -> None
Teardown the handler.
This method should be implemented to, for example, terminate a subprocess that was started when creating the handler instance.
Source code in src/mkdocstrings/_internal/handlers/base.py
278 279 280 281 282 283 | |
Handlers ¤
Handlers(
*,
theme: str,
default: str,
inventory_project: str,
inventory_version: str = "0.0.0",
handlers_config: dict[str, HandlerConfig] | None = None,
custom_templates: str | None = None,
mdx: Sequence[str | Extension] | None = None,
mdx_config: Mapping[str, Any] | None = None,
locale: str = "en",
tool_config: Any,
)
A collection of handlers.
Do not instantiate this directly. The plugin will keep one instance of this for the purpose of caching. Use mkdocstrings.MkdocstringsPlugin.get_handler for convenient access.
Parameters:
-
(theme¤str) –The theme to use.
-
(default¤str) –The default handler to use.
-
(inventory_project¤str) –The project name to use in the inventory.
-
(inventory_version¤str, default:'0.0.0') –The project version to use in the inventory.
-
(handlers_config¤dict[str, HandlerConfig] | None, default:None) –The handlers configuration.
-
(custom_templates¤str | None, default:None) –The path to custom templates.
-
(mdx¤Sequence[str | Extension] | None, default:None) –A list of Markdown extensions to use.
-
(mdx_config¤Mapping[str, Any] | None, default:None) –Configuration for the Markdown extensions.
-
(locale¤str, default:'en') –The locale to use for translations.
-
(tool_config¤Any) –Tool configuration to pass down to handlers.
Methods:
-
get_handler–Get a handler thanks to its name.
-
get_handler_config–Return the global configuration of the given handler.
-
get_handler_name–Return the handler name defined in an "autodoc" instruction YAML configuration, or the global default handler.
-
teardown–Teardown all cached handlers and clear the cache.
Attributes:
-
inventory(Inventory) –The objects inventory.
-
seen_handlers(Iterable[BaseHandler]) –Get the handlers that were encountered so far throughout the build.
Source code in src/mkdocstrings/_internal/handlers/base.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | |
inventory instance-attribute ¤
The objects inventory.
seen_handlers property ¤
seen_handlers: Iterable[BaseHandler]
Get the handlers that were encountered so far throughout the build.
Returns:
-
Iterable[BaseHandler]–An iterable of instances of
BaseHandler -
Iterable[BaseHandler]–(usable only to loop through it).
get_handler ¤
get_handler(
name: str, handler_config: dict | None = None
) -> BaseHandler
Get a handler thanks to its name.
This function dynamically imports a module named "mkdocstrings.handlers.NAME", calls its get_handler method to get an instance of a handler, and caches it in dictionary. It means that during one run (for each reload when serving, or once when building), a handler is instantiated only once, and reused for each "autodoc" instruction asking for it.
Parameters:
-
(name¤str) –The name of the handler. Really, it's the name of the Python module holding it.
-
(handler_config¤dict | None, default:None) –Configuration passed to the handler.
Returns:
-
BaseHandler–An instance of a subclass of
BaseHandler, as instantiated by theget_handlermethod of the handler's module.
Source code in src/mkdocstrings/_internal/handlers/base.py
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | |
get_handler_config ¤
Return the global configuration of the given handler.
Parameters:
Returns:
-
dict–The global configuration of the given handler. It can be an empty dictionary.
Source code in src/mkdocstrings/_internal/handlers/base.py
549 550 551 552 553 554 555 556 557 558 | |
get_handler_name ¤
Return the handler name defined in an "autodoc" instruction YAML configuration, or the global default handler.
Parameters:
Returns:
-
str–The name of the handler to use.
Source code in src/mkdocstrings/_internal/handlers/base.py
538 539 540 541 542 543 544 545 546 547 | |
teardown ¤
teardown() -> None
Teardown all cached handlers and clear the cache.
Source code in src/mkdocstrings/_internal/handlers/base.py
658 659 660 661 662 663 664 | |
HeadingShiftingTreeprocessor ¤
Bases: Treeprocessor
Shift levels of all Markdown headings according to the configured base level.
Parameters:
-
(md¤Markdown) –A
markdown.Markdowninstance. -
(shift_by¤int) –The number of heading "levels" to add to every heading.
Methods:
-
run–Shift the levels of all headings in the document.
Attributes:
-
name(str) –The name of the treeprocessor.
-
regex(Pattern) –The regex to match heading tags.
-
shift_by(int) –The number of heading "levels" to add to every heading.
<h2>withshift_by = 3becomes<h5>.
Source code in src/mkdocstrings/_internal/handlers/rendering.py
203 204 205 206 207 208 209 210 211 | |
name class-attribute instance-attribute ¤
name: str = 'mkdocstrings_headings'
The name of the treeprocessor.
regex class-attribute instance-attribute ¤
The regex to match heading tags.
shift_by instance-attribute ¤
shift_by: int = shift_by
The number of heading "levels" to add to every heading. <h2> with shift_by = 3 becomes <h5>.
run ¤
run(root: Element) -> None
Shift the levels of all headings in the document.
Source code in src/mkdocstrings/_internal/handlers/rendering.py
213 214 215 216 217 218 219 220 221 222 | |
Highlighter ¤
Bases: Highlight
Code highlighter that tries to match the Markdown configuration.
Picking up the global config and defaults works only if you use the codehilite or pymdownx.highlight (recommended) Markdown extension.
-
If you use
pymdownx.highlight, highlighting settings are picked up from it, and the default CSS class is.highlight. This also means the default ofguess_lang: false. -
Otherwise, if you use the
codehiliteextension, settings are picked up from it, and the default CSS class is.codehilite. Also consider settingguess_lang: false. -
If neither are added to
markdown_extensions, highlighting is enabled anyway. This is for backwards compatibility. If you really want to disable highlighting even in mkdocstrings, add one of these extensions anyway and setuse_pygments: false.
The underlying implementation is pymdownx.highlight regardless.
Parameters:
Methods:
-
highlight–Highlight a code-snippet.
Source code in src/mkdocstrings/_internal/handlers/rendering.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
highlight ¤
highlight(
src: str,
language: str | None = None,
*,
inline: bool = False,
dedent: bool = True,
linenums: bool | None = None,
**kwargs: Any,
) -> str
Highlight a code-snippet.
Parameters:
-
(src¤str) –The code to highlight.
-
(language¤str | None, default:None) –Explicitly tell what language to use for highlighting.
-
(inline¤bool, default:False) –Whether to highlight as inline.
-
(dedent¤bool, default:True) –Whether to dedent the code before highlighting it or not.
-
(linenums¤bool | None, default:None) –Whether to add line numbers in the result.
-
(**kwargs¤Any, default:{}) –Pass on to
pymdownx.highlight.Highlight.highlight.
Returns:
-
str–The highlighted code as HTML text, marked safe (not escaped for HTML).
Source code in src/mkdocstrings/_internal/handlers/rendering.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
IdPrependingTreeprocessor ¤
Bases: Treeprocessor
Prepend the configured prefix to IDs of all HTML elements.
Parameters:
-
(md¤Markdown) –A
markdown.Markdowninstance. -
(id_prefix¤str) –The prefix to add to every ID. It is prepended without any separator.
Methods:
-
run–Prepend the configured prefix to all IDs in the document.
Attributes:
-
id_prefix(str) –The prefix to add to every ID. It is prepended without any separator; specify your own separator if needed.
-
name(str) –The name of the treeprocessor.
Source code in src/mkdocstrings/_internal/handlers/rendering.py
142 143 144 145 146 147 148 149 150 | |
id_prefix instance-attribute ¤
id_prefix: str = id_prefix
The prefix to add to every ID. It is prepended without any separator; specify your own separator if needed.
name class-attribute instance-attribute ¤
name: str = 'mkdocstrings_ids'
The name of the treeprocessor.
run ¤
run(root: Element) -> None
Prepend the configured prefix to all IDs in the document.
Source code in src/mkdocstrings/_internal/handlers/rendering.py
152 153 154 155 | |
Inventory ¤
Inventory(
items: list[InventoryItem] | None = None,
project: str = "project",
version: str = "0.0.0",
)
Bases: dict
Inventory of collected and rendered objects.
Parameters:
-
(items¤list[InventoryItem] | None, default:None) –A list of items.
-
(project¤str, default:'project') –The project name.
-
(version¤str, default:'0.0.0') –The project version.
Methods:
-
format_sphinx–Format this inventory as a Sphinx
objects.invfile. -
parse_sphinx–Parse a Sphinx v2 inventory file and return an
Inventoryfrom it. -
register–Create and register an item.
Attributes:
Source code in src/mkdocstrings/_internal/inventory.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
format_sphinx ¤
format_sphinx() -> bytes
Format this inventory as a Sphinx objects.inv file.
Returns:
-
bytes–The inventory as bytes.
Source code in src/mkdocstrings/_internal/inventory.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
parse_sphinx classmethod ¤
parse_sphinx(
in_file: BinaryIO,
*,
domain_filter: Collection[str] = (),
) -> Inventory
Parse a Sphinx v2 inventory file and return an Inventory from it.
Parameters:
-
(in_file¤BinaryIO) –The binary file-like object to read from.
-
(domain_filter¤Collection[str], default:()) –A collection of domain values to allow (and filter out all other ones).
Returns:
-
Inventory–An inventory containing the collected items.
Source code in src/mkdocstrings/_internal/inventory.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
register ¤
register(
name: str,
domain: str,
role: str,
uri: str,
priority: int = 1,
dispname: str | None = None,
) -> None
Create and register an item.
Parameters:
-
(name¤str) –The item name.
-
(domain¤str) –The item domain, like 'python' or 'crystal'.
-
(role¤str) –The item role, like 'class' or 'method'.
-
(uri¤str) –The item URI.
-
(priority¤int, default:1) –The item priority. Only used internally by mkdocstrings and Sphinx.
-
(dispname¤str | None, default:None) –The item display name.
Source code in src/mkdocstrings/_internal/inventory.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
InventoryItem ¤
InventoryItem(
name: str,
domain: str,
role: str,
uri: str,
priority: int = 1,
dispname: str | None = None,
)
Inventory item.
Parameters:
-
(name¤str) –The item name.
-
(domain¤str) –The item domain, like 'python' or 'crystal'.
-
(role¤str) –The item role, like 'class' or 'method'.
-
(uri¤str) –The item URI.
-
(priority¤int, default:1) –The item priority. Only used internally by mkdocstrings and Sphinx.
-
(dispname¤str | None, default:None) –The item display name.
Methods:
-
format_sphinx–Format this item as a Sphinx inventory line.
-
parse_sphinx–Parse a line from a Sphinx v2 inventory file and return an
InventoryItemfrom it.
Attributes:
-
dispname(str) –The item display name.
-
domain(str) –The item domain.
-
name(str) –The item name.
-
priority(int) –The item priority.
-
role(str) –The item role.
-
sphinx_item_regex–Regex to parse a Sphinx v2 inventory line.
-
uri(str) –The item URI.
Source code in src/mkdocstrings/_internal/inventory.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
sphinx_item_regex class-attribute instance-attribute ¤
sphinx_item_regex = compile(
"^(.+?)\\s+(\\S+):(\\S+)\\s+(-?\\d+)\\s+(\\S+)\\s*(.*)$"
)
Regex to parse a Sphinx v2 inventory line.
format_sphinx ¤
format_sphinx() -> str
Format this item as a Sphinx inventory line.
Returns:
-
str–A line formatted for an
objects.invfile.
Source code in src/mkdocstrings/_internal/inventory.py
52 53 54 55 56 57 58 59 60 61 62 63 64 | |
parse_sphinx classmethod ¤
parse_sphinx(
line: str, *, return_none: Literal[False]
) -> InventoryItem
parse_sphinx(
line: str, *, return_none: Literal[True]
) -> InventoryItem | None
parse_sphinx(
line: str, *, return_none: bool = False
) -> InventoryItem | None
Parse a line from a Sphinx v2 inventory file and return an InventoryItem from it.
Source code in src/mkdocstrings/_internal/inventory.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
LoggerAdapter ¤
Bases: LoggerAdapter
A logger adapter to prefix messages.
This adapter also adds an additional parameter to logging methods called once: if True, the message will only be logged once.
Examples:
In Python code:
>>> logger = get_logger("myplugin")
>>> logger.debug("This is a debug message.")
>>> logger.info("This is an info message.", once=True)
In Jinja templates (logger available in context as log):
{{ log.debug("This is a debug message.") }}
{{ log.info("This is an info message.", once=True) }}
Parameters:
-
(prefix¤str) –The string to insert in front of every message.
-
(logger¤Logger) –The logger instance.
Methods:
Attributes:
-
prefix–The prefix to insert in front of every message.
Source code in src/mkdocstrings/_internal/loggers.py
48 49 50 51 52 53 54 55 56 57 58 | |
log ¤
Log a message.
Parameters:
-
(level¤int) –The logging level.
-
(msg¤object) –The message.
-
(*args¤object, default:()) –Additional arguments passed to parent method.
-
(**kwargs¤object, default:{}) –Additional keyword arguments passed to parent method.
Source code in src/mkdocstrings/_internal/loggers.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
process ¤
Process the message.
Parameters:
Returns:
Source code in src/mkdocstrings/_internal/loggers.py
60 61 62 63 64 65 66 67 68 69 70 | |
MkdocstringsExtension ¤
Bases: Extension
Our Markdown extension.
It cannot work outside of mkdocstrings.
Parameters:
-
(handlers¤Handlers) –The handlers container.
-
(autorefs¤AutorefsPlugin) –The autorefs plugin instance.
-
(**kwargs¤Any, default:{}) –Keyword arguments used by
markdown.extensions.Extension.
Methods:
-
extendMarkdown–Register the extension.
Source code in src/mkdocstrings/_internal/extension.py
319 320 321 322 323 324 325 326 327 328 329 | |
extendMarkdown ¤
Register the extension.
Add an instance of our AutoDocProcessor to the Markdown parser.
Parameters:
Source code in src/mkdocstrings/_internal/extension.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
MkdocstringsInnerExtension ¤
Bases: Extension
Extension that should always be added to Markdown sub-documents that handlers request (and only them).
Parameters:
-
(headings¤list[Element]) –A list that will be populated with all HTML heading elements encountered in the document.
Methods:
-
extendMarkdown–Register the extension.
Attributes:
-
headings–The list that will be populated with all HTML heading elements encountered in the document.
Source code in src/mkdocstrings/_internal/handlers/rendering.py
275 276 277 278 279 280 281 282 283 | |
headings instance-attribute ¤
headings = headings
The list that will be populated with all HTML heading elements encountered in the document.
extendMarkdown ¤
Register the extension.
Parameters:
Source code in src/mkdocstrings/_internal/handlers/rendering.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
MkdocstringsPlugin ¤
MkdocstringsPlugin()
Bases: BasePlugin[PluginConfig]
An mkdocs plugin.
This plugin defines the following event hooks:
on_configon_envon_post_build
Check the Developing Plugins page of mkdocs for more information about its plugin system.
Methods:
-
get_handler–Get a handler by its name. See mkdocstrings.Handlers.get_handler.
-
on_config–Instantiate our Markdown extension.
-
on_post_build–Teardown the handlers.
Attributes:
-
css_filename(str) –The path of the CSS file to write in the site directory.
-
handlers(Handlers) –Get the instance of mkdocstrings.Handlers for this plugin/build.
-
inventory_enabled(bool) –Tell if the inventory is enabled or not.
-
on_env–Extra actions that need to happen after all Markdown-to-HTML page rendering.
-
plugin_enabled(bool) –Tell if the plugin is enabled or not.
Source code in src/mkdocstrings/_internal/plugin.py
97 98 99 100 | |
css_filename class-attribute instance-attribute ¤
css_filename: str = 'assets/_mkdocstrings.css'
The path of the CSS file to write in the site directory.
handlers property ¤
handlers: Handlers
Get the instance of mkdocstrings.Handlers for this plugin/build.
Raises:
-
RuntimeError–If the plugin hasn't been initialized with a config.
Returns:
-
Handlers–An instance of mkdocstrings.Handlers (the same throughout the build).
on_env class-attribute instance-attribute ¤
on_env = CombinedEvent(
_on_env_load_inventories,
_on_env_add_css,
_on_env_write_inventory,
_on_env_apply_backlinks,
)
Extra actions that need to happen after all Markdown-to-HTML page rendering.
Hook for the on_env event.
- Gather results from background inventory download tasks.
- Write mkdocstrings' extra files (CSS, inventory) into the site directory.
- Apply backlinks to the HTML output of each page.
get_handler ¤
get_handler(handler_name: str) -> BaseHandler
Get a handler by its name. See mkdocstrings.Handlers.get_handler.
Parameters:
Returns:
-
BaseHandler–An instance of a subclass of
BaseHandler.
Source code in src/mkdocstrings/_internal/plugin.py
289 290 291 292 293 294 295 296 297 298 | |
on_config ¤
on_config(config: MkDocsConfig) -> MkDocsConfig | None
Instantiate our Markdown extension.
Hook for the on_config event. In this hook, we instantiate our MkdocstringsExtension and add it to the list of Markdown extensions used by mkdocs.
We pass this plugin's configuration dictionary to the extension when instantiating it (it will need it later when processing markdown to get handlers and their global configurations).
Parameters:
-
(config¤MkDocsConfig) –The MkDocs config object.
Returns:
-
MkDocsConfig | None–The modified config.
Source code in src/mkdocstrings/_internal/plugin.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
on_post_build ¤
Teardown the handlers.
Hook for the on_post_build event. This hook is used to teardown all the handlers that were instantiated and cached during documentation buildup.
For example, a handler could open a subprocess in the background and keep it open to feed it "autodoc" instructions and get back JSON data. If so, it should then close the subprocess at some point: the proper place to do this is in the handler's teardown method, which is indirectly called by this hook.
Parameters:
-
(config¤MkDocsConfig) –The MkDocs config object.
-
(**kwargs¤Any, default:{}) –Additional arguments passed by MkDocs.
Source code in src/mkdocstrings/_internal/plugin.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
ParagraphStrippingTreeprocessor ¤
Bases: Treeprocessor
Unwraps the <p> element around the whole output.
Methods:
-
run–Unwrap the root element if it's a single
<p>element.
Attributes:
name class-attribute instance-attribute ¤
name: str = 'mkdocstrings_strip_paragraph'
The name of the treeprocessor.
strip class-attribute instance-attribute ¤
strip: bool = False
Whether to strip <p> elements or not.
run ¤
Unwrap the root element if it's a single <p> element.
Source code in src/mkdocstrings/_internal/handlers/rendering.py
263 264 265 266 267 268 269 | |
PluginConfig ¤
Bases: Config
The configuration options of mkdocstrings, written in mkdocs.yml.
Attributes:
-
custom_templates–Location of custom templates to use when rendering API objects.
-
default_handler–The default handler to use. The value is the name of the handler module. Default is "python".
-
enable_inventory–Whether to enable object inventory creation.
-
enabled–Whether to enable the plugin. Default is true. If false, mkdocstrings will not collect or render anything.
-
handlers–Global configuration of handlers.
-
locale–The locale to use for translations.
custom_templates class-attribute instance-attribute ¤
custom_templates = Optional(Dir(exists=True))
Location of custom templates to use when rendering API objects.
Value should be the path of a directory relative to the MkDocs configuration file.
default_handler class-attribute instance-attribute ¤
default_handler = Type(str, default='python')
The default handler to use. The value is the name of the handler module. Default is "python".
enable_inventory class-attribute instance-attribute ¤
enable_inventory = Optional(Type(bool))
Whether to enable object inventory creation.
enabled class-attribute instance-attribute ¤
enabled = Type(bool, default=True)
Whether to enable the plugin. Default is true. If false, mkdocstrings will not collect or render anything.
handlers class-attribute instance-attribute ¤
handlers = Type(dict, default={})
Global configuration of handlers.
You can set global configuration per handler, applied everywhere, but overridable in each "autodoc" instruction. Example:
plugins:
- mkdocstrings:
handlers:
python:
options:
option1: true
option2: "value"
rust:
options:
option9: 2
TemplateLogger ¤
TemplateLogger(logger: LoggerAdapter)
A wrapper class to allow logging in templates.
The logging methods provided by this class all accept two parameters:
msg: The message to log.once: IfTrue, the message will only be logged once.
Methods:
-
debug–Function to log a DEBUG message.
-
info–Function to log an INFO message.
-
warning–Function to log a WARNING message.
-
error–Function to log an ERROR message.
-
critical–Function to log a CRITICAL message.
Parameters:
-
(logger¤LoggerAdapter) –A logger adapter.
Attributes:
-
critical–Log a CRITICAL message.
-
debug–Log a DEBUG message.
-
error–Log an ERROR message.
-
info–Log an INFO message.
-
warning–Log a WARNING message.
Source code in src/mkdocstrings/_internal/loggers.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
critical instance-attribute ¤
critical = get_template_logger_function(critical)
Log a CRITICAL message.
do_any ¤
Check if at least one of the item in the sequence evaluates to true.
The any builtin as a filter for Jinja templates.
Parameters:
-
(seq¤Sequence) –An iterable object.
-
(attribute¤str | None, default:None) –The attribute name to use on each object of the iterable.
Returns:
-
bool–A boolean telling if any object of the iterable evaluated to True.
Source code in src/mkdocstrings/_internal/handlers/base.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
get_logger ¤
get_logger(name: str) -> LoggerAdapter
Return a pre-configured logger.
Parameters:
Returns:
-
LoggerAdapter–A logger configured to work well in MkDocs.
Source code in src/mkdocstrings/_internal/loggers.py
193 194 195 196 197 198 199 200 201 202 203 | |
get_template_logger ¤
get_template_logger(
handler_name: str | None = None,
) -> TemplateLogger
Return a logger usable in templates.
Parameters:
Returns:
-
TemplateLogger–A template logger.
Source code in src/mkdocstrings/_internal/loggers.py
206 207 208 209 210 211 212 213 214 215 216 | |
get_template_logger_function ¤
get_template_logger_function(
logger_func: Callable,
) -> Callable
Create a wrapper function that automatically receives the Jinja template context.
Parameters:
Returns:
-
Callable–A function.
Source code in src/mkdocstrings/_internal/loggers.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
get_template_path ¤
Return the path to the template currently using the given context.
Parameters:
Returns:
-
str–The relative path to the template.
Source code in src/mkdocstrings/_internal/loggers.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
makeExtension ¤
makeExtension(
*,
default_handler: str | None = None,
inventory_project: str | None = None,
inventory_version: str | None = None,
handlers: dict[str, dict] | None = None,
custom_templates: str | None = None,
markdown_extensions: list[str | dict] | None = None,
locale: str | None = None,
config_file_path: str | None = None,
) -> MkdocstringsExtension
Create the extension instance.
We only support this function being used by Zensical. Consider this function private API.
Source code in src/mkdocstrings/_internal/extension.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |