| 1 |
.. _ext-intersphinx: |
| 2 |
|
| 3 |
:mod:`sphinx.ext.intersphinx` -- Link to other projects' documentation |
| 4 |
====================================================================== |
| 5 |
|
| 6 |
.. module:: sphinx.ext.intersphinx |
| 7 |
:synopsis: Link to other Sphinx documentation. |
| 8 |
|
| 9 |
.. index:: pair: automatic; linking |
| 10 |
|
| 11 |
.. versionadded:: 0.5 |
| 12 |
|
| 13 |
.. role:: code-py(code) |
| 14 |
:language: Python |
| 15 |
|
| 16 |
This extension can generate links to the documentation of objects in external |
| 17 |
projects, either explicitly through the :rst:role:`external` role, or as a |
| 18 |
fallback resolution for any other cross-reference. |
| 19 |
|
| 20 |
Usage for fallback resolution is simple: whenever Sphinx encounters a |
| 21 |
cross-reference that has no matching target in the current documentation set, |
| 22 |
it looks for targets in the external documentation sets configured in |
| 23 |
:confval:`intersphinx_mapping`. A reference like |
| 24 |
``:py:class:`zipfile.ZipFile``` can then link to the Python documentation |
| 25 |
for the ZipFile class, without you having to specify where it is located |
| 26 |
exactly. |
| 27 |
|
| 28 |
When using the :rst:role:`external` role, you can force lookup to any external |
| 29 |
projects, and optionally to a specific external project. |
| 30 |
A link like ``:external:ref:`comparison manual <comparisons>``` will then link |
| 31 |
to the label "comparisons" in whichever configured external project, if it |
| 32 |
exists, |
| 33 |
and a link like ``:external+python:ref:`comparison manual <comparisons>``` will |
| 34 |
link to the label "comparisons" only in the doc set "python", if it exists. |
| 35 |
|
| 36 |
Behind the scenes, this works as follows: |
| 37 |
|
| 38 |
* Each Sphinx HTML build creates a file named :file:`objects.inv` that contains |
| 39 |
a mapping from object names to URIs relative to the HTML set's root. |
| 40 |
|
| 41 |
* Projects using the Intersphinx extension can specify the location of such |
| 42 |
mapping files in the :confval:`intersphinx_mapping` config value. The mapping |
| 43 |
will then be used to resolve both :rst:role:`external` references, and also |
| 44 |
otherwise missing references to objects into links to the other documentation. |
| 45 |
|
| 46 |
* By default, the mapping file is assumed to be at the same location as the rest |
| 47 |
of the documentation; however, the location of the mapping file can also be |
| 48 |
specified individually, e.g. if the docs should be buildable without Internet |
| 49 |
access. |
| 50 |
|
| 51 |
|
| 52 |
Configuration |
| 53 |
------------- |
| 54 |
|
| 55 |
To use Intersphinx linking, add ``'sphinx.ext.intersphinx'`` to your |
| 56 |
:confval:`extensions` config value, and use these config values to activate |
| 57 |
linking: |
| 58 |
|
| 59 |
.. confval:: intersphinx_mapping |
| 60 |
:type: :code-py:`dict[str, tuple[str, tuple[str, tuple[str | None, ...]]]]` |
| 61 |
:default: :code-py:`{}` |
| 62 |
|
| 63 |
This config value contains the locations and names of other projects that |
| 64 |
should be linked to in this documentation. |
| 65 |
|
| 66 |
Relative local paths for target locations are taken as relative to the base |
| 67 |
of the built documentation, while relative local paths for inventory |
| 68 |
locations are taken as relative to the source directory. |
| 69 |
|
| 70 |
When fetching remote inventory files, proxy settings will be read from |
| 71 |
the ``$HTTP_PROXY`` environment variable. |
| 72 |
|
| 73 |
**Format** |
| 74 |
|
| 75 |
.. versionadded:: 1.0 |
| 76 |
|
| 77 |
A dictionary mapping unique identifiers to a tuple ``(target, inventory)``. |
| 78 |
Each ``target`` is the base URI of a foreign Sphinx documentation set and can |
| 79 |
be a local path or an HTTP URI. The ``inventory`` indicates where the |
| 80 |
inventory file can be found: it can be ``None`` (an :file:`objects.inv` file |
| 81 |
at the same location as the base URI) or another local file path or a full |
| 82 |
HTTP URI to an inventory file. |
| 83 |
|
| 84 |
The unique identifier can be used in the :rst:role:`external` role, so that |
| 85 |
it is clear which intersphinx set the target belongs to. A link like |
| 86 |
``:external+python:ref:`comparison manual <comparisons>``` will link to the |
| 87 |
label "comparisons" in the doc set "python", if it exists. |
| 88 |
|
| 89 |
**Example** |
| 90 |
|
| 91 |
To add links to modules and objects in the Python standard library |
| 92 |
documentation, use:: |
| 93 |
|
| 94 |
intersphinx_mapping = {'python': ('https://docs.python.org/3', None)} |
| 95 |
|
| 96 |
This will download the corresponding :file:`objects.inv` file from the |
| 97 |
Internet and generate links to the pages under the given URI. The downloaded |
| 98 |
inventory is cached in the Sphinx environment, so it must be re-downloaded |
| 99 |
whenever you do a full rebuild. |
| 100 |
|
| 101 |
A second example, showing the meaning of a non-``None`` value of the second |
| 102 |
tuple item:: |
| 103 |
|
| 104 |
intersphinx_mapping = {'python': ('https://docs.python.org/3', |
| 105 |
'python-inv.txt')} |
| 106 |
|
| 107 |
This will read the inventory from :file:`python-inv.txt` in the source |
| 108 |
directory, but still generate links to the pages under |
| 109 |
``https://docs.python.org/3``. It is up to you to update the inventory file |
| 110 |
as new objects are added to the Python documentation. |
| 111 |
|
| 112 |
**Multiple targets for the inventory** |
| 113 |
|
| 114 |
.. versionadded:: 1.3 |
| 115 |
|
| 116 |
Alternative files can be specified for each inventory. One can give a |
| 117 |
tuple for the second inventory tuple item as shown in the following |
| 118 |
example. This will read the inventory iterating through the (second) |
| 119 |
tuple items until the first successful fetch. The primary use case for |
| 120 |
this to specify mirror sites for server downtime of the primary |
| 121 |
inventory:: |
| 122 |
|
| 123 |
intersphinx_mapping = {'python': ('https://docs.python.org/3', |
| 124 |
(None, 'python-inv.txt'))} |
| 125 |
|
| 126 |
For a set of books edited and tested locally and then published |
| 127 |
together, it could be helpful to try a local inventory file first, |
| 128 |
to check references before publication:: |
| 129 |
|
| 130 |
intersphinx_mapping = { |
| 131 |
'otherbook': |
| 132 |
('https://myproj.readthedocs.io/projects/otherbook/en/latest', |
| 133 |
('../../otherbook/build/html/objects.inv', None)), |
| 134 |
} |
| 135 |
|
| 136 |
.. confval:: intersphinx_resolve_self |
| 137 |
:type: :code-py:`str` |
| 138 |
:default: :code-py:`''` |
| 139 |
|
| 140 |
If provided, :confval:`!intersphinx_resolve_self` overrides intersphinx's |
| 141 |
resolution mechanism to resolve all references to the current project, |
| 142 |
rather than an external reference. |
| 143 |
This is useful when documentation is shared between projects, |
| 144 |
with the 'upstream' or 'parent' project using intersphinx-style references |
| 145 |
in its documentation. |
| 146 |
For example, a project such as *Astropy* might set: |
| 147 |
|
| 148 |
.. code-block:: python |
| 149 |
|
| 150 |
intersphinx_resolve_self = 'astropy' |
| 151 |
|
| 152 |
Projects re-using *Astropy*'s documentation or inheriting their docstrings |
| 153 |
would then configure their :confval:`!intersphinx_mapping` with |
| 154 |
an :code-py:`'astropy'` key, pointing to *astropy*'s :file:`objects.inv`. |
| 155 |
For example: |
| 156 |
|
| 157 |
.. code-block:: python |
| 158 |
|
| 159 |
intersphinx_mapping = { |
| 160 |
'astropy': ('https://docs.astropy.org/en/stable/', None), |
| 161 |
} |
| 162 |
|
| 163 |
.. confval:: intersphinx_cache_limit |
| 164 |
:type: :code-py:`int` |
| 165 |
:default: :code-py:`5` (five days) |
| 166 |
|
| 167 |
The maximum number of days to cache remote inventories. |
| 168 |
Set this to a negative value to cache inventories for unlimited time. |
| 169 |
|
| 170 |
.. confval:: intersphinx_timeout |
| 171 |
:type: :code-py:`int | float | None` |
| 172 |
:default: :code-py:`None` |
| 173 |
|
| 174 |
The number of seconds for timeout. Use ``None`` for no timeout. |
| 175 |
|
| 176 |
.. note:: |
| 177 |
|
| 178 |
timeout is not a time limit on the entire response download; rather, an |
| 179 |
exception is raised if the server has not issued a response for *timeout* |
| 180 |
seconds. |
| 181 |
|
| 182 |
.. confval:: intersphinx_disabled_reftypes |
| 183 |
:type: :code-py:`Sequence[str]` |
| 184 |
:default: :code-py:`['std:doc']` |
| 185 |
|
| 186 |
.. versionadded:: 4.3 |
| 187 |
|
| 188 |
.. versionchanged:: 5.0 |
| 189 |
|
| 190 |
Changed default value from an empty list to ``['std:doc']``. |
| 191 |
|
| 192 |
A list of strings being either: |
| 193 |
|
| 194 |
- the name of a specific reference type in a domain, |
| 195 |
e.g., ``std:doc``, ``py:func``, or ``cpp:class``, |
| 196 |
- the name of a domain, and a wildcard, e.g., |
| 197 |
``std:*``, ``py:*``, or ``cpp:*``, or |
| 198 |
- simply a wildcard ``*``. |
| 199 |
|
| 200 |
When a non-:rst:role:`external` cross-reference is being resolved by |
| 201 |
intersphinx, skip resolution if it matches one of the specifications in this |
| 202 |
list. |
| 203 |
|
| 204 |
For example, with ``intersphinx_disabled_reftypes = ['std:doc']`` |
| 205 |
a cross-reference ``:doc:`installation``` will not be attempted to be |
| 206 |
resolved by intersphinx, but ``:external+otherbook:doc:`installation``` will |
| 207 |
be attempted to be resolved in the inventory named ``otherbook`` in |
| 208 |
:confval:`intersphinx_mapping`. |
| 209 |
At the same time, all cross-references generated in, e.g., Python, |
| 210 |
declarations will still be attempted to be resolved by intersphinx. |
| 211 |
|
| 212 |
If ``*`` is in the list of domains, then no non-:rst:role:`external` |
| 213 |
references will be resolved by intersphinx. |
| 214 |
|
| 215 |
Explicitly Reference External Objects |
| 216 |
------------------------------------- |
| 217 |
|
| 218 |
The Intersphinx extension provides the following role. |
| 219 |
|
| 220 |
.. rst:role:: external |
| 221 |
|
| 222 |
.. versionadded:: 4.4 |
| 223 |
|
| 224 |
Use Intersphinx to perform lookup only in external projects, and not the |
| 225 |
current project. Intersphinx still needs to know the type of object you |
| 226 |
would like to find, so the general form of this role is to write the |
| 227 |
cross-refererence as if the object is in the current project, but then prefix |
| 228 |
it with ``:external``. |
| 229 |
The two forms are then |
| 230 |
|
| 231 |
- ``:external:domain:reftype:`target```, |
| 232 |
e.g., ``:external:py:class:`zipfile.ZipFile```, or |
| 233 |
- ``:external:reftype:`target```, |
| 234 |
e.g., ``:external:doc:`installation```. |
| 235 |
With this shorthand, the domain is assumed to be ``std``. |
| 236 |
|
| 237 |
If you would like to constrain the lookup to a specific external project, |
| 238 |
then the key of the project, as specified in :confval:`intersphinx_mapping`, |
| 239 |
is added as well to get the two forms |
| 240 |
|
| 241 |
- ``:external+invname:domain:reftype:`target```, |
| 242 |
e.g., ``:external+python:py:class:`zipfile.ZipFile```, or |
| 243 |
- ``:external+invname:reftype:`target```, |
| 244 |
e.g., ``:external+python:doc:`installation```. |
| 245 |
|
| 246 |
Showing all links of an Intersphinx mapping file |
| 247 |
------------------------------------------------ |
| 248 |
|
| 249 |
To show all Intersphinx links and their targets of an Intersphinx mapping file, |
| 250 |
run ``python -m sphinx.ext.intersphinx url-or-path``. This is helpful when |
| 251 |
searching for the root cause of a broken Intersphinx link in a documentation |
| 252 |
project. |
| 253 |
The following example prints the Intersphinx mapping of the Python documentation: |
| 254 |
|
| 255 |
.. code-block:: console |
| 256 |
|
| 257 |
$ python -m sphinx.ext.intersphinx https://docs.python.org/3/objects.inv |
| 258 |
|
| 259 |
Using Intersphinx with inventory file under Basic Authorization |
| 260 |
--------------------------------------------------------------- |
| 261 |
|
| 262 |
Intersphinx supports Basic Authorization like this:: |
| 263 |
|
| 264 |
intersphinx_mapping = {'python': ('https://user:[email protected]/3', |
| 265 |
None)} |
| 266 |
|
| 267 |
The user and password will be stripped from the URL when generating the links. |