问题描述
我有一个设置,我想在我的第一个文档中以嵌套方式使用两个自定义指令:
.. custom_directive_1::
.. custom_directive_2::
:argument1: argumenta
:argument1: argumentb
内容如下:
自定义指令 1:
class CD1(Directive):
option_spec = Directive.option_spec
has_content = True
def run(self):
self.assert_has_content()
#add custom classes to nodes
node = nodes.container()
node['classes'].extend(["class1","class2"])
self.add_name(node)
self.state.nested_parse(self.content,2,node)
for i in node.children:
i['classes'].extend(["class3","class4"])
def setup(app):
app.add_directive("custom_directive_1",CD1)
return {
'version': '0.1','parallel_read_safe': True,'parallel_write_safe': True,}
自定义指令 2:
def parse_arguments(content):
arguments = {}
for item in content:
item = item.split(":")
arguments[item[1]] = ":".join(item[2:]).strip()
return arguments
class CD2(Directive):
option_spec = Directive.option_spec
has_content = True
def run(self):
self.assert_has_content()
node = nodes.paragraph()
arguments = parse_arguments(self.content)
script_reference = ' <script src="../../_static/path/to/jsfile"></script>'
new_content = [
'.. raw:: html','',script_reference
]
#insert template into other template depending on argument1 input
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)),"main_template_file.html")) as f:
main_template_file= f.read()
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)),"templates","{}.html".format(arguments["argument1"]))) as f:
figure = main_template_file.format(f.read(),arguments.get("argumentb",""))
for line in figure.split("\n"):
new_content.append(line)
for i,line in enumerate(new_content):
self.content.data.insert(i,line)
self.content.items.insert(i,(None,i))
self.state.nested_parse(self.content,node)
print(self.content)
return [node]
def setup(app):
app.add_directive("custom_directive_2",CD2)
return {
'version': '0.1',}
问题在于,传递给指令 2 的两个参数也被指令 1 解析。但是,当我在指令 1 中使用 .. figure::
时,参数仅由图形指令解释并被指令 1 忽略。有没有办法让指令 2 的行为类似或让指令 1 只解析指令 2 的结果?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)