Skip to content

Commit

Permalink
Remove hidden pass-through calls when adding asset files
Browse files Browse the repository at this point in the history
``Sphinx.add_css_file()`` called ``Builder.add_css_file()``
and``Sphinx.add_js_file()`` called ``Builder.add_js_file()``.
  • Loading branch information
AA-Turner committed Aug 12, 2023
1 parent 916d827 commit fa17437
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 0 additions & 6 deletions sphinx/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 5 additions & 3 deletions sphinx/ext/mathjax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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]:
Expand Down

0 comments on commit fa17437

Please sign in to comment.