【发布时间】:2020-04-22 15:59:32
【问题描述】:
我在交叉引用自定义指令生成的部分时遇到问题。
指令如下:
from docutils import nodes
from docutils.parsers import rst
class TestDirective(rst.Directive):
has_content = False
required_arguments = 1
option_spec = {}
def run(self):
my_arg = self.arguments[0]
target_node = nodes.target('', '', refid=nodes.make_id(my_arg))
section = nodes.section(
'',
nodes.title(text=my_arg),
ids=[nodes.make_id(my_arg)],
names=[nodes.fully_normalize_name(my_arg)])
return [target_node, section]
def setup(app):
app.add_directive('mytest', TestDirective)
下面是它的使用方法:
=============
Test document
=============
.. mytest:: section1
Section 1 content.
.. _section2:
section2
========
Section 2 content.
现在,以下仅适用于section2:
Here are links to :ref:`section1` and :ref:`section2`.
该链接仅针对section2 正确生成,我收到以下错误:
test.rst:19: WARNING: undefined label: section1 (if the link has no caption the
label must precede a section header)
我怎样才能做到这一点?
【问题讨论】:
-
我想问一下你在哪里读到定义
from docutils.parsers import rst的API?我尝试对此进行调查,并让代码在一定程度上运行,但没有记录的 API,我查看了 docutils 源代码,找不到时间来尝试理解这一切。如果你能推荐一些可以缓解这部分研究的东西,我可能会再试一次。 -
@bad_coder:嗯,实际上来自Sphinx tutorial
-
@bad_coder:好的!
-
@abey 我回答了我自己的帖子。您可能会发现它很有用。这是一种折磨。见stackoverflow.com/questions/64146870/…
标签: python python-sphinx cross-reference docutils