click_extra.sphinx.viewcode module¶

Publish only the sphinx.ext.viewcode pages that a documented object links to.

sphinx.ext.viewcode reads the source of every module a documented object may come from, and keeps an entry for that module even when none of its objects gets a [source] link. Each such entry becomes a page of highlighted source that only the _modules index links to. A documented constant is the usual cause: viewcode looks for its definition in the module that defines the class of its value, like re for a compiled pattern, and finds none there.

A serial read drops such an entry when it purges the next document, so only the last document read leaves one behind. A parallel read merges the entries of its workers after every purge has run, so it publishes a page for each unused module (sphinx-doc/sphinx#14675). This module drops every entry with no recorded use once the read is over, which gives both reads the same pages.

The same entry also sets where the [docs] links of a page point. viewcode builds all of them from one module path: the path of the signature that created the entry. A documented instance reports the module of its class, so an instance can create the entry of that class’s module and lend it its own module path, under which none of the objects of the page is indexed. The links then lead to anchors that do not exist, and which signature comes first follows the read order (sphinx-doc/sphinx#14815). This module gives each page the module path under which most of its objects are indexed, and points each link at the page that holds the object.

Todo

Delete drop_unused_viewcode_modules() once sphinx-doc/sphinx#14675 is fixed, and anchor_viewcode_backlinks() once sphinx-doc/sphinx#14815 ships, each when the sphinx extra’s floor passes the release that carries the fix. The module goes with the second of the two.

click_extra.sphinx.viewcode.drop_unused_viewcode_modules(app, env)[source]¶

Forget each module sphinx.ext.viewcode read but no document links.

Connected to env-updated, which fires once the read is over: after a parallel read merged the entries of its workers, and before Sphinx pickles the environment. Inert without sphinx.ext.viewcode, which creates the entries.

Return type:

None

Point the [docs] links of each viewcode page at the objects’ own anchors.

Connected to env-updated after drop_unused_viewcode_modules(). For each module, counts the linked objects that the Python domain indexes under each leading part of the module path, and keeps the part that indexes the most, the shortest on a tie. Each link then leads to the page of the object indexed under that part. A module whose objects no part indexes keeps the links viewcode gave it. Inert without sphinx.ext.viewcode.

Return type:

None

click_extra.sphinx.viewcode.setup(app)[source]¶

Register the viewcode fixes on app.

Called from click_extra.sphinx.setup() so projects only need to list "click_extra.sphinx" in their extensions. Both hooks share the env-updated event, so they run in the order they are connected: the backlinks are set on the modules the first hook keeps.

Return type:

None