0

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?

LazyLeopard
  • 194
  • 1
  • 11
  • 1
    If you had actually tried your example, you would have found it works as you desire. Please show some effort in your questions on SO. – Steve Piercy Mar 22 '20 at 20:45
  • Can you get the first result using `separated` links? – LazyLeopard Mar 23 '20 at 08:10
  • 1
    Yes, but maybe I don't understand what you are asking. Did you actually try your first example? If so, and if it was not what you wanted, please edit your question to describe what you do want. Is your question how to use multiple anonymous links with the same label in one paragraph? – Steve Piercy Mar 23 '20 at 09:15
  • basically I was trying to do `\`here \`_` intuitively hoping that a link called `here` will appear and then clicking it will take me to the web address. Instead it just becomes a local cross-link to `/pytorch`. – LazyLeopard Mar 23 '20 at 09:27
  • 2
    You cannot use [cross-referencing syntax](https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#cross-referencing-syntax) to achieve that end. That aside, it is poor for accessibility to hyperlink "here" or "click here". I strongly recommend instead that you hyperlink *descriptive text*, and never use "click here" in text. Touch devices do not use a mouse. – Steve Piercy Mar 23 '20 at 09:49
  • Yeah, I got your point! But you may want to change the *hyperlink descriptive text* for the same link for different contexts and the same text for different links. – LazyLeopard Mar 23 '20 at 09:54

2 Answers2

2

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|
Steve Piercy
  • 13,693
  • 1
  • 44
  • 57
  • Just one thing to note. It probably will work with html builders only, and you still can't change the *description text*. – LazyLeopard Mar 23 '20 at 13:48
1

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

LazyLeopard
  • 194
  • 1
  • 11