diff options
-rw-r--r-- | CHANGES | 4 | ||||
-rw-r--r-- | sphinx/application.py | 20 | ||||
-rw-r--r-- | sphinx/ext/mathjax.py | 2 | ||||
-rw-r--r-- | sphinx/registry.py | 3 | ||||
-rw-r--r-- | tests/test_ext_math.py | 2 |
5 files changed, 19 insertions, 12 deletions
@@ -30,9 +30,9 @@ Features added * #9097: Optimize the paralell build * #9131: Add :confval:`nitpick_ignore_regex` to ignore nitpicky warnings using regular expressions -* #9174: Add ``Sphinx.add_html_assets_in_all_pages`` to tell extensions to include +* #9174: Add ``Sphinx.set_html_assets_policy`` to tell extensions to include HTML assets in all the pages. Extensions can check this via - ``Sphinx.html_assets_in_all_pages`` + ``Sphinx.registry.html_assets_policy`` Bugs fixed diff --git a/sphinx/application.py b/sphinx/application.py index 59e1683b3..1da222d7f 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -146,7 +146,6 @@ class Sphinx: self.project: Project = None self.registry = SphinxComponentRegistry() self.html_themes: Dict[str, str] = {} - self.html_assets_in_all_pages: bool = False # validate provided directories self.srcdir = abspath(srcdir) @@ -1182,13 +1181,6 @@ class Sphinx: logger.debug('[app] adding environment collector: %r', collector) collector().enable(self) - def add_html_assets_in_all_pages(self): - """Tell extensions to insert HTML assets in all the pages. - - .. versionadded: 4.1 - """ - self.html_assets_in_all_pages = True - def add_html_theme(self, name: str, theme_path: str) -> None: """Register a HTML Theme. @@ -1265,6 +1257,18 @@ class Sphinx: return True + def set_html_assets_policy(self, policy): + """Set the policy to include assets in HTML pages. + + - always: include the assets in all the pages + - per_page: include the assets only in pages where they are used + + .. versionadded: 4.1 + """ + if policy not in ('always', 'per_page'): + raise ValueError('policy %s is not supported' % policy) + self.registry.html_assets_policy = policy + class TemplateBridge: """ diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py index 8fc63cb9e..46ca3b332 100644 --- a/sphinx/ext/mathjax.py +++ b/sphinx/ext/mathjax.py @@ -79,7 +79,7 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: Dict 'mathjax extension to work') domain = cast(MathDomain, app.env.get_domain('math')) - if app.html_assets_in_all_pages or domain.has_equations(pagename): + if app.registry.html_assets_policy == 'always' or domain.has_equations(pagename): # Enable mathjax only if equations exists options = {'async': 'async'} if app.config.mathjax_options: diff --git a/sphinx/registry.py b/sphinx/registry.py index cdf77cb67..6147a15de 100644 --- a/sphinx/registry.py +++ b/sphinx/registry.py @@ -93,6 +93,9 @@ class SphinxComponentRegistry: self.html_inline_math_renderers: Dict[str, Tuple[Callable, Callable]] = {} self.html_block_math_renderers: Dict[str, Tuple[Callable, Callable]] = {} + #: HTML assets + self.html_assets_policy: str = 'per_page' + #: js_files; list of JS paths or URLs self.js_files: List[Tuple[str, Dict[str, Any]]] = [] diff --git a/tests/test_ext_math.py b/tests/test_ext_math.py index 52d49ec43..973fc3699 100644 --- a/tests/test_ext_math.py +++ b/tests/test_ext_math.py @@ -261,7 +261,7 @@ def test_mathjax_is_not_installed_if_no_equations(app, status, warning): @pytest.mark.sphinx('html', testroot='ext-math', confoverrides={'extensions': ['sphinx.ext.mathjax']}) def test_mathjax_is_installed_if_no_equations_when_forced(app, status, warning): - app.add_html_assets_in_all_pages() + app.set_html_assets_policy('always') app.builder.build_all() content = (app.outdir / 'index.html').read_text() |