click_extra.sphinx.doctrees module¶
Keep the build environment out of each pickled page.
Sphinx pickles the doctree of every page it reads. Before it does, it removes the build environment from a copy of the page’s settings. A node that belongs to another document escapes that copy: it pickles its own document, whose settings still hold the whole environment. On a project with an API reference, each page holding such nodes then weighs megabytes, and a build writes hundreds of them.
Two parsers leave such nodes. myst-parser parses an {eval-rst} block into a
throwaway document that shares the page’s settings, then moves the parsed nodes
into the page. docutils points each moved node at the page, but not its
descendants, which keep the throwaway document as theirs. The python:render-rst
and python:render-myst directives of click_extra.sphinx.python parse
their output into a throwaway document the same way.
This module points every node of a page at the page’s own document, once Sphinx has read the page and before it pickles it.
Todo
Re-scope this module once myst-parser parses an {eval-rst} block into the
page’s own document, which
executablebooks/MyST-Parser#1212
asks for in its item 3, and the sphinx extra’s myst-parser floor passes that
release. The hook then only serves the python:render-* directives, which can
point their nodes at the page where they parse them, and the module can go.
- click_extra.sphinx.doctrees.rehome_moved_nodes(app, doctree)[source]¶
Point every node of
doctreeatdoctreeitself.A node moved in from a throwaway document keeps that document, and with it a reference to the build environment, through its settings. Pointing the node at the page lets it pickle without them.
- Return type:
- click_extra.sphinx.doctrees.setup(app)[source]¶
Register the rehoming hook on
app.Called from
click_extra.sphinx.setup()so projects only need to list"click_extra.sphinx"in theirextensions.doctree-readis the last event before Sphinx pickles a page, and priority 900 runs the hook after the handlers at the default 500, which can add nodes to the page too.- Return type: