我有一个简单的狮身人面像扩展如下:
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.util.compat import Directive
class testnode(nodes.Element):
def __init__(self, *args, **kwargs):
super(testnode, self).__init__(*args, **kwargs)
self['foo'] = '?'
def visit_testnode_latex(self, node):
self.body.append('Test: %s' % node['foo'])
def depart_testnode_latex(self, node):
pass
def visit_testnode_html(self, node):
self.body.append('<p>Test: %s</p>' % node['foo'])
def depart_testnode_html(self, node):
pass
class TestDirective(Directive):
has_content = False
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = False
option_spec = {
'foo': directives.unchanged,
}
def run(self):
node = testnode()
node['foo'] = self.options.get('foo')
return [node]
def setup(app):
app.add_directive("testdirective", TestDirective)
app.add_node(testnode,
html=(visit_testnode_html,
depart_testnode_html),
latex=(visit_testnode_latex,
depart_testnode_latex))
给定一个包含
.. testdirective::
:foo: bar
HTML 输出包含 »Test: bar« 但 LaTeX 输出包含 »Test: ?« (默认值)。node['foo']
我在分配后检查了它是否具有正确的值TestDirective.run()
,但在 LaTeX 编写器运行之前,它似乎不会一直存在。
我究竟做错了什么?