11

在自定义 Sphinx 域中,我想创建对不同域中另一个节点的引用。例如:

.. py:class:: foo.bar

   Lorem ipsum.

.. example:directive:: baz -> foo.bar

   Sit amet, sit.

example:directive::说我的“方法”baz返回一些 type foo.bar,这是一个 Python 类。所以我想将它与其他py:class:: foo.bar描述交叉引用。

from sphinx.directives import ObjectDescription

class ExampleDescription(ObjectDescription):
    def handle_signature(self, sig, signode):

        # lots of parsing and node creation here

        # parsed_annotation = "foo.bar"
        signode += addnodes.desc_returns(parsed_annotation, parsed_annotation)

example在我的自定义域中,我正在解析我的指令并构建元素,一切都很好,即使我的域中的交叉引用也可以通过子类化该sphinx.domains.Domain:resolve_xref方法来正常工作。我只是不确定如何以编程方式在我的handle_signature方法中插入一个节点,该节点稍后会解析为另一个域中的节点。我会以某种方式必须实例化 asphinx.domains.python.PyXRefRole吗?

HTML 中的预期结果将类似于:

<dl>
  <dt>
    <code>baz</code>
    →
    <a href="example.html#py.class.foo.bar">
      <code>foo.bar</code>
    </a>
  </dt>
</dl>
4

0 回答 0