1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
Description: replace domain.has_equations() with context['has_maths_elements']
See https://github.com/sphinx-doc/sphinx/commit/9b7205b65eebdaf0d1ed544929dca77e3e7a537b.
.
This is mostly based on that Sphinx commit, with the exception that we
default to True, so it unconditionally includes jsmath.js for older
Sphinx versions.
Author: Dmitry Shachnev <mitya57@debian.org>
Forwarded: https://github.com/sphinx-doc/sphinxcontrib-jsmath/pull/32
Last-Update: 2025-05-11
--- a/sphinxcontrib/jsmath/__init__.py
+++ b/sphinxcontrib/jsmath/__init__.py
@@ -15,8 +15,6 @@ from typing import Any, Dict, cast
from docutils import nodes
from sphinx.application import Sphinx
from sphinx.builders.html import StandaloneHTMLBuilder
-from sphinx.domains.math import MathDomain
-from sphinx.environment import BuildEnvironment
from sphinx.errors import ExtensionError
from sphinx.locale import get_translation
from sphinx.util.math import get_node_equation_number
@@ -62,7 +60,13 @@ def html_visit_displaymath(self: HTMLTra
raise nodes.SkipNode
-def install_jsmath(app: Sphinx, env: BuildEnvironment) -> None:
+def install_jsmath(
+ app: Sphinx,
+ pagename: str,
+ templatename: str,
+ context: dict[str, Any],
+ event_arg: Any,
+) -> None:
if app.builder.format != 'html' or app.builder.math_renderer_name != 'jsmath': # type: ignore # NOQA
return
if not app.config.jsmath_path:
@@ -70,8 +74,8 @@ def install_jsmath(app: Sphinx, env: Bui
'jsmath extension to work')
builder = cast(StandaloneHTMLBuilder, app.builder)
- domain = cast(MathDomain, env.get_domain('math'))
- if domain.has_equations():
+ page_has_equations = context.get('has_maths_elements', True)
+ if app.registry.html_assets_policy == 'always' or page_has_equations:
# Enable jsmath only if equations exists
builder.add_js_file(app.config.jsmath_path)
@@ -84,7 +88,7 @@ def setup(app: Sphinx) -> Dict[str, Any]
(html_visit_displaymath, None))
app.add_config_value('jsmath_path', '', False)
- app.connect('env-updated', install_jsmath)
+ app.connect('html-page-context', install_jsmath)
return {
'version': __version__,
'parallel_read_safe': True,
--- a/tests/test_jsmath.py
+++ b/tests/test_jsmath.py
@@ -9,6 +9,7 @@
"""
import pytest
+import sphinx
@pytest.mark.sphinx('html', testroot='basic')
@@ -16,6 +17,7 @@ def test_basic(app, status, warning):
app.builder.build_all()
content = (app.outdir / 'math.html').read_text()
print(content)
+ assert 'jsmath.js' in content
assert '<div class="math notranslate nohighlight">\nE = mc^2</div>' in content
assert ('<span class="eqno">(1)<a class="headerlink" href="#equation-pythagorean" '
'title="Permalink to this equation">¶</a></span>'
@@ -35,6 +37,7 @@ def test_numfig_enabled(app, status, war
app.builder.build_all()
content = (app.outdir / 'math.html').read_text()
+ assert 'jsmath.js' in content
assert '<div class="math notranslate nohighlight">\nE = mc^2</div>' in content
assert ('<span class="eqno">(1.1)<a class="headerlink" href="#equation-pythagorean" '
'title="Permalink to this equation">¶</a></span>'
@@ -48,6 +51,10 @@ def test_numfig_enabled(app, status, war
assert '<a class="reference internal" href="#equation-pythagorean">(1.1)</a>' in content
+@pytest.mark.skipif(
+ sphinx.version_info < (8, 2),
+ reason='Sphinx < 8.2 does not have `has_maths_elements` in context',
+)
@pytest.mark.sphinx('html', testroot='nomath')
def test_disabled_when_equations_not_found(app, status, warning):
app.builder.build_all()
|