LazyLeopard
LazyLeopard

Reputation: 194

Sphinx / reST: Multiple anonymous explicit external links in the same paragraph

How can I create two separated external links in the same paragraph, i.e. something like:

for pytorch click `here <http://www.pytorch.org>`_ and for tensorflow click
`here <http://www.tensorflow.org>`_

that show as the following

for pytorch click here and for tensorflow click here

BUT with separated or non-inline links! This would allow me to gather all my external links at one place and invoke them using any caption I like.

Now, If I use anonymous links as in the following code:

.. __: pytorch_
for example.com click here__

.. __: tensorflow_
and for tensorflow.com click here__

.. _pytorch: http://www.pytorch.org
.. _tensorflow: http://www.tensorflow.org

This is what I get (Note separate paragraphs)

for pytorch click here

and for tensorflow click here

In other words, if we take this example ... can I use the same caption (e.g. here) to link to two different explicit and non-inline external links in the same paragraph?

Upvotes: 0

Views: 391

Answers (2)

LazyLeopard
LazyLeopard

Reputation: 194

The extlinks extension is intended to shorten external links, but it also allows one to specify a link description text

To use it do the following in your conf.py

# enable `extlinks` extension
extensions = [
        'sphinx.ext.extlinks',
]

# add external URLs
extlinks = {
        'pytorch': ('https://www.pytorch.org/%s', 'pytorch'),
        'tensorflow': ('https://www.tensorflow.org/%s', 'tensorflow')
        }

Now Inside the document do the following:

Incidentally :tensorflow:`some <>` libraries turn out to be more popular than
:pytorch:`some <>`

The output should look like this

Incidentally some libraries turn out to be more popular than some

Upvotes: 1

Steve Piercy
Steve Piercy

Reputation: 15045

Use a substitution with raw-html role, following this answer.

Add to conf.py:

rst_epilog = """
.. role:: raw-html(raw)
   :format: html

.. |pytorch| replace:: :raw-html:`<a href="https://www.pytorch.org">here</a>`

.. |tensorflow| replace:: :raw-html:`<a href="https://www.tensorflow.org">here</a>`
"""

In your .rst:

|pytorch| and |tensorflow|

Upvotes: 3

Related Questions