I'd like to create a custom Directive
that uses the an existing Directive (code-block
in this example) in it's implementation.
The manual equivalent of this in reStructuredText would be:
.. mydirective:: py
.. code-block: py
print("Hello world")
However, I would like the code-block
to be created within my-directive
's definition. I found an example that hard-codes the appropriate reStructuredText
for the existing Directive (below), but this depends on the parser using rST
.
class MyDirective(Directive):
has_content = True
def run(self):
# Do custom stuff...
# Use code-block Directive
new_content = [
'.. tab:: {}'.format(json.dumps(tab_args)),
' {}'.format(tab_name),
'',
' .. code-block:: {}'.format(lang),
]
if 'linenos' in self.options:
new_content.append(' :linenos:')
new_content.append('')
for idx, line in enumerate(new_content):
self.content.data.insert(idx, line)
self.content.items.insert(idx, (None, idx))
node = nodes.container()
self.state.nested_parse(self.content, self.content_offset, node)
return node.children
How could I implement this in a parser-independent fashion?