在 reStructuredText 中格式化链接中的文本

发布于 2024-10-12 14:53:20 字数 1022 浏览 3 评论 0原文

如何格式化 reStructuredText 中指定链接内的文本?

具体来说,我希望从我的第一个代码生成以下 HTML:

<a href="http://docs.python.org/library/optparse.html"><tt>optparse.OptionParser</tt> documentation documentation</a>

结果应如下所示:

optparse.OptionParser 文档

,其中“optparse.OptionParser”部分采用固定宽度字体。

但是我尝试

```optparse.OptionParser`` <http://docs.python.org/library/optparse.html>`_

过,这

<tt class="docutils literal">`optparse.OptionParser</tt> documentation &lt;<a class="reference external" href="http://docs.python.org/library/optparse.html">http://docs.python.org/library/optparse.html</a>&gt;`_

看起来像这样

``optparse.OptionParser<代码> 文档\_

How do you format text within a denoted link in reStructuredText?

Specifically, I wish to generate the following HTML from my rst:

<a href="http://docs.python.org/library/optparse.html"><tt>optparse.OptionParser</tt> documentation documentation</a>

The result should look like this:

optparse.OptionParser
documentation

where the "optparse.OptionParser" portion is in fixed-width font.

I tried

```optparse.OptionParser`` <http://docs.python.org/library/optparse.html>`_

however, this gave

<tt class="docutils literal">`optparse.OptionParser</tt> documentation <<a class="reference external" href="http://docs.python.org/library/optparse.html">http://docs.python.org/library/optparse.html</a>>`_

which looks like this

``optparse.OptionParser
documentation
<http://docs.python.org/library/optparse.html>\
_

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水波映月 2024-10-19 14:53:20

这个构造:

Here you have |optparse.OptionParser|_.

.. |optparse.OptionParser| replace:: ``optparse.OptionParser`` documentation
.. _optparse.OptionParser: http://docs.python.org/library/optparse.html

生成这个 HTML(添加了一些换行符):

<p>Here you have 
  <a class="reference external" href="http://docs.python.org/library/optparse.html">
  <tt class="docutils literal"><span class="pre">optparse.OptionParser</span></tt> documentation</a>.
</p>

我意识到这并不完全是您所要求的,但也许它已经足够接近了。另请参阅http://docutils.sourceforge.net/FAQ.html #is-nested-inline-markup-possible

This construct:

Here you have |optparse.OptionParser|_.

.. |optparse.OptionParser| replace:: ``optparse.OptionParser`` documentation
.. _optparse.OptionParser: http://docs.python.org/library/optparse.html

produces this HTML (some linebreaks added):

<p>Here you have 
  <a class="reference external" href="http://docs.python.org/library/optparse.html">
  <tt class="docutils literal"><span class="pre">optparse.OptionParser</span></tt> documentation</a>.
</p>

I realize that this is not exactly what you asked for, but maybe it's close enough. See also http://docutils.sourceforge.net/FAQ.html#is-nested-inline-markup-possible.

驱逐舰岛风号 2024-10-19 14:53:20

您尝试过 intersphinx 吗?使用该扩展,以下标记:

:py:class:`optparse.OptionParser`

生成此 HTML:

<a class="reference external" href="http://docs.python.org/2.6/library/optparse.html#optparse.OptionParser" title="(in Python v2.6)"><tt class="xref py py-class docutils literal"><span class="pre">optparse.OptionParser</span></tt></a>

使用 Python 2.6 和 Sphinx 1.0.5 进行测试。

Have you tried intersphinx? Using that extension, the following markup:

:py:class:`optparse.OptionParser`

produces this HTML:

<a class="reference external" href="http://docs.python.org/2.6/library/optparse.html#optparse.OptionParser" title="(in Python v2.6)"><tt class="xref py py-class docutils literal"><span class="pre">optparse.OptionParser</span></tt></a>

Tested with Python 2.6 and Sphinx 1.0.5.

听,心雨的声音 2024-10-19 14:53:20

摘自 mzjn 引用的同一常见问题解答页面:

The "raw" directive can be used to insert raw HTML into HTML output:

Here is some |stuff|.

.. |stuff| raw:: html

   <em>emphasized text containing a
   <a href="http://example.org">hyperlink</a> and
   <tt>inline literals</tt></em>

理论上应该可以用 RST 做不到的复杂事情。

Taking from the same FAQ page referenced by mzjn:

The "raw" directive can be used to insert raw HTML into HTML output:

Here is some |stuff|.

.. |stuff| raw:: html

   <em>emphasized text containing a
   <a href="http://example.org">hyperlink</a> and
   <tt>inline literals</tt></em>

It should in theory be possible to do complicated things with that that can't be done with RST.

晨与橙与城 2024-10-19 14:53:20

如果您想本质上

<span class="red">This is red text</span>

使用 Sphinx 获得与 reStructuredText 中的 HTML/CSS 等效的内容,您可以通过创建一个角色来实现:

.. role:: red

然后像这样使用它:

:red:`This is red text`

应该只有一个刻度线 `上面那行的末尾。
当然,您必须将其包含

.red { color: red }

在 CSS 文件中。

If you want to essentially do get HTML/CSS equivalent of

<span class="red">This is red text</span>

in reStructuredText using Sphinx, you can do this by creating a role:

.. role:: red

Then you use it like this:

:red:`This is red text`

There should be only one tick mark ` at the end of the line above.
You, of course, have to have

.red { color: red }

in your CSS file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文