Ad

Python-sphinx Extension, Extra Html Files

- 1 answer

I am working on extension for sphinx doc generator.

I would like to add some html, js, css and json files to html build of the doc.

They are not part of template and they should not be affected by anything in sphinx template. They are just viewer for files which I would like to add in downloads folder. (I also do not how.)

How to include extra html files in sphinx extension? JS/CSS can be added as described there https://docs.readthedocs.io/en/latest/guides/adding-custom-css.html but there is not a function for html.

def setup(app):
    app.add_stylesheet('css/custom.css')
    app.add_javascript("js/custom.js")
    # and custom html or any generic file ? 
Ad

Answer

There is a html_static_path in conf.py (it is list of paths). There you can add you static files (by folder).

In order to dynamically generate static files. There is not a explicit way function to add static files. Instead static files can be just copied by python to html_static_path.

Where to find html_static_path?

class Node(nodes.TextElement):
    @staticmethod
    def visit_html(self, node), 
        self.document.settings.env.config.html_static_path

class Direc(Directive):
    def run(self):
        self.state.document.settings.env.config.html_static_path

Also html_static_path is relative to

self.document.settings.env.app.confdir

And build dir can be obrained by

document.settings.env.app.builder.outdir

There is more of html related config in http://www.sphinx-doc.org/en/master/usage/configuration.html

Ad
source: stackoverflow.com
Ad