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] 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]