Search code examples
python-sphinxrestructuredtext

Format text in a link in reStructuredText


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 &lt;<a class="reference external" href="http://docs.python.org/library/optparse.html">http://docs.python.org/library/optparse.html</a>&gt;`_

which looks like this

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


Solution

  • 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.