| 1 |
.. _events: |
| 2 |
|
| 3 |
Event callbacks API |
| 4 |
=================== |
| 5 |
|
| 6 |
Connecting callback functions to events is a simple way to extend Sphinx, |
| 7 |
by hooking into the build process at various points. |
| 8 |
|
| 9 |
Use :meth:`.Sphinx.connect` in an extension's ``setup`` function, |
| 10 |
or a ``setup`` function in your project's :file:`conf.py`, |
| 11 |
to connect functions to the events: |
| 12 |
|
| 13 |
.. code-block:: python |
| 14 |
|
| 15 |
def source_read_handler(app, docname, source): |
| 16 |
print('do something here...') |
| 17 |
|
| 18 |
def setup(app): |
| 19 |
app.connect('source-read', source_read_handler) |
| 20 |
|
| 21 |
.. seealso:: |
| 22 |
|
| 23 |
Extensions can add their own events by using :meth:`.Sphinx.add_event`, |
| 24 |
and calling them them with |
| 25 |
:meth:`.EventManager.emit` or :meth:`.EventManager.emit_firstresult`. |
| 26 |
|
| 27 |
Core events overview |
| 28 |
-------------------- |
| 29 |
|
| 30 |
Below is an overview of the core event that happens during a build. |
| 31 |
|
| 32 |
.. code-block:: none |
| 33 |
|
| 34 |
1. event.config-inited(app,config) |
| 35 |
2. event.builder-inited(app) |
| 36 |
3. event.env-get-outdated(app, env, added, changed, removed) |
| 37 |
4. event.env-before-read-docs(app, env, docnames) |
| 38 |
|
| 39 |
for docname in docnames: |
| 40 |
5. event.env-purge-doc(app, env, docname) |
| 41 |
|
| 42 |
if doc changed and not removed: |
| 43 |
6. source-read(app, docname, source) |
| 44 |
7. run source parsers: text -> docutils.document |
| 45 |
- parsers can be added with the app.add_source_parser() API |
| 46 |
- event.include-read(app, relative_path, parent_docname, content) |
| 47 |
is called for each include directive |
| 48 |
8. apply transforms based on priority: docutils.document -> docutils.document |
| 49 |
- event.doctree-read(app, doctree) is called in the middle of transforms, |
| 50 |
transforms come before/after this event depending on their priority. |
| 51 |
|
| 52 |
9. event.env-merge-info(app, env, docnames, other) |
| 53 |
- if running in parallel mode, this event will be emitted for each process |
| 54 |
|
| 55 |
10. event.env-updated(app, env) |
| 56 |
11. event.env-get-updated(app, env) |
| 57 |
|
| 58 |
if environment is written to disk: |
| 59 |
12. event.env-check-consistency(app, env) |
| 60 |
|
| 61 |
13. event.write-started(app, builder) |
| 62 |
- This is called after ``app.parallel_ok`` has been set, |
| 63 |
which must not be altered by any event handler. |
| 64 |
|
| 65 |
# The updated-docs list can be builder dependent, but generally includes all new/changed documents, |
| 66 |
# plus any output from `env-get-updated`, and then all "parent" documents in the ToC tree |
| 67 |
# For builders that output a single page, they are first joined into a single doctree before post-transforms |
| 68 |
# or the doctree-resolved event is emitted |
| 69 |
for docname in updated-docs: |
| 70 |
14. apply post-transforms (by priority): docutils.document -> docutils.document |
| 71 |
15. event.doctree-resolved(app, doctree, docname) |
| 72 |
- In the event that any reference nodes fail to resolve, the following may emit: |
| 73 |
- event.missing-reference(app, env, node, contnode) |
| 74 |
- event.warn-missing-reference(app, domain, node) |
| 75 |
|
| 76 |
16. Generate output files |
| 77 |
17. event.build-finished(app, exception) |
| 78 |
|
| 79 |
Here is also a flow diagram of the events, |
| 80 |
within the context of the Sphinx build process: |
| 81 |
|
| 82 |
.. graphviz:: /_static/diagrams/sphinx_core_events_flow.dot |
| 83 |
:caption: Sphinx core events flow |
| 84 |
|
| 85 |
Core event details |
| 86 |
------------------ |
| 87 |
|
| 88 |
Here is a more detailed list of these events. |
| 89 |
|
| 90 |
.. event:: config-inited (app, config) |
| 91 |
|
| 92 |
:param app: :class:`.Sphinx` |
| 93 |
:param config: :class:`.Config` |
| 94 |
|
| 95 |
Emitted when the config object has been initialized. |
| 96 |
|
| 97 |
.. versionadded:: 1.8 |
| 98 |
|
| 99 |
.. event:: builder-inited (app) |
| 100 |
|
| 101 |
:param app: :class:`.Sphinx` |
| 102 |
|
| 103 |
Emitted when the builder object has been created |
| 104 |
(available as ``app.builder``). |
| 105 |
|
| 106 |
.. event:: env-get-outdated (app, env, added, changed, removed) |
| 107 |
|
| 108 |
:param app: :class:`.Sphinx` |
| 109 |
:param env: :class:`.BuildEnvironment` |
| 110 |
:param added: ``Set[str]`` |
| 111 |
:param changed: ``Set[str]`` |
| 112 |
:param removed: ``Set[str]`` |
| 113 |
:returns: ``Sequence[str]`` of additional docnames to re-read |
| 114 |
|
| 115 |
Emitted when the environment determines which source files have changed and |
| 116 |
should be re-read. |
| 117 |
*added*, *changed* and *removed* are sets of docnames |
| 118 |
that the environment has determined. |
| 119 |
You can return a list of docnames to re-read in addition to these. |
| 120 |
|
| 121 |
.. versionadded:: 1.1 |
| 122 |
|
| 123 |
.. event:: env-purge-doc (app, env, docname) |
| 124 |
|
| 125 |
:param app: :class:`.Sphinx` |
| 126 |
:param env: :class:`.BuildEnvironment` |
| 127 |
:param docname: ``str`` |
| 128 |
|
| 129 |
Emitted when all traces of a source file should be cleaned from the |
| 130 |
environment, that is, if the source file is removed or before it is freshly read. |
| 131 |
This is for extensions that keep their own caches |
| 132 |
in attributes of the environment. |
| 133 |
|
| 134 |
For example, there is a cache of all modules on the environment. |
| 135 |
When a source file has been changed, the cache's entries for the file are cleared, |
| 136 |
since the module declarations could have been removed from the file. |
| 137 |
|
| 138 |
.. versionadded:: 0.5 |
| 139 |
|
| 140 |
.. event:: env-before-read-docs (app, env, docnames) |
| 141 |
|
| 142 |
:param app: :class:`.Sphinx` |
| 143 |
:param env: :class:`.BuildEnvironment` |
| 144 |
:param docnames: ``list[str]`` |
| 145 |
|
| 146 |
Emitted after the environment has determined the list of all added and |
| 147 |
changed files and just before it reads them. |
| 148 |
It allows extension authors to reorder |
| 149 |
the list of docnames (*inplace*) before processing, |
| 150 |
or add more docnames that Sphinx did not consider changed |
| 151 |
(but never add any docnames that are not in :attr:`.found_docs`). |
| 152 |
|
| 153 |
You can also remove document names; do this with caution since it will make |
| 154 |
Sphinx treat changed files as unchanged. |
| 155 |
|
| 156 |
.. versionadded:: 1.3 |
| 157 |
|
| 158 |
.. event:: source-read (app, docname, content) |
| 159 |
|
| 160 |
:param app: :class:`.Sphinx` |
| 161 |
:param docname: ``str`` |
| 162 |
:param content: ``list[str]`` |
| 163 |
with a single element, |
| 164 |
representing the content of the source file. |
| 165 |
|
| 166 |
Emitted when a source file has been read. |
| 167 |
|
| 168 |
You can process the ``content`` and |
| 169 |
replace this item to implement source-level transformations. |
| 170 |
|
| 171 |
For example, if you want to use ``$`` signs to delimit inline math, like in |
| 172 |
LaTeX, you can use a regular expression to replace ``$...$`` by |
| 173 |
``:math:`...```. |
| 174 |
|
| 175 |
.. versionadded:: 0.5 |
| 176 |
|
| 177 |
.. event:: include-read (app, relative_path, parent_docname, content) |
| 178 |
|
| 179 |
:param app: :class:`.Sphinx` |
| 180 |
:param relative_path: :class:`~pathlib.Path` |
| 181 |
representing the included file |
| 182 |
relative to the :term:`source directory`. |
| 183 |
:param parent_docname: ``str`` |
| 184 |
of the document name that |
| 185 |
contains the :dudir:`include` directive. |
| 186 |
:param content: ``list[str]`` |
| 187 |
with a single element, |
| 188 |
representing the content of the included file. |
| 189 |
|
| 190 |
Emitted when a file has been read with the :dudir:`include` directive. |
| 191 |
|
| 192 |
You can process the ``content`` and replace this item |
| 193 |
to transform the included content, as with the :event:`source-read` event. |
| 194 |
|
| 195 |
.. versionadded:: 7.2.5 |
| 196 |
|
| 197 |
.. seealso:: The :dudir:`include` directive and the :event:`source-read` event. |
| 198 |
|
| 199 |
.. event:: object-description-transform (app, domain, objtype, contentnode) |
| 200 |
|
| 201 |
:param app: :class:`.Sphinx` |
| 202 |
:param domain: ``str`` |
| 203 |
:param objtype: ``str`` |
| 204 |
:param contentnode: :class:`.desc_content` |
| 205 |
|
| 206 |
Emitted when an object description directive has run. The *domain* and |
| 207 |
*objtype* arguments are strings indicating object description of the object. |
| 208 |
And *contentnode* is a content for the object. It can be modified in-place. |
| 209 |
|
| 210 |
.. versionadded:: 2.4 |
| 211 |
|
| 212 |
.. event:: doctree-read (app, doctree) |
| 213 |
|
| 214 |
:param app: :class:`.Sphinx` |
| 215 |
:param doctree: :class:`docutils.nodes.document` |
| 216 |
|
| 217 |
Emitted when a doctree has been parsed and read by the environment, and is |
| 218 |
about to be pickled. |
| 219 |
The ``doctree`` can be modified in-place. |
| 220 |
|
| 221 |
.. event:: missing-reference (app, env, node, contnode) |
| 222 |
|
| 223 |
:param app: :class:`.Sphinx` |
| 224 |
:param env: :class:`.BuildEnvironment` |
| 225 |
:param node: The :class:`.pending_xref` node to be resolved. |
| 226 |
Its ``reftype``, ``reftarget``, ``modname`` and ``classname`` attributes |
| 227 |
determine the type and target of the reference. |
| 228 |
:param contnode: The node that carries the text and formatting inside the |
| 229 |
future reference and should be a child of the returned reference node. |
| 230 |
:returns: A new node to be inserted in the document tree in place of the node, |
| 231 |
or ``None`` to let other handlers try. |
| 232 |
|
| 233 |
Emitted when a cross-reference to an object cannot be resolved. |
| 234 |
If the event handler can resolve the reference, it should return a |
| 235 |
new docutils node to be inserted in the document tree in place of the node |
| 236 |
*node*. Usually this node is a :class:`~nodes.reference` node containing |
| 237 |
*contnode* as a child. |
| 238 |
If the handler can not resolve the cross-reference, |
| 239 |
it can either return ``None`` to let other handlers try, |
| 240 |
or raise :class:`~sphinx.errors.NoUri` to prevent other handlers in |
| 241 |
trying and suppress a warning about this cross-reference being unresolved. |
| 242 |
|
| 243 |
.. versionadded:: 0.5 |
| 244 |
|
| 245 |
.. event:: warn-missing-reference (app, domain, node) |
| 246 |
|
| 247 |
:param app: :class:`.Sphinx` |
| 248 |
:param domain: The :class:`.Domain` of the missing reference. |
| 249 |
:param node: The :class:`.pending_xref` node that could not be resolved. |
| 250 |
:returns: ``True`` if a warning was emitted, else ``None`` |
| 251 |
|
| 252 |
Emitted when a cross-reference to an object cannot be resolved even after |
| 253 |
:event:`missing-reference`. |
| 254 |
If the event handler can emit warnings for the missing reference, |
| 255 |
it should return ``True``. |
| 256 |
The configuration variables |
| 257 |
:confval:`nitpick_ignore` and :confval:`nitpick_ignore_regex` |
| 258 |
prevent the event from being emitted for the corresponding nodes. |
| 259 |
|
| 260 |
.. versionadded:: 3.4 |
| 261 |
|
| 262 |
.. event:: doctree-resolved (app, doctree, docname) |
| 263 |
|
| 264 |
:param app: :class:`.Sphinx` |
| 265 |
:param doctree: :class:`docutils.nodes.document` |
| 266 |
:param docname: ``str`` |
| 267 |
|
| 268 |
Emitted when a doctree has been "resolved" by the environment, that is, all |
| 269 |
references have been resolved and TOCs have been inserted. The *doctree* can |
| 270 |
be modified in place. |
| 271 |
|
| 272 |
Here is the place to replace custom nodes that don't have visitor methods in |
| 273 |
the writers, so that they don't cause errors when the writers encounter them. |
| 274 |
|
| 275 |
.. event:: env-merge-info (app, env, docnames, other) |
| 276 |
|
| 277 |
:param app: :class:`.Sphinx` |
| 278 |
:param env: :class:`.BuildEnvironment` |
| 279 |
:param docnames: ``list[str]`` |
| 280 |
:param other: :class:`.BuildEnvironment` |
| 281 |
|
| 282 |
This event is only emitted when parallel reading of documents is enabled. It |
| 283 |
is emitted once for every subprocess that has read some documents. |
| 284 |
|
| 285 |
You must handle this event in an extension that stores data in the |
| 286 |
environment in a custom location. Otherwise the environment in the main |
| 287 |
process will not be aware of the information stored in the subprocess. |
| 288 |
|
| 289 |
*other* is the environment object from the subprocess, *env* is the |
| 290 |
environment from the main process. *docnames* is a set of document names |
| 291 |
that have been read in the subprocess. |
| 292 |
|
| 293 |
.. versionadded:: 1.3 |
| 294 |
|
| 295 |
.. event:: env-updated (app, env) |
| 296 |
|
| 297 |
:param app: :class:`.Sphinx` |
| 298 |
:param env: :class:`.BuildEnvironment` |
| 299 |
:returns: iterable of ``str`` |
| 300 |
|
| 301 |
Emitted after reading all documents, when the environment and all |
| 302 |
doctrees are now up-to-date. |
| 303 |
|
| 304 |
You can return an iterable of docnames from the handler. These documents |
| 305 |
will then be considered updated, and will be (re-)written during the writing |
| 306 |
phase. |
| 307 |
|
| 308 |
.. versionadded:: 0.5 |
| 309 |
|
| 310 |
.. versionchanged:: 1.3 |
| 311 |
The handlers' return value is now used. |
| 312 |
|
| 313 |
.. event:: env-get-updated (app, env) |
| 314 |
|
| 315 |
:param app: :class:`.Sphinx` |
| 316 |
:param env: :class:`.BuildEnvironment` |
| 317 |
:returns: iterable of ``str`` |
| 318 |
|
| 319 |
Emitted when the environment determines which source files have changed and |
| 320 |
should be re-read. |
| 321 |
You can return an iterable of docnames to re-read. |
| 322 |
|
| 323 |
.. event:: env-check-consistency (app, env) |
| 324 |
|
| 325 |
:param app: :class:`.Sphinx` |
| 326 |
:param env: :class:`.BuildEnvironment` |
| 327 |
|
| 328 |
Emitted when Consistency checks phase. You can check consistency of |
| 329 |
metadata for whole of documents. |
| 330 |
|
| 331 |
.. versionadded:: 1.6 |
| 332 |
|
| 333 |
.. event:: write-started (app, builder) |
| 334 |
|
| 335 |
:param app: :class:`.Sphinx` |
| 336 |
:param builder: :class:`.Builder` |
| 337 |
|
| 338 |
Emitted before the builder starts to |
| 339 |
resolve and write documents. |
| 340 |
|
| 341 |
.. versionadded:: 7.4 |
| 342 |
|
| 343 |
.. event:: build-finished (app, exception) |
| 344 |
|
| 345 |
:param app: :class:`.Sphinx` |
| 346 |
:param exception: ``Exception`` or ``None`` |
| 347 |
|
| 348 |
Emitted when a build has finished, before Sphinx exits, usually used for |
| 349 |
cleanup. This event is emitted even when the build process raised an |
| 350 |
exception, given as the *exception* argument. The exception is reraised in |
| 351 |
the application after the event handlers have run. If the build process |
| 352 |
raised no exception, *exception* will be ``None``. This allows to customize |
| 353 |
cleanup actions depending on the exception status. |
| 354 |
|
| 355 |
.. versionadded:: 0.5 |
| 356 |
|
| 357 |
Builder specific events |
| 358 |
----------------------- |
| 359 |
|
| 360 |
These events are emitted by specific builders. |
| 361 |
|
| 362 |
.. event:: html-collect-pages (app) |
| 363 |
|
| 364 |
:param app: :class:`.Sphinx` |
| 365 |
:returns: iterable of ``(pagename, context, templatename)`` |
| 366 |
where *pagename* and *templatename* are strings and |
| 367 |
*context* is a ``dict[str, Any]``. |
| 368 |
|
| 369 |
Emitted when the HTML builder is starting to write non-document pages. |
| 370 |
|
| 371 |
You can add pages to write by returning an iterable from this event. |
| 372 |
|
| 373 |
.. versionadded:: 1.0 |
| 374 |
|
| 375 |
.. event:: html-page-context (app, pagename, templatename, context, doctree) |
| 376 |
|
| 377 |
:param app: :class:`.Sphinx` |
| 378 |
:param pagename: ``str`` |
| 379 |
:param templatename: ``str`` |
| 380 |
:param context: ``dict[str, Any]`` |
| 381 |
:param doctree: :class:`docutils.nodes.document` or ``None`` |
| 382 |
:returns: ``str`` or ``None`` |
| 383 |
|
| 384 |
Emitted when the HTML builder has created a context dictionary to render a |
| 385 |
template with -- this can be used to add custom elements to the context. |
| 386 |
|
| 387 |
The *pagename* argument is the canonical name of the page being rendered, |
| 388 |
that is, without ``.html`` suffix and using slashes as path separators. |
| 389 |
The *templatename* is the name of the template to render, this will be |
| 390 |
``'page.html'`` for all pages from reStructuredText documents. |
| 391 |
|
| 392 |
The *context* argument is a dictionary of values that are given to the |
| 393 |
template engine to render the page and can be modified to include custom |
| 394 |
values. |
| 395 |
|
| 396 |
The *doctree* argument will be a doctree when |
| 397 |
the page is created from a reStructuredText documents; |
| 398 |
it will be ``None`` when the page is created from an HTML template alone. |
| 399 |
|
| 400 |
You can return a string from the handler, it will then replace |
| 401 |
``'page.html'`` as the HTML template for this page. |
| 402 |
|
| 403 |
.. tip:: |
| 404 |
|
| 405 |
You can install JS/CSS files for the specific page via |
| 406 |
:meth:`.Sphinx.add_js_file` and :meth:`.Sphinx.add_css_file` |
| 407 |
(since v3.5.0). |
| 408 |
|
| 409 |
.. versionadded:: 0.4 |
| 410 |
|
| 411 |
.. versionchanged:: 1.3 |
| 412 |
The return value can now specify a template name. |
| 413 |
|
| 414 |
.. event:: linkcheck-process-uri (app, uri) |
| 415 |
|
| 416 |
:param app: :class:`.Sphinx` |
| 417 |
:param uri: ``str`` of the collected URI |
| 418 |
:returns: ``str`` or ``None`` |
| 419 |
|
| 420 |
Emitted when the linkcheck builder collects hyperlinks from document. |
| 421 |
|
| 422 |
The event handlers can modify the URI by returning a string. |
| 423 |
|
| 424 |
.. versionadded:: 4.1 |