thclark / sphinx-charts

Interactive charts in sphinx HTML docs, using plot.ly and D3
MIT License
26 stars 3 forks source link

Incorrect linking of json asset from non-root pages #5

Closed thclark closed 3 years ago

thclark commented 3 years ago

A relative, not absolute, link is given to the javascript that renders the chart.

Say you have a non-root page at

examples/look_at_my_charts.rst

And it includes a chart whose json is kept alongside the .rst file: examples/chart_to_look_at.json

sphinx-charts will place the json file at _charts/examples/chart_to_look_at.json but the browser can't find it because the path isn't necessarily correctly resolved... The browser will try to fetch:

examples/_charts/examples/chart_to_look_at.json

Fix can be inspired by the Include directive which shows the path resolution:

class Include(BaseInclude, SphinxDirective):
    """
    Like the standard "Include" directive, but interprets absolute paths
    "correctly", i.e. relative to source directory.
    """

    def run(self) -> List[Node]:
        if self.arguments[0].startswith('<') and \
           self.arguments[0].endswith('>'):
            # docutils "standard" includes, do not do path processing
            return super().run()
        rel_filename, filename = self.env.relfn2path(self.arguments[0])
        self.arguments[0] = filename
        self.env.note_included(filename)
        return super().run()