reST: inline links with arbitrary titles?

2020-07-18 20:24发布

If I've defined an internal hyperlink target, how can I link to it using arbitrary text as the link title?

For example:

_`This is a very long internal link target`.

...

A `short reference`_ to the very long link target.

How could I make the “short reference” target the “very long internal target”?

Edit: Another example:

Widgets
=======

...

Use a `widget`_.

How can I make “widget” (singular) link to the “Widgets” (plural) heading?

(of course, I realize that this could be done using `widget`_\s… But that doesn't solve the general problem)

3条回答
趁早两清
2楼-- · 2020-07-18 20:30

A pure-reStructuredText method is to define multiple target names for the same target:

.. _my widget:
.. _above:
My long title about widgets
===========================
…
See how to use  `my widget`_ in the section above_.

(A limitation of this approach is that you may want to use the same text (e.g. "above") for two different locations, and the approach above fails in this case.)

查看更多
\"骚年 ilove
3楼-- · 2020-07-18 20:33

Nicely, mzjn's answer also works for classes:

:class:`MyClasses <mymodule.MyClass>` in plural is awesome.

Hurray!

查看更多
Root(大扎)
4楼-- · 2020-07-18 20:42

I haven't found a way to do this using "standard" reST. But with Sphinx, the following works:

.. _`This is a very long internal link target`:

:ref:`Short reference <This is a very long internal link target>` to the very long link target.
查看更多
登录 后发表回答