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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
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.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``


Bugs fixed
Expand Down
12 changes: 12 additions & 0 deletions sphinx/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,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


class TemplateBridge:
"""
Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/mathjax.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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'

#: js_files; list of JS paths or URLs
self.js_files: List[Tuple[str, Dict[str, Any]]] = []

Expand Down
13 changes: 13 additions & 0 deletions tests/test_ext_math.py
Original file line number Diff line number Diff line change
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