如何在Shinx中创建可引用的标签节点 [英] How to create referenceable label node above section in sphinx

查看:19
本文介绍了如何在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中创建可引用的标签节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆