91

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

具体来说,我希望从我的第一个 HTML 中生成以下 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 documentation <http://docs.python.org/library/optparse.html>\_

4

4 回答 4

97

这个构造:

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

于 2011-01-29T11:11:35.490 回答
7

你试过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 测试。

于 2011-01-20T17:57:59.887 回答
4

取自 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 做不了的事情来做复杂的事情。

于 2014-01-10T19:24:36.983 回答
0

如果你想基本上得到 HTML/CSS 等价于

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

在使用 Sphinx 的 reStructuredText 中,您可以通过创建角色来做​​到这一点:

.. role:: red

然后你像这样使用它:

:red:`This is red text`

`上面一行的末尾应该只有一个刻度线。你当然必须有

.red { color: red }

在您的 CSS 文件中。

于 2011-08-04T17:51:42.010 回答