Issue #1379 new

Adding CSS and javascript files from extensions

created an issue

It does not seem possible (let alone trivial) to inject CSS or JS content from a sphinx extension, not without serious contorsions anyway:

  • It's easy to do for the project owner by editing

  • It's specifically supported for theme authors using css_files and script_files

For an extension however I have not found it, the only hook I've managed to dig is connecting to html-page-context and injecting stuff inline in context['metatags'], which feels like a hack and a misuse of the hook. On top of that, it forces the content inline and prevent minimization by sphinx (not that sphinx does minification at the moment, as far as I know) or styles and scripts being injected only where relevant (that is, on documents where the directive or role they're working with was used).

Comments (4)

  1. masklinn reporter

    Not sufficient, these methods only add the provided file paths (prefixed with _static) to the generated pages, they don't publish the files themselves to _static. So the user of the extension would also have to add the file paths to his html_static_path, and thus the extension also has to provide a way to get these paths (and pray it is not a zip/an egg).

  2. masklinn reporter

    So worked around the issue thus: add a sub-directory of the package to the config's html_static_path on the fly and use add_javascript and add_stylesheet to inject those into the document:


    This works-ish but:

    • depending on the names picked for the static files, it may collide with theme or user-provided file names. It may be possible to alleviate that issue a bit by creating a sub-directory named after the extension
    • it will absolutely definitely not work with zipped modules, since it needs an actual filesystem directory
  3. Log in to comment