微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sphinx 中的嵌套自定义指令

如何解决sphinx 中的嵌套自定义指令

我有一个设置,我想在我的第一个文档中以嵌套方式使用两个自定义指令:

.. 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 举报,一经查实,本站将立刻删除。