base
Base module for handlers.
This module contains the base classes for implementing collectors, renderers, and the combination of the two: handlers.
It also provides two methods:
get_handler
, that will cache handlers into theHANDLERS_CACHE
dictionary.teardown
, that will teardown all the cached handlers, and then clear the cache.
BaseCollector
¤
The base collector class.
Inherit from this class to implement a collector.
You will have to implement the collect
method.
You can also implement the teardown
method.
collect(identifier, config)
¤
Collect data given an identifier and selection 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:
Name | Type | Description | Default |
---|---|---|---|
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. |
required |
config |
Mapping[str, Any]
|
The handler's configuraton options. |
required |
Returns:
Type | Description |
---|---|
CollectorItem
|
Anything you want, as long as you can feed it to the renderer's |
Source code in mkdocstrings/handlers/base.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
|
teardown()
¤
Teardown the collector.
This method should be implemented to, for example, terminate a subprocess that was started when creating the collector instance.
Source code in mkdocstrings/handlers/base.py
339 340 341 342 343 344 |
|
BaseHandler(*args, **kwargs)
¤
Bases: BaseCollector
, BaseRenderer
The base handler class.
Inherit from this class to implement a handler.
It's usually just a combination of a collector and a renderer, but you can make it as complex as you need.
Attributes:
Name | Type | Description |
---|---|---|
domain |
str
|
The cross-documentation domain/language for this handler. |
enable_inventory |
bool
|
Whether this handler is interested in enabling the creation
of the |
fallback_config |
dict
|
The configuration used to collect item during autorefs fallback. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
str | BaseCollector | BaseRenderer
|
Collector and renderer, or handler name, theme and custom_templates. |
()
|
**kwargs |
str | BaseCollector | BaseRenderer
|
Same thing, but with keyword arguments. |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
When the givin parameters are invalid. |
Source code in mkdocstrings/handlers/base.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
|
load_inventory(in_file, url, base_url=None, **kwargs)
classmethod
¤
Yield items and their URLs from an inventory file streamed from in_file
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_file |
BinaryIO
|
The binary file-like object to read the inventory from. |
required |
url |
str
|
The URL that this file is being streamed from (used to guess |
required |
base_url |
Optional[str]
|
The URL that this inventory's sub-paths are relative to. |
None
|
**kwargs |
Any
|
Ignore additional arguments passed from the config. |
{}
|
Yields:
Type | Description |
---|---|
Iterator[tuple[str, str]]
|
Tuples of (item identifier, item URL). |
Source code in mkdocstrings/handlers/base.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
|
BaseRenderer(handler, theme, custom_templates=None)
¤
The base renderer class.
Inherit from this class to implement a renderer.
You will have to implement the render
method.
You can also 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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
str
|
The name of the handler. |
required |
theme |
str
|
The name of theme to use. |
required |
custom_templates |
Optional[str]
|
Directory containing custom templates. |
None
|
Source code in mkdocstrings/handlers/base.py
78 79 80 81 82 83 84 85 86 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 |
|
do_convert_markdown(text, heading_level, html_id='', *, strip_paragraph=False)
¤
Render Markdown text; for use inside templates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The text to convert. |
required |
heading_level |
int
|
The base heading level to start all Markdown headings from. |
required |
html_id |
str
|
The HTML id of the element that's considered the parent of this element. |
''
|
strip_paragraph |
bool
|
Whether to exclude the tag from around the whole output. |
False
|
Returns:
Type | Description |
---|---|
Markup
|
An HTML string. |
Source code in mkdocstrings/handlers/base.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
do_heading(content, heading_level, *, role=None, hidden=False, toc_label=None, **attributes)
¤
Render an HTML heading and register it for the table of contents. For use inside templates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
The HTML within the heading. |
required |
heading_level |
int
|
The level of heading (e.g. 3 -> |
required |
role |
Optional[str]
|
An optional role for the object bound to this heading. |
None
|
hidden |
bool
|
If True, only register it for the table of contents, don't render anything. |
False
|
toc_label |
Optional[str]
|
The title to use in the table of contents ('data-toc-label' attribute). |
None
|
**attributes |
str
|
Any extra HTML attributes of the heading. |
{}
|
Returns:
Type | Description |
---|---|
Markup
|
An HTML string. |
Source code in mkdocstrings/handlers/base.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
get_anchors(data)
¤
Return the possible identifiers (HTML anchors) for a collected item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
CollectorItem
|
The collected data. |
required |
Returns:
Type | Description |
---|---|
Sequence[str]
|
The HTML anchors (without '#'), or an empty tuple if this item doesn't have an anchor. |
Source code in mkdocstrings/handlers/base.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
get_headings()
¤
Return and clear the headings gathered so far.
Returns:
Type | Description |
---|---|
Sequence[Element]
|
A list of HTML elements. |
Source code in mkdocstrings/handlers/base.py
278 279 280 281 282 283 284 285 286 |
|
get_templates_dir(handler)
¤
Return the path to the handler's templates directory.
Override to customize how the templates directory is found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
str
|
The name of the handler to get the templates directory of. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
When the templates directory cannot be found. |
Returns:
Type | Description |
---|---|
Path
|
The templates directory path. |
Source code in mkdocstrings/handlers/base.py
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 |
|
render(data, config)
¤
Render a template using provided data and configuration options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
CollectorItem
|
The collected data to render. |
required |
config |
Mapping[str, Any]
|
The handler's configuraton options. |
required |
Returns:
Type | Description |
---|---|
str
|
The rendered template as HTML. |
Source code in mkdocstrings/handlers/base.py
122 123 124 125 126 127 128 129 130 131 132 |
|
update_env(md, config)
¤
Update the Jinja environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
md |
Markdown
|
The Markdown instance. Useful to add functions able to convert Markdown into the environment filters. |
required |
config |
dict
|
Configuration options for |
required |
Source code in mkdocstrings/handlers/base.py
288 289 290 291 292 293 294 295 296 297 298 299 |
|
Handlers(config)
¤
A collection of handlers.
Do not instantiate this directly. The plugin will keep one instance of this for the purpose of caching. Use mkdocstrings.plugin.MkdocstringsPlugin.get_handler for convenient access.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
dict
|
Configuration options for |
required |
Source code in mkdocstrings/handlers/base.py
508 509 510 511 512 513 514 515 516 517 |
|
seen_handlers: Iterable[BaseHandler]
property
¤
Get the handlers that were encountered so far throughout the build.
Returns:
Type | Description |
---|---|
Iterable[BaseHandler]
|
An iterable of instances of |
Iterable[BaseHandler]
|
(usable only to loop through it). |
get_anchors(identifier)
¤
Return the canonical HTML anchor for the identifier, if any of the seen handlers can collect it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
identifier |
str
|
The identifier (one that collect can accept). |
required |
Returns:
Type | Description |
---|---|
Sequence[str]
|
A tuple of strings - anchors without '#', or an empty tuple if there isn't any identifier familiar with it. |
Source code in mkdocstrings/handlers/base.py
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
|
get_handler(name, handler_config=None)
¤
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 | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the handler. Really, it's the name of the Python module holding it. |
required |
handler_config |
Optional[dict]
|
Configuration passed to the handler. |
None
|
Returns:
Type | Description |
---|---|
BaseHandler
|
An instance of a subclass of |
BaseHandler
|
as instantiated by the |
Source code in mkdocstrings/handlers/base.py
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
|
get_handler_config(name)
¤
Return the global configuration of the given handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the handler to get the global configuration of. |
required |
Returns:
Type | Description |
---|---|
dict
|
The global configuration of the given handler. It can be an empty dictionary. |
Source code in mkdocstrings/handlers/base.py
552 553 554 555 556 557 558 559 560 561 562 563 564 |
|
get_handler_name(config)
¤
Return the handler name defined in an "autodoc" instruction YAML configuration, or the global default handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
dict
|
A configuration dictionary, obtained from YAML below the "autodoc" instruction. |
required |
Returns:
Type | Description |
---|---|
str
|
The name of the handler to use. |
Source code in mkdocstrings/handlers/base.py
538 539 540 541 542 543 544 545 546 547 548 549 550 |
|
teardown()
¤
Teardown all cached handlers and clear the cache.
Source code in mkdocstrings/handlers/base.py
614 615 616 617 618 |
|
do_any(seq, attribute=None)
¤
Check if at least one of the item in the sequence evaluates to true.
The any
builtin as a filter for Jinja templates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Sequence
|
An iterable object. |
required |
attribute |
str | None
|
The attribute name to use on each object of the iterable. |
None
|
Returns:
Type | Description |
---|---|
bool
|
A boolean telling if any object of the iterable evaluated to True. |
Source code in mkdocstrings/handlers/base.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|