Skip to content

Commit

Permalink
Enable automatic formatting for sphinx/domains/math.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jan 1, 2025
1 parent 4a81818 commit f42f367
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
1 change: 0 additions & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ preview = true
quote-style = "single"
exclude = [
"sphinx/builders/latex/constants.py",
"sphinx/domains/math.py",
"sphinx/domains/python/_annotations.py",
"sphinx/domains/python/__init__.py",
"sphinx/domains/python/_object.py",
Expand Down
64 changes: 44 additions & 20 deletions sphinx/domains/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@


class MathReferenceRole(XRefRole):
def result_nodes(self, document: nodes.document, env: BuildEnvironment, node: Element,
is_ref: bool) -> tuple[list[Node], list[system_message]]:
def result_nodes(
self,
document: nodes.document,
env: BuildEnvironment,
node: Element,
is_ref: bool,
) -> tuple[list[Node], list[system_message]]:
node['refdomain'] = 'math'
return [node], []

Expand Down Expand Up @@ -60,8 +65,12 @@ def equations(self) -> dict[str, tuple[str, int]]:
def note_equation(self, docname: str, labelid: str, location: Any = None) -> None:
if labelid in self.equations:
other = self.equations[labelid][0]
logger.warning(__('duplicate label of equation %s, other instance in %s'),
labelid, other, location=location)
logger.warning(
__('duplicate label of equation %s, other instance in %s'),
labelid,
other,
location=location,
)

self.equations[labelid] = (docname, self.env.new_serialno('eqno') + 1)

Expand All @@ -71,8 +80,9 @@ 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 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)

Expand All @@ -93,9 +103,16 @@ def merge_domaindata(self, docnames: Set[str], otherdata: dict[str, Any]) -> Non
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,
) -> nodes.reference | None:
def resolve_xref(
self,
env: BuildEnvironment,
fromdocname: str,
builder: Builder,
typ: str,
target: str,
node: pending_xref,
contnode: Element,
) -> nodes.reference | None:
assert typ in {'eq', 'numref'}
result = self.equations.get(target)
if result:
Expand All @@ -104,7 +121,8 @@ def resolve_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder
node_id = make_id('equation-%s' % target)
if env.config.math_numfig and env.config.numfig:
if docname in env.toc_fignumbers:
numbers = env.toc_fignumbers[docname]['displaymath'].get(node_id, ())
toc_dm = env.toc_fignumbers[docname]['displaymath']
numbers = toc_dm.get(node_id, ())
eqno = '.'.join(map(str, numbers))
eqno = env.config.math_numsep.join(eqno.rsplit('.', 1))
else:
Expand All @@ -113,21 +131,27 @@ def resolve_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder
eqno = str(number)

try:
eqref_format = env.config.math_eqref_format or "({number})"
eqref_format = env.config.math_eqref_format or '({number})'
title = nodes.Text(eqref_format.format(number=eqno))
except KeyError as exc:
logger.warning(__('Invalid math_eqref_format: %r'), exc,
location=node)
title = nodes.Text("(%d)" % number)
title = nodes.Text("(%d)" % number)
logger.warning(__('Invalid math_eqref_format: %r'), exc, location=node)
title = nodes.Text('(%d)' % number)
return make_refnode(builder, fromdocname, docname, node_id, title)
else:
return None

def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder,
target: str, node: pending_xref, contnode: Element,
) -> list[tuple[str, nodes.reference]]:
refnode = self.resolve_xref(env, fromdocname, builder, 'eq', target, node, contnode)
def resolve_any_xref(
self,
env: BuildEnvironment,
fromdocname: str,
builder: Builder,
target: str,
node: pending_xref,
contnode: Element,
) -> list[tuple[str, nodes.reference]]:
refnode = self.resolve_xref(
env, fromdocname, builder, 'eq', target, node, contnode
)
if refnode is None:
return []
else:
Expand All @@ -143,7 +167,7 @@ def has_equations(self, docname: str | None = None) -> bool:
return (
self.data['has_equations'].get(docname, False)
or any(map(self.has_equations, self.env.toctree_includes.get(docname, ())))
)
) # fmt: skip


def setup(app: Sphinx) -> ExtensionMetadata:
Expand Down

0 comments on commit f42f367

Please sign in to comment.