From 80acc8280a92e996a1976b1f5e0b767e2943cc15 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 19 Oct 2024 21:02:09 +0100 Subject: [PATCH 1/5] Remove has_equations from the mathematics domain --- sphinx/builders/html/__init__.py | 1 + sphinx/domains/math.py | 22 ---------------------- sphinx/ext/mathjax.py | 3 +-- sphinx/writers/html.py | 3 +++ sphinx/writers/html5.py | 3 +++ 5 files changed, 8 insertions(+), 24 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 742a351fd61..961c875a513 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -656,6 +656,7 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None: metatags = self.docwriter.clean_meta ctx = self.get_doc_context(docname, body, metatags) + ctx['has_equations'] = self.docwriter.has_equations self.handle_page(docname, ctx, event_arg=doctree) def write_doc_serialized(self, docname: str, doctree: nodes.document) -> None: diff --git a/sphinx/domains/math.py b/sphinx/domains/math.py index 19e050739db..a14d9eae5bc 100644 --- a/sphinx/domains/math.py +++ b/sphinx/domains/math.py @@ -41,7 +41,6 @@ class MathDomain(Domain): initial_data: dict[str, Any] = { 'objects': {}, # labelid -> (docname, eqno) - 'has_equations': {}, # docname -> bool } dangling_warnings = { 'eq': 'equation not found: %(target)s', @@ -71,28 +70,16 @@ def get_equation_number_for(self, labelid: str) -> int | None: else: return None - def process_doc(self, env: BuildEnvironment, docname: str, - document: nodes.document) -> None: - def math_node(node: Node) -> bool: - return isinstance(node, nodes.math | nodes.math_block) - - self.data['has_equations'][docname] = any(document.findall(math_node)) - def clear_doc(self, docname: str) -> None: for equation_id, (doc, _eqno) in list(self.equations.items()): if doc == docname: del self.equations[equation_id] - self.data['has_equations'].pop(docname, None) - def merge_domaindata(self, docnames: Set[str], otherdata: dict[str, Any]) -> None: for labelid, (doc, eqno) in otherdata['objects'].items(): if doc in docnames: self.equations[labelid] = (doc, eqno) - for docname in docnames: - self.data['has_equations'][docname] = otherdata['has_equations'][docname] - def resolve_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder, typ: str, target: str, node: pending_xref, contnode: Element, ) -> Element | None: @@ -136,15 +123,6 @@ def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Bui def get_objects(self) -> Iterable[tuple[str, str, str, str, str, int]]: return [] - def has_equations(self, docname: str | None = None) -> bool: - if not docname: - return any(self.data['has_equations'].values()) - - return ( - self.data['has_equations'].get(docname, False) - or any(map(self.has_equations, self.env.toctree_includes.get(docname, ()))) - ) - def setup(app: Sphinx) -> ExtensionMetadata: app.add_domain(MathDomain) diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py index ba8cd03e945..e3034cba003 100644 --- a/sphinx/ext/mathjax.py +++ b/sphinx/ext/mathjax.py @@ -81,9 +81,8 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict msg = 'mathjax_path config value must be set for the mathjax extension to work' raise ExtensionError(msg) - domain = app.env.domains.math_domain builder = cast(StandaloneHTMLBuilder, app.builder) - if app.registry.html_assets_policy == 'always' or domain.has_equations(pagename): + if app.registry.html_assets_policy == 'always' or context.get('has_equations'): # Enable mathjax only if equations exists if app.config.mathjax2_config: if app.config.mathjax_path == MATHJAX_URL: diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 4b193345da0..c83dc809e00 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -24,6 +24,8 @@ class HTMLWriter(Writer): # type: ignore[misc] # override embed-stylesheet default value to False. settings_default_overrides = {'embed_stylesheet': False} + has_equations: bool = False + def __init__(self, builder: StandaloneHTMLBuilder) -> None: super().__init__() self.builder = builder @@ -57,3 +59,4 @@ def translate(self) -> None: ): setattr(self, attr, getattr(visitor, attr, None)) self.clean_meta = ''.join(self.visitor.meta[2:]) + self.has_equations = getattr(visitor, 'has_equations', False) diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index e002788fc97..0b0d55858c5 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -66,6 +66,7 @@ def __init__(self, document: nodes.document, builder: Builder) -> None: self._table_row_indices = [0] self._fieldlist_row_indices = [0] self.required_params_left = 0 + self.has_equations: bool = False def visit_start_of_file(self, node: Element) -> None: # only occurs in the single-file builder @@ -958,12 +959,14 @@ def visit_field(self, node: Element) -> None: node['classes'].append('field-odd') def visit_math(self, node: Element, math_env: str = '') -> None: + self.has_equations = True # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] visit, _ = self.builder.app.registry.html_inline_math_renderers[name] visit(self, node) def depart_math(self, node: Element, math_env: str = '') -> None: + self.has_equations = True # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] _, depart = self.builder.app.registry.html_inline_math_renderers[name] From 063a64bab08d7a515e8b314f2fea8a1288864a1d Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 19 Oct 2024 21:11:26 +0100 Subject: [PATCH 2/5] Add `self.has_equations = True` to `{visit,depart}_math_block()` --- sphinx/writers/html5.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index 0b0d55858c5..e58182310f3 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -960,6 +960,7 @@ def visit_field(self, node: Element) -> None: def visit_math(self, node: Element, math_env: str = '') -> None: self.has_equations = True + # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] visit, _ = self.builder.app.registry.html_inline_math_renderers[name] @@ -967,6 +968,7 @@ def visit_math(self, node: Element, math_env: str = '') -> None: def depart_math(self, node: Element, math_env: str = '') -> None: self.has_equations = True + # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] _, depart = self.builder.app.registry.html_inline_math_renderers[name] @@ -974,12 +976,16 @@ def depart_math(self, node: Element, math_env: str = '') -> None: depart(self, node) def visit_math_block(self, node: Element, math_env: str = '') -> None: + self.has_equations = True + # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] visit, _ = self.builder.app.registry.html_block_math_renderers[name] visit(self, node) def depart_math_block(self, node: Element, math_env: str = '') -> None: + self.has_equations = True + # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] _, depart = self.builder.app.registry.html_block_math_renderers[name] From 0b9848ec68f65f27cef66070a9c29fc23eec45f1 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sun, 20 Oct 2024 01:24:08 +0100 Subject: [PATCH 3/5] Use an instance variable; rename; remove from depart_* methods --- sphinx/builders/html/__init__.py | 2 +- sphinx/ext/mathjax.py | 2 +- sphinx/writers/html.py | 5 ++--- sphinx/writers/html5.py | 10 +++------- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 961c875a513..e9b5ceb1d36 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -656,7 +656,7 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None: metatags = self.docwriter.clean_meta ctx = self.get_doc_context(docname, body, metatags) - ctx['has_equations'] = self.docwriter.has_equations + ctx['_has_equations'] = self.docwriter._has_equations self.handle_page(docname, ctx, event_arg=doctree) def write_doc_serialized(self, docname: str, doctree: nodes.document) -> None: diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py index e3034cba003..b8eda4bfa50 100644 --- a/sphinx/ext/mathjax.py +++ b/sphinx/ext/mathjax.py @@ -82,7 +82,7 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict raise ExtensionError(msg) builder = cast(StandaloneHTMLBuilder, app.builder) - if app.registry.html_assets_policy == 'always' or context.get('has_equations'): + if app.registry.html_assets_policy == 'always' or context.get('_has_equations'): # Enable mathjax only if equations exists if app.config.mathjax2_config: if app.config.mathjax_path == MATHJAX_URL: diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index c83dc809e00..1b495591796 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -24,11 +24,10 @@ class HTMLWriter(Writer): # type: ignore[misc] # override embed-stylesheet default value to False. settings_default_overrides = {'embed_stylesheet': False} - has_equations: bool = False - def __init__(self, builder: StandaloneHTMLBuilder) -> None: super().__init__() self.builder = builder + self._has_equations: bool = False def translate(self) -> None: # sadly, this is mostly copied from parent class @@ -59,4 +58,4 @@ def translate(self) -> None: ): setattr(self, attr, getattr(visitor, attr, None)) self.clean_meta = ''.join(self.visitor.meta[2:]) - self.has_equations = getattr(visitor, 'has_equations', False) + self._has_equations = getattr(visitor, '_has_equations', False) diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index e58182310f3..8a721e913ad 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -66,7 +66,7 @@ def __init__(self, document: nodes.document, builder: Builder) -> None: self._table_row_indices = [0] self._fieldlist_row_indices = [0] self.required_params_left = 0 - self.has_equations: bool = False + self._has_equations: bool = False def visit_start_of_file(self, node: Element) -> None: # only occurs in the single-file builder @@ -959,7 +959,7 @@ def visit_field(self, node: Element) -> None: node['classes'].append('field-odd') def visit_math(self, node: Element, math_env: str = '') -> None: - self.has_equations = True + self._has_equations = True # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] @@ -967,8 +967,6 @@ def visit_math(self, node: Element, math_env: str = '') -> None: visit(self, node) def depart_math(self, node: Element, math_env: str = '') -> None: - self.has_equations = True - # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] _, depart = self.builder.app.registry.html_inline_math_renderers[name] @@ -976,7 +974,7 @@ def depart_math(self, node: Element, math_env: str = '') -> None: depart(self, node) def visit_math_block(self, node: Element, math_env: str = '') -> None: - self.has_equations = True + self._has_equations = True # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] @@ -984,8 +982,6 @@ def visit_math_block(self, node: Element, math_env: str = '') -> None: visit(self, node) def depart_math_block(self, node: Element, math_env: str = '') -> None: - self.has_equations = True - # see validate_math_renderer name: str = self.builder.math_renderer_name # type: ignore[assignment] _, depart = self.builder.app.registry.html_block_math_renderers[name] From 68039ee2c1ed3e1a332c260fd1ab76760b13cdf7 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sun, 20 Oct 2024 01:29:54 +0100 Subject: [PATCH 4/5] CHANGES --- CHANGES.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 6cef031159d..f0a70f31fc7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -9,6 +9,10 @@ Dependencies Incompatible changes -------------------- +* #13044: Remove the internal and undocumented ``has_equations`` data and + :py:meth:`!has_equations` method from the :py:class:`MathDomain`` domain. + Patch by Adam Turner. + Deprecated ---------- From 224305ed9ea8045e9975a6225cd4bc5f716db26a Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sun, 20 Oct 2024 01:31:48 +0100 Subject: [PATCH 5/5] CHANGES --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index f0a70f31fc7..d342aa21e0e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,7 +10,7 @@ Incompatible changes -------------------- * #13044: Remove the internal and undocumented ``has_equations`` data and - :py:meth:`!has_equations` method from the :py:class:`MathDomain`` domain. + :py:meth:`!has_equations` method from the :py:class:`!MathDomain`` domain. Patch by Adam Turner. Deprecated