Generating labels for nodes of a custom directive(为自定义指令的节点生成标签)
本文介绍了为自定义指令的节点生成标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Sphinx "TODO" Directive example我想引用嵌入在.rst文件中的todo实例。例如,如果.rst文件内容包含:
.. todo:: foo
.. todo:: bar
我可以看到以下代码(摘自Sphinx TODO示例页面)
class TodoDirective(SphinxDirective):
# this enables content in the directive
has_content = True
def run(self):
targetid = 'todo-%d' % self.env.new_serialno('todo')
targetnode = nodes.target('', '', ids=[targetid])
todo_node = todo('
'.join(self.content))
todo_node += nodes.title(_('Todo'), _('Todo'))
self.state.nested_parse(self.content, self.content_offset, todo_node)
if not hasattr(self.env, 'todo_all_todos'):
self.env.todo_all_todos = []
self.env.todo_all_todos.append({
'docname': self.env.docname,
'lineno': self.lineno,
'todo': todo_node.deepcopy(),
'target': targetnode,
})
return [targetnode, todo_node]
使用ids:todo-0和todo-1创建目标节点。
通过将指令嵌入到.rst文件中成功引用的:
.. todolist::
我想做的是引用.rst文件中的todo项,如下所示:
:ref:`todo-0`
:ref:`todo-1`
这将需要让TodoDirective为每个目标节点生成一个标签。我还想不出该怎么做。
这里发布了这个简单的项目:https://github.com/natersoz/sphinx_sandbox
推荐答案
我遇到了与您相同的问题,并能够通过查看autosectionlabel扩展解决该问题。
他们在那里所做的是将引用添加到标签域数据。我让它在一个定制指令中工作,如下所示:nodeId = nodes.make_id("some-id")
self.env.app.env.domaindata["std"]["labels"][nodeId] = self.env.docname, nodeId, "Title"
section = nodes.section(ids=[nodeId])
section.append(nodes.title(text="Title"))
Key是上面代码的第二行。
您还希望将标签添加到anonlabels,以便能够通过
:ref:`foo <nodeId>`
如下:
self.env.app.env.domaindata["std"]["anonlabels"][nodeId] = self.env.docname, nodeId
这篇关于为自定义指令的节点生成标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:为自定义指令的节点生成标签
基础教程推荐
猜你喜欢
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 求两个直方图的卷积 2022-01-01
