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 all commits
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 @@ -55,6 +55,9 @@ Features added
* #9097: Optimize the paralell build
* #9131: Add :confval:`nitpick_ignore_regex` to ignore nitpicky warnings using
regular expressions
* #9174: Add ``Sphinx.set_html_assets_policy`` to tell extensions to include
HTML assets in all the pages. Extensions can check this via
``Sphinx.registry.html_assets_policy``
* C++, add support for

- ``inline`` variables,
Expand Down
12 changes: 12 additions & 0 deletions sphinx/application.py
Expand Up @@ -1250,6 +1250,18 @@ def is_parallel_allowed(self, typ: str) -> bool:

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

@property
def html_themes(self) -> Dict[str, str]:
warnings.warn('app.html_themes is deprecated.',
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.registry.html_assets_policy == 'always' or domain.has_equations(pagename):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tk0miya I don't like too much how this line is checking for the always policy. Do you have a better suggestion for this?

# Enable mathjax only if equations exists
options = {'async': 'async'}
if app.config.mathjax_options:
Expand Down
3 changes: 3 additions & 0 deletions sphinx/registry.py
Expand Up @@ -93,6 +93,9 @@ def __init__(self) -> None:
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'

#: HTML themes
self.html_themes: Dict[str, str] = {}

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.set_html_assets_policy('always')
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