How to create referenceable label node above section in sphinx(如何在Shinx中创建可引用的标签节点)
本文介绍了如何在Shinx中创建可引用的标签节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Shinx创建自定义指令。 此指令列出了所有可能的对象(每个对象都在单独的部分中)。
现在,我希望文档的其他部分(文件)可以引用这些对象。
我想做一些非常简单的事情,比如:
class MyDirective(Directive):
def run(self, obj):
id1 = 'object-unique-id1'
id2 = 'object-unique-id2'
label = nodes.label('abc1', refid=id1)
section = nodes.section(ids=[id2])
section += nodes.title(text='abc')
section += label
return [section]
但它不允许我通过:ref:object-unique-id1、:ref:object-unique-id2或:ref:abc引用此部分。
所以我的问题是:如何创建可以引用的节点?
推荐答案
在节前添加目标似乎有效。类似于:
class MyDirective(Directive):
def run(self, obj):
titleTxt = 'abc'
lineno = self.state_machine.abs_line_number()
target = nodes.target()
section = nodes.section()
# titleTxt appears to need to be same as the section's title text
self.state.add_target(titleTxt, '', target, lineno)
section += nodes.title(titleTxt, '')
return [target, section]
注意对self.state.add_target的调用。
在构建稍后的某个地方,目标会被魔术般地创建,并且应该能够用
引用您的部分:ref:`abc`
项目中的任何位置。
这篇关于如何在Shinx中创建可引用的标签节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:如何在Shinx中创建可引用的标签节点
基础教程推荐
猜你喜欢
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
