From fa17437254291a2a3a41da9dc0de6a768cec87d4 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 12 Aug 2023 08:50:33 +0100 Subject: [PATCH] Remove hidden pass-through calls when adding asset files ``Sphinx.add_css_file()`` called ``Builder.add_css_file()`` and``Sphinx.add_js_file()`` called ``Builder.add_js_file()``. --- sphinx/application.py | 6 ------ sphinx/builders/html/__init__.py | 10 ++++++++-- sphinx/ext/mathjax.py | 8 +++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/sphinx/application.py b/sphinx/application.py index 44432f8a6e7..b42fb9dba60 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -1030,9 +1030,6 @@ def add_js_file(self, filename: str | None, priority: int = 500, kwargs['defer'] = 'defer' self.registry.add_js_file(filename, priority=priority, **kwargs) - if hasattr(self, 'builder') and hasattr(self.builder, 'add_js_file'): - self.builder.add_js_file(filename, - priority=priority, **kwargs) def add_css_file(self, filename: str, priority: int = 500, **kwargs: Any) -> None: """Register a stylesheet to include in the HTML output. @@ -1093,9 +1090,6 @@ def add_css_file(self, filename: str, priority: int = 500, **kwargs: Any) -> Non """ logger.debug('[app] adding stylesheet: %r', filename) self.registry.add_css_files(filename, priority=priority, **kwargs) - if hasattr(self, 'builder') and hasattr(self.builder, 'add_css_file'): - self.builder.add_css_file(filename, - priority=priority, **kwargs) def add_latex_package(self, packagename: str, options: str | None = None, after_hyperref: bool = False) -> None: diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 10e8ce0e239..1d0953cb8fc 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -498,9 +498,15 @@ def prepare_writing(self, docnames: set[str]) -> None: rellinks.append((indexname, indexcls.localname, '', indexcls.shortname)) + # add assets registered after ``Builder.init()``. + for css_filename, attrs in self.app.registry.css_files: + self.add_css_file(css_filename, **attrs) + for js_filename, attrs in self.app.registry.js_files: + self.add_js_file(js_filename or '', **attrs) + # back up _css_files and _js_files to allow adding CSS/JS files to a specific page. - self._orig_css_files = list(self._css_files) - self._orig_js_files = list(self._js_files) + self._orig_css_files = list(dict.fromkeys(self._css_files)) + self._orig_js_files = list(dict.fromkeys(self._js_files)) styles = list(self._get_style_filenames()) self.globalcontext = { diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py index d620002d514..0131a944109 100644 --- a/sphinx/ext/mathjax.py +++ b/sphinx/ext/mathjax.py @@ -14,6 +14,7 @@ import sphinx from sphinx.application import Sphinx +from sphinx.builders.html import StandaloneHTMLBuilder from sphinx.domains.math import MathDomain from sphinx.errors import ExtensionError from sphinx.locale import _ @@ -79,6 +80,7 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict 'mathjax extension to work') domain = cast(MathDomain, app.env.get_domain('math')) + builder = cast(StandaloneHTMLBuilder, app.builder) if app.registry.html_assets_policy == 'always' or domain.has_equations(pagename): # Enable mathjax only if equations exists if app.config.mathjax2_config: @@ -87,10 +89,10 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict 'mathjax_config/mathjax2_config does not work ' 'for the current MathJax version, use mathjax3_config instead') body = 'MathJax.Hub.Config(%s)' % json.dumps(app.config.mathjax2_config) - app.add_js_file(None, type='text/x-mathjax-config', body=body) + builder.add_js_file('', type='text/x-mathjax-config', body=body) if app.config.mathjax3_config: body = 'window.MathJax = %s' % json.dumps(app.config.mathjax3_config) - app.add_js_file(None, body=body) + builder.add_js_file('', body=body) options = {} if app.config.mathjax_options: @@ -102,7 +104,7 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict else: # Load other MathJax via "async" method options['async'] = 'async' - app.add_js_file(app.config.mathjax_path, **options) + builder.add_js_file(app.config.mathjax_path, **options) def setup(app: Sphinx) -> dict[str, Any]: