如何使我的狮身人面像指令将资产添加到_static文件夹?

问题描述

我正在构建自定义的狮身人面像扩展名和指令,以在狮身人面像和ReadTheDocs上呈现交互式图表。图表的实际数据位于.json文件中。

.rst文档中,包括图表的样子:

.. chart:: charts/test.json
    :width: 400px
    :height: 250px

    This is the caption of the chart,yay...

我要采取的步骤是:

  1. 使用与图表大小相同的占位符(加载微调器)呈现文档

  2. 使用jQuery(在添加的javascript文件中)获取json文件在这种情况下为charts/test.json)的URI(我将其作为属性添加到指令中dom节点的属性)>

  3. 使用jQuery提取文件并解析JSON

  4. 成功获取数据后,使用plotly库将其呈现为图表,并使用jQuery删除占位符


指令如下:


class PlotlyChartDirective(Directive):
    """ Top-level plotly chart directive """

    has_content = True

    def px_value(argument):
        # This is not callable as self.align.  We cannot make it a
        # staticmethod because we're saving an unbound method in
        # option_spec below.
        return directives.length_or_percentage_or_unitless(argument,'px')

    required_arguments = 1
    optional_arguments = 0

    option_spec = {
        # Todo allow static images for PDF renders   'altimage': directives.unchanged,'height': px_value,'width': px_value,}

    def run(self):
        """ Parse a plotly chart directive """

        self.assert_has_content()
        env = self.state.document.settings.env
        # Ensure the current chart ID is initialised in the environment
        if 'next_plotly_chart_id' not in env.temp_data:
            env.temp_data['next_plotly_chart_id'] = 0

        id = env.temp_data['next_plotly_chart_id']

        # Handle the URI of the *.json asset
        uri = directives.uri(self.arguments[0])

        # Create the main node container and store the URI of the file which will be collected later
        node = nodes.container()
        node['classes'] = ['sphinx-plotly']

        # Increment the ID counter ready for the next chart
        env.temp_data['next_plotly_chart_id'] += 1

        # Only if its a supported builder do we proceed (otherwise return an empty node)
        if env.app.builder.name in get_compatible_builders(env.app):

            chart_node = nodes.container()
            chart_node['classes'] = ['sphinx-plotly-chart',f"sphinx-plotly-chart-id-{id}",f"sphinx-plotly-chart-uri-{uri}"]

            placeholder_node = nodes.container()
            placeholder_node['classes'] = ['sphinx-plotly-placeholder',f"sphinx-plotly-placeholder-{id}"]
            placeholder_node += nodes.caption('','Loading...')

            node += chart_node
            node += placeholder_node

            # Add optional chart caption and legend (inspired by figure directive)
            if self.content:
                caption_node = nodes.Element()  # Anonymous container for parsing
                self.state.nested_parse(self.content,self.content_offset,caption_node)
                first_node = caption_node[0]
                if isinstance(first_node,nodes.paragraph):
                    caption = nodes.caption(first_node.rawsource,'',*first_node.children)
                    caption.source = first_node.source
                    caption.line = first_node.line
                    node += caption
                elif not (isinstance(first_node,nodes.comment) and len(first_node) == 0):
                    error = self.state_machine.reporter.error(
                        'Chart caption must be a paragraph or empty comment.',nodes.literal_block(self.block_text,self.block_text),line=self.lineno)
                    return [node,error]
                if len(caption_node) > 1:
                    node += nodes.legend('',*caption_node[1:])

        return [node]

无论我在figureImage指令的源代码中寻找什么(我以此为依据),我都无法弄清楚如何从输入中复制原始图像位置,进入构建目录中的静态文件夹。

没有将参数中指定的*.json文件复制到我的指令中,我总是得到一个找不到的文件

我一直在努力寻找支持方法(我假设Sphinx app实例将具有add_static_file()方法,就像它具有add_css_file()方法一样)。

我还尝试添加文件列表以复制到app实例,然后在构建结束时复制资产(但由于无法将属性添加到{{1 }}类)

胡说八道

自定义指令中,如何将资产(其路径由该指令的参数指定)复制到构建的Sphinx目录中?

解决方法

我意识到在指令的_static方法中直接使用sphinx的copyfile实用程序(仅在文件被更改的情况下才进行复制,所以很快)会很简单。

查看如何获取run()src_uri并直接在此更新的build_uri中复制文件:

Directive