Bernhard Leiner
2009-04-30 14:45:24 UTC
Hello,
I'm trying to write a supposedly simple docutils "my_insert" directive
that is capable of inserting arbitrary rst content into a document.
Basically this directive should work as follows:
.. my_insert:: keyword
Inside the run() method of the "my_insert" directive, depending on the
"keyword", a function is called which returns some string in rst
format. The best thing I achieved so far is this version:
class My_Insert (Directive) :
required_arguments = 1
# table of functions to be used to get content, depending on the argument
func_d = { 'key_1' : function_1
}
def run (self):
rst_input = self.func_d[self.arguments[0]]()
node = nodes.literal_block (text = rst_input)
node['language'] = 'none'
return [node]
Note that this only includes the returned string inside a new
literal_block. I think I need something along this inside my run()
method to trigger parsing of the input:
def run (self):
rst_input = self.func_d[self.arguments[0]]()
node = nodes.Element ()
self.content = rst_input # XXX this does not work
self.state.nested_parse (self.content, self.content_offset, node)
return [node]
but unfortunately I'm not able to include the "rst_input" string into
self.content. The simple assignment as used above definitely does not
work.
Maybe I'm missing just a little bit, maybe my approach is not feasible
at all... I don't know
Thanks a lot in advance for any help!
I'm trying to write a supposedly simple docutils "my_insert" directive
that is capable of inserting arbitrary rst content into a document.
Basically this directive should work as follows:
.. my_insert:: keyword
Inside the run() method of the "my_insert" directive, depending on the
"keyword", a function is called which returns some string in rst
format. The best thing I achieved so far is this version:
class My_Insert (Directive) :
required_arguments = 1
# table of functions to be used to get content, depending on the argument
func_d = { 'key_1' : function_1
}
def run (self):
rst_input = self.func_d[self.arguments[0]]()
node = nodes.literal_block (text = rst_input)
node['language'] = 'none'
return [node]
Note that this only includes the returned string inside a new
literal_block. I think I need something along this inside my run()
method to trigger parsing of the input:
def run (self):
rst_input = self.func_d[self.arguments[0]]()
node = nodes.Element ()
self.content = rst_input # XXX this does not work
self.state.nested_parse (self.content, self.content_offset, node)
return [node]
but unfortunately I'm not able to include the "rst_input" string into
self.content. The simple assignment as used above definitely does not
work.
Maybe I'm missing just a little bit, maybe my approach is not feasible
at all... I don't know
Thanks a lot in advance for any help!
--
Bernhard Leiner http://bernh.net
Bernhard Leiner http://bernh.net