48

I'd like to link to some URL in my Sphinx docs:

<a href="http://some.url">blah</a>

I have found something similar in the docs: http://sphinx-doc.org/ext/extlinks.html - but it is rather about replacing the custom syntax with the link, by convention. Instead, I just want to generate a link to external web resource.

0

4 Answers 4

73

See the reStructuredText documentation. It can be done either with a named reference:

Test hyperlink: SO_.
    
.. _SO: https://stackoverflow.com/

Or with:

Test hyperlink: `Stack Overflow home <SO>`_.
    
.. _SO: https://stackoverflow.com/

Or with an embedded URI:

Test hyperlink: `Stack Overflow home <https://stackoverflow.com/>`_.
Sign up to request clarification or add additional context in comments.

12 Comments

How about multiple words in the link text?
Yes, but for the case of the named reference, it took me quite a while to figure out that the correct syntax is "` Stack Overflow_ `" (without the spaces -- can't properly format this in a comment) and then " .. _Stack Overflow: stackoverflow.com". It's not in the documentation.
I think the documentation does say that you can put the text in backquotes (with the underscore outside), i.e. `Stack Overflow`_, but it's shown in the "inline markup" section of the quick reference, not in the hyperlinking section (unless you check the specification itself).
I cannot for the life of me get this to work with a "named" reference using the syntax Stack Overflow home <SO>. I even copied and pasted this exact code! It just constructs a relative path to the current docs file, with SO at the end. Has this syntax been removed in recent versions?
@shadowtalker I made it work. See my answer.
|
8

This has been very frustrating, but I tracked down a solution as of mid-2023.

Presently, the fashion something called `"work-stealing" <BL94_>`_.

and later...

.. _BL94: http://supertech.csail.mit.edu/papers/steal.pdf

Note the two underscores: once inside the angle brackets, and once after the closing grave-accent. (Back-quote, if you're younger than the internet.) This correctly links the text "work-stealing" to the PDF article.

2 Comments

This is almost good, but you should use two underscores rather than one. I expanded on it in my own answer (would've posted a comment but couldn't figure out the proper inline syntax).
Just had this exact problem and this solution worked for me.
5

A modification of Ian's answer:

Presently, the fashion something called `"work-stealing" <BL94_>`__.
and later...

.. _BL94: http://supertech.csail.mit.edu/papers/steal.pdf

The difference is that you should use two underscores at the end, rather than one.

This is because according to the spec, these are exactly equivalent:

`blah <foo>`_     <- only one underscore!
`blah`_

.. blah: foo

So the example with one underscore will generate a target for "work-stealing" as well, which is likely not what is desired.

Using two underscores will create an anonymous reference instead, which won't result in a separate new target.

Comments

4

To state the options for the original question: How to get

<a href="http://some.url">blah</a>

Simple named hyperlink:

Use a simple hyperlink reference and target. The target can be used for more than one reference to the same URI.

How to get blah_.

.. _blah: http://some.url

Named hyperlink with special chars or multiple words: Use a phrase reference and target. The target can be used for more than one reference to the same URI.

`How to get blah`_.

.. _How to get blah: http://some.url

If a long link text is used just once or if the same link text is used for links to different URIs: Use an anonymous hyperlink (you need one target for every reference):

How to get blah__ or `how to get blah`__.
And now for something completely different (blah__).

__ http://some.url
__ http://some.url
__ https://another.url

Keep reference and target in one place: Use an embedded URI (named or anonymous).

`How to get blah <http://some.url>`_.
How to get `blah <http://some.url>`__.

A reference with custom link text to an existing named target: Use an embedded alias (named or anonymous).

`How to get blah <blah_>`__.

.. _blah: http://some.url

2 Comments

It would be better if each option included a pinpoint link to the reST spec.
Added them in an edit. Thanks for the hint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.