Python Sphinx 锚点在任意线上

2024-01-04

如何使用 Sphinx 在 ReST 中的任意行设置锚点引用?

为了更清楚,这里有一个例子:

A title with an anchor
----------------------

some stuff

这将创建一个标题A title with an anchor并添加一个额外的on-hover 行尾的字符,这将是对该行/标题的锚引用。

现在在以下情况...

``some arbitrary line``
    this is actually a definition

...我想要一个锚some arbitrary line,与标题的方式相同。


您可以使用名为的角色来执行此操作ref: https://www.sphinx-doc.org/en/master/usage/restructedtext/roles.html#role-ref https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-ref

特别阅读第二个要点,它解决了如果参考文献没有放在标题之前会发生什么情况。

例如,在一个名为example.rst这就是你将如何使用:

.. _arbitrary-anchor:

Some Arbitrary Line
    This is actually a definition

标签“任意锚”在整个文档中必须是唯一的。要在文档中的某个位置引用此锚点,您可以执行以下操作:

Lorem ipsum :ref:`here is the anchor link <arbitrary-anchor>` dolor sit amet

不幸的是,当您hover在引用的行上,但您应该能够使用显式链接来访问它http://example.com/example.html#arbitrary-anchor


既然你提到了定义- 还值得注意的是,有一个角色叫做term它允许您引用术语表中的定义。

有关如何使用它的示例,请参阅:http://sphinx-doc.org/glossary.html#term-role http://sphinx-doc.org/glossary.html#term-role

以及如何在第三段中引用这一点:http://sphinx-doc.org/domains.html#domains http://sphinx-doc.org/domains.html#domains


最后,如果您需要在段落中间插入锚点,一种方法是创建一个显式的锚点<a id=#sample>inline anchor</a>通过使用raw role: http://docutils.sourceforge.net/docs/ref/rst/roles.html#raw http://docutils.sourceforge.net/docs/ref/rst/roles.html#raw


EDIT:

还有一种选择。这将创建锚点和悬停效果。

.. rst:role:: Sample rst role

    This is a sample definition which links back to its anchor :rst:role:`Sample rst role`

这是一个看起来很有趣的指令,我已经使用了一段时间了。我在查看这个页面的源码时发现了它:

http://sphinx-doc.org/markup/inline.html#inline-markup http://sphinx-doc.org/markup/inline.html#inline-markup

http://sphinx-doc.org/_sources/markup/inline.txt http://sphinx-doc.org/_sources/markup/inline.txt

The text looks like this when you hover: enter image description here

The text looks like this after you click on the link: enter image description here

此选项不太理想,因为它显示:定义的左侧和右侧。但这很好,因为它创建了一个锚点并将鼠标悬停在不是标题的内容上(因此它也不会出现在目录中,这正是我想要的)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python Sphinx 锚点在任意线上 的相关文章

随机推荐