我正在 Sphinx 中编写自定义角色/指令,我需要插入一个节点,在该节点中我将文本字符串解释为 reStructuredText。有没有办法做到这一点?
我找到了这个来源http://agateau.com/2015/docutils-snippets,它说明了如何使用docutils.nodes
类以编程方式构建文档树片段,例如
class BulletList(Directive):
def run(self):
fruits = ['Apples', 'Oranges', 'Bananas']
lst = nodes.bullet_list()
for fruit in fruits:
item = nodes.list_item()
lst += item
item += nodes.paragraph(text=fruit)
return [lst]
我想做的是
:myrole:`this is *really* cool|trade|`
通过在 conf.py 中执行此操作:
def myrole(name, rawtext, text, lineno, inliner, options={}, content=[]):
# somehow convert the "text" parameter into docutils nodes
# by interpreting it as RST
nodes = ???? what goes here ???
return nodes, []
def setup(app):
app.add_role('myrole', myrole)