将链接中的文本设置为 ReStructuredText 格式

如何以 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 文件 < 网址0 _

9847 次浏览

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.

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.

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.

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.