Skip to main content
 首页 » 编程设计

python之Sphinx 从自定义代码引用中删除代码格式

2025年02月15日7开发

我有以下类(class):

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

整个模块都是autodoc。我想要一个指向 x 的父实现的链接,它呈现为“parent”,而不是“parent”。如何从自定义方法引用中删除代码格式?

链接本身根据 cross reference formats 的第一条规则被正确解释.

这是为链接生成的 HTML:

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

[为了便于阅读,我插入了换行符。原始 HTML 标签之间没有分隔符或空格。]

我在 Anaconda 环境中使用 Sphinx 1.6.3 和 Python 3.6.2。

反题在这里:Sphinx add code formatting to :ref:

请您参考如下方法:

我也遇到了同样的问题。我找到的唯一解决方案是写:

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

它在生成的 HTML 页面上创建指向 A.x 条目的常规 ol' 超链接。但是,这只适用于 HTML 输出;其他格式(例如 LaTeX)将有损坏的链接。