I'm writing custom roles/directives in Sphinx, and I need to insert a node in which I interpret a text string as reStructuredText. Is there a way to do this?
I found this source http://agateau.com/2015/docutils-snippets which says how to use docutils.nodes
classes to construct a doctree fragment programmatically, e.g.
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]
What I want to do is something like
:myrole:`this is *really* cool|trade|`
by doing this in 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)