Sphinx remove code formatting from custom code ref

2019-05-20 05:04发布

问题:

I have the following classes:

class A:
    def x():
        """ Do the thing. """
class B(A):
    def x():
        """
        Do the thing, but better than the :py:meth:`parent <A.x>`
        """

The entire module is autodoced. I would like to have a link to the parent implementation of x that renders as "parent", not as "parent". How do I remove the code formatting from the custom method reference?

The link itself is interpreted correctly based on the first rule of cross reference formats.

Here is the generated HTML for the link:

<a class="reference internal" href="#my_module.A.x" title="my_module.A.x">
    <code class="xref py py-meth docutils literal">
        <span class="pre">parent</span>
    </code>
</a>

[Line breaks inserted by me for legibility. Original HTML has no breaks or spaces between tags.]

I am using Sphinx 1.6.3 with Python 3.6.2 in an Anaconda environment.

Inverse question is here: Sphinx add code formatting to :ref:

回答1:

I am facing the same problem. The only solution I have found is to write:

"""
Do the thing, but better than the `parent <#my_module.A.x>`_
"""

which creates a regular ol' hyperlink to the A.x entry on the generated HTML page. However, this only works for HTML output; other formats (e.g., LaTeX) will have broken links.