Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/extdev/extensions/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=======================
Built-in extensions API
=======================

Sphinx includes several :ref:`built-in extensions <builtin-extensions>`,
which third-party extensions may wish to integrate with. This chapter
describes the extensions bundled with Sphinx which provide an API which
other extensions may interface with.

For the API documentation on writing your own extension, refer to
:ref:`dev-extensions`.

.. toctree::

linkcode
25 changes: 25 additions & 0 deletions doc/extdev/extensions/linkcode.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
``sphinx.ext.linkcode`` API
===========================

Refer to :mod:`sphinx.ext.linkcode` more for information about the
extension and its configuration.

Builder API
-----------

.. class:: MyAwesomeBuilder

.. attribute:: supported_linkcode

The linkcode extension will only inject references for an ``html``
builder. If a builder wishes to support managing references
generated by linkcode, the ``supported_linkcode`` attribute can be
defined in the builder's implementation.

For example:

.. code-block:: python

class MyAwesomeBuilder(Builder):
supported_linkcode = True
...
1 change: 1 addition & 0 deletions doc/extdev/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,5 @@ disposal when developing Sphinx extensions. Some are core to Sphinx
logging
i18n
utils
extensions/index
deprecated
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 a `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