import docutils # this is merely to specifically identify elements derived # from the `my_extension` directive class mydirective(docutils.nodes.Element): pass class MyExtensionDirective(docutils.parsers.rst.Directive): required_arguments = 1; optional_arguments = 0; final_argument_whitespace = True; has_content = True; def run(self): self.assert_has_content() text = '\n'.join(self.content) n = mydirective(text, **self.options) self.add_name(n) # this will turn the directive argument into a title node # (as we generally allow the title to contain inline markup, # we use the inline parser to get (`textnodes`) list of inline # elements/nodes) title_text = self.arguments[0] textnodes, messages = self.state.inline_text(title_text, self.lineno) # the following would end up calling TextElement(rawsource,text,*children) constructor title = docutils.nodes.title(title_text, '', *textnodes) title.source, title.line = ( self.state_machine.get_source_and_line(self.lineno)) # add a new sub-node and any system messages from the inline parsing n += title n += messages # This will parse the directive content self.state.nested_parse(self.content, self.content_offset, n) return [n] # This overrides what directive class will be used to process the `my_extension` directive directives._directives['my_extension'] = MyExtensionDirective;