繁体   English   中英

如何在自定义 python-sphinx 指令/扩展中使用现有指令?

[英]How can I use an existing Directive within a custom python-sphinx Directive/extension?

我想创建一个自定义Directive ,该指令在其实现中使用现有指令(本示例中的code-block )。

reStructuredText 中的手动等效项是:

.. mydirective:: py

   .. code-block: py
        
      print("Hello world")     

但是,我希望在my-directive的定义中创建code-block 我找到了一个为现有指令(如下)硬编码适当reStructuredText的示例,但这取决于使用rST的解析器。

class MyDirective(Directive):
    has_content = True

    def run(self):
        # Do custom stuff...

        # Use code-block Directive
        new_content = [
            '.. tab:: {}'.format(json.dumps(tab_args)),
            '   {}'.format(tab_name),
            '',
            '   .. code-block:: {}'.format(lang),
        ]

        if 'linenos' in self.options:
            new_content.append('      :linenos:')

        new_content.append('')

        for idx, line in enumerate(new_content):
            self.content.data.insert(idx, line)
            self.content.items.insert(idx, (None, idx))

        node = nodes.container()
        self.state.nested_parse(self.content, self.content_offset, node)
        return node.children

我如何以独立于解析器的方式实现它?

最后,我的解决方案是:

from sphinx.directives.code import CodeBlock


class CodeTabDirective(CodeBlock):
    """ Tab directive with a codeblock as its content"""

    def run(self):
        self.assert_has_content()
        
        code_block = super().run()[0]

        # Set anything required by OtherDirective

        node = OtherDirective.run(self)[0]  # Generates container
        node.append(code_block)  # Put code block inside container

        return [node]

OtherDirective是另一个现有指令。

我无法继承这两个指令并通过super使用它们的功能,因为我需要从两者调用一个名为run的方法。

更新

您可以在sphinx-tabsCodeTabDirective中看到最终解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM