Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sphinx.add_html_assets_in_all_pages #9174

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -30,6 +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
HTML assets in all the pages. Extensions can check this via
``Sphinx.html_assets_in_all_pages``


Bugs fixed
Expand Down
8 changes: 8 additions & 0 deletions sphinx/application.py
Expand Up @@ -146,6 +146,7 @@ def __init__(self, srcdir: str, confdir: Optional[str], outdir: str, doctreedir:
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)
Expand Down Expand Up @@ -1181,6 +1182,13 @@ def add_env_collector(self, collector: Type[EnvironmentCollector]) -> None:
logger.debug('[app] adding environment collector: %r', collector)
collector().enable(self)

def add_html_assets_in_all_pages(self):
humitos marked this conversation as resolved.
Show resolved Hide resolved
"""Tell extensions to insert HTML assets in all the pages.

.. versionadded: 4.1
"""
self.html_assets_in_all_pages = True
humitos marked this conversation as resolved.
Show resolved Hide resolved

def add_html_theme(self, name: str, theme_path: str) -> None:
"""Register a HTML Theme.

Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/mathjax.py
Expand Up @@ -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 domain.has_equations(pagename):
if app.html_assets_in_all_pages or domain.has_equations(pagename):
# Enable mathjax only if equations exists
options = {'async': 'async'}
if app.config.mathjax_options:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_ext_math.py
Expand Up @@ -256,3 +256,16 @@ def test_mathjax_is_not_installed_if_no_equations(app, status, warning):

content = (app.outdir / 'index.html').read_text()
assert 'MathJax.js' not in content


@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.builder.build_all()

content = (app.outdir / 'index.html').read_text()
assert MATHJAX_URL in content

content = (app.outdir / 'nomath.html').read_text()
assert MATHJAX_URL in content