| 1 |
:mod:`sphinx.ext.extlinks` -- Markup to shorten external links |
| 2 |
============================================================== |
| 3 |
|
| 4 |
.. module:: sphinx.ext.extlinks |
| 5 |
:synopsis: Allow inserting external links with common base URLs easily. |
| 6 |
.. moduleauthor:: Georg Brandl |
| 7 |
|
| 8 |
.. versionadded:: 1.0 |
| 9 |
|
| 10 |
.. role:: code-py(code) |
| 11 |
:language: Python |
| 12 |
|
| 13 |
This extension is meant to help with the common pattern of having many external |
| 14 |
links that point to URLs on one and the same site, e.g. links to bug trackers, |
| 15 |
version control web interfaces, or simply subpages in other websites. It does |
| 16 |
so by providing aliases to base URLs, so that you only need to give the subpage |
| 17 |
name when creating a link. |
| 18 |
|
| 19 |
Let's assume that you want to include many links to issues at the Sphinx |
| 20 |
tracker, at :samp:`https://github.com/sphinx-doc/sphinx/issues/{num}`. Typing |
| 21 |
this URL again and again is tedious, so you can use :mod:`~sphinx.ext.extlinks` |
| 22 |
to avoid repeating yourself. |
| 23 |
|
| 24 |
The extension adds a config value: |
| 25 |
|
| 26 |
.. confval:: extlinks |
| 27 |
:type: :code-py:`dict[str, tuple[str, str | None]]` |
| 28 |
:default: :code-py:`{}` |
| 29 |
|
| 30 |
This config value must be a dictionary of external sites, mapping unique |
| 31 |
short alias names to a *base URL* and a *caption*. For example, to create an |
| 32 |
alias for the above mentioned issues, you would add :: |
| 33 |
|
| 34 |
extlinks = {'issue': ('https://github.com/sphinx-doc/sphinx/issues/%s', |
| 35 |
'issue %s')} |
| 36 |
|
| 37 |
Now, you can use the alias name as a new role, e.g. ``:issue:`123```. This |
| 38 |
then inserts a link to https://github.com/sphinx-doc/sphinx/issues/123. |
| 39 |
As you can see, the target given in the role is substituted in the *base URL* |
| 40 |
in the place of ``%s``. |
| 41 |
|
| 42 |
The link caption depends on the second item in the tuple, the *caption*: |
| 43 |
|
| 44 |
- If *caption* is ``None``, the link caption is the full URL. |
| 45 |
- If *caption* is a string, then it must contain ``%s`` exactly once. In |
| 46 |
this case the link caption is *caption* with the partial URL substituted |
| 47 |
for ``%s`` -- in the above example, the link caption would be |
| 48 |
``issue 123``. |
| 49 |
|
| 50 |
To produce a literal ``%`` in either *base URL* or *caption*, use ``%%``:: |
| 51 |
|
| 52 |
extlinks = {'KnR': ('https://example.org/K%%26R/page/%s', |
| 53 |
'[K&R; page %s]')} |
| 54 |
|
| 55 |
You can also use the usual "explicit title" syntax supported by other roles |
| 56 |
that generate links, i.e. ``:issue:`this issue <123>```. In this case, the |
| 57 |
*caption* is not relevant. |
| 58 |
|
| 59 |
.. versionchanged:: 4.0 |
| 60 |
|
| 61 |
Support to substitute by '%s' in the caption. |
| 62 |
|
| 63 |
.. note:: |
| 64 |
|
| 65 |
Since links are generated from the role in the reading stage, they appear as |
| 66 |
ordinary links to e.g. the ``linkcheck`` builder. |
| 67 |
|
| 68 |
.. confval:: extlinks_detect_hardcoded_links |
| 69 |
:type: :code-py:`bool` |
| 70 |
:default: :code-py:`False` |
| 71 |
|
| 72 |
If enabled, extlinks emits a warning if a hardcoded link is replaceable |
| 73 |
by an extlink, and suggests a replacement via warning. |
| 74 |
|
| 75 |
.. versionadded:: 4.5 |