Skip to content

Commit

Permalink
Allow builders to override support for linkcode references (#12852)
Browse files Browse the repository at this point in the history
Signed-off-by: James Knight <james.d.knight@live.com>
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
  • Loading branch information
jdknight and AA-Turner authored Oct 6, 2024
1 parent 7ad2733 commit d1c4480
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Features added
Patch by Hugo van Kemenade.
* #11809: Improve the formatting for RFC section anchors.
Patch by Jakub Stasiak and Adam Turner.
* #12852: Support a :attr:`.Builder.supported_linkcode` attribute
for builders to enable use of :mod:`sphinx.ext.linkcode`-generated
references.

Bugs fixed
----------
Expand Down
22 changes: 22 additions & 0 deletions doc/extdev/builderapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,25 @@ Builder API
.. attribute:: events

An :class:`.EventManager` object.

.. rubric:: Overridable Attributes (extensions)

Builder sub-classes can set these attributes to support built-in extensions:

.. attribute:: supported_linkcode

By default, the :mod:`linkcode <sphinx.ext.linkcode>` extension will
only inject references for an ``html`` builder.
The ``supported_linkcode`` attribute can be defined in a non-HTML builder
to support managing references generated by linkcode.
The expected value for this attribute is an expression
which is compatible with :rst:dir:`only`.

For example, if a builder was named ``custom-builder``,
the following can be used:

.. code-block:: python
class CustomBuilder(Builder):
supported_linkcode = 'custom-builder'
...
8 changes: 7 additions & 1 deletion sphinx/ext/linkcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def doctree_read(app: Sphinx, doctree: Node) -> None:
raise LinkcodeError(msg)
assert resolve_target is not None # for mypy

# By default, the linkcode extension will only inject references
# for an ``html`` builder. If a builder wishes to support managing
# references generated by linkcode as well, it can define the
# ``supported_linkcode`` attribute.
node_only_expr = getattr(app.builder, 'supported_linkcode', 'html')

domain_keys = {
'py': ['module', 'fullname'],
'c': ['names'],
Expand Down Expand Up @@ -67,7 +73,7 @@ def doctree_read(app: Sphinx, doctree: Node) -> None:
uris.add(uri)

inline = nodes.inline('', _('[source]'), classes=['viewcode-link'])
onlynode = addnodes.only(expr='html')
onlynode = addnodes.only(expr=node_only_expr)
onlynode += nodes.reference('', '', inline, internal=False, refuri=uri)
signode += onlynode

Expand Down

0 comments on commit d1c4480

Please sign in to comment.