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

Remove has_equations from the mathematics domain #13044

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 0 additions & 22 deletions sphinx/domains/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions sphinx/ext/mathjax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions sphinx/writers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
AA-Turner marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, builder: StandaloneHTMLBuilder) -> None:
super().__init__()
self.builder = builder
Expand Down Expand Up @@ -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)
9 changes: 9 additions & 0 deletions sphinx/writers/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -958,25 +959,33 @@ 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
AA-Turner marked this conversation as resolved.
Show resolved Hide resolved

# see validate_math_renderer
name: str = self.builder.math_renderer_name # type: ignore[assignment]
_, depart = self.builder.app.registry.html_inline_math_renderers[name]
if depart:
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]
Expand Down
Loading