diff --git a/CHANGES.rst b/CHANGES.rst index 6cef031159d..d342aa21e0e 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 ---------- diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 742a351fd61..e9b5ceb1d36 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..b8eda4bfa50 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..1b495591796 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -27,6 +27,7 @@ class HTMLWriter(Writer): # type: ignore[misc] 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 @@ -57,3 +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) diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index e002788fc97..8a721e913ad 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,6 +959,8 @@ 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] @@ -971,6 +974,8 @@ 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]