2009-05-02 13:38:45 -05:00
|
|
|
:mod:`sphinx.ext.extlinks` -- Markup to shorten external links
|
|
|
|
==============================================================
|
|
|
|
|
|
|
|
.. module:: sphinx.ext.extlinks
|
|
|
|
:synopsis: Allow inserting external links with common base URLs easily.
|
|
|
|
.. moduleauthor:: Georg Brandl
|
|
|
|
|
|
|
|
.. versionadded:: 1.0
|
|
|
|
|
|
|
|
This extension is meant to help with the common pattern of having many external
|
|
|
|
links that point to URLs on one and the same site, e.g. links to bug trackers,
|
|
|
|
version control web interfaces, or simply subpages in other websites. It does
|
|
|
|
so by providing aliases to base URLs, so that you only need to give the subpage
|
|
|
|
name when creating a link.
|
|
|
|
|
|
|
|
Let's assume that you want to include many links to issues at the Sphinx
|
2015-02-27 10:02:41 -06:00
|
|
|
tracker, at :samp:`https://github.com/sphinx-doc/sphinx/issues/{num}`. Typing
|
2009-05-02 13:38:45 -05:00
|
|
|
this URL again and again is tedious, so you can use :mod:`~sphinx.ext.extlinks`
|
|
|
|
to avoid repeating yourself.
|
|
|
|
|
2019-02-26 10:09:43 -06:00
|
|
|
The extension adds a config value:
|
2009-05-02 13:38:45 -05:00
|
|
|
|
|
|
|
.. confval:: extlinks
|
|
|
|
|
|
|
|
This config value must be a dictionary of external sites, mapping unique
|
2021-04-09 05:24:15 -05:00
|
|
|
short alias names to a *base URL* and a *caption*. For example, to create an
|
2009-05-02 13:38:45 -05:00
|
|
|
alias for the above mentioned issues, you would add ::
|
|
|
|
|
2015-01-02 06:49:38 -06:00
|
|
|
extlinks = {'issue': ('https://github.com/sphinx-doc/sphinx/issues/%s',
|
2021-02-15 12:59:35 -06:00
|
|
|
'issue %s')}
|
2009-05-02 13:38:45 -05:00
|
|
|
|
|
|
|
Now, you can use the alias name as a new role, e.g. ``:issue:`123```. This
|
2015-01-02 06:49:38 -06:00
|
|
|
then inserts a link to https://github.com/sphinx-doc/sphinx/issues/123.
|
2021-04-09 05:24:15 -05:00
|
|
|
As you can see, the target given in the role is substituted in the *base URL*
|
2010-01-02 13:51:22 -06:00
|
|
|
in the place of ``%s``.
|
2009-05-02 13:38:45 -05:00
|
|
|
|
2021-02-15 12:59:35 -06:00
|
|
|
The link caption depends on the second item in the tuple, the *caption*:
|
2009-05-02 13:38:45 -05:00
|
|
|
|
2021-02-15 12:59:35 -06:00
|
|
|
- If *caption* is ``None``, the link caption is the full URL.
|
|
|
|
- If *caption* is a string, then it must contain ``%s`` exactly once. In
|
|
|
|
this case the link caption is *caption* with the partial URL substituted
|
|
|
|
for ``%s`` -- in the above example, the link caption would be
|
2009-05-02 13:38:45 -05:00
|
|
|
``issue 123``.
|
|
|
|
|
2021-04-09 05:24:15 -05:00
|
|
|
To produce a literal ``%`` in either *base URL* or *caption*, use ``%%``::
|
|
|
|
|
|
|
|
extlinks = {'wikipedia': ('https://en.wikipedia.org/w/index.php?search=%s&title=Special%%3ASearch&fulltext=Search&ns0=1'
|
|
|
|
'Search %s on Wikipedia')}
|
|
|
|
|
2009-05-02 13:38:45 -05:00
|
|
|
You can also use the usual "explicit title" syntax supported by other roles
|
2009-05-03 03:24:28 -05:00
|
|
|
that generate links, i.e. ``:issue:`this issue <123>```. In this case, the
|
2021-02-15 12:59:35 -06:00
|
|
|
*caption* is not relevant.
|
2010-01-03 03:24:49 -06:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Since links are generated from the role in the reading stage, they appear as
|
|
|
|
ordinary links to e.g. the ``linkcheck`` builder.
|