mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Enable automatic formatting for `sphinx/domains/math.py
`
This commit is contained in:
parent
4a81818bb3
commit
f42f3676d5
@ -394,7 +394,6 @@ preview = true
|
|||||||
quote-style = "single"
|
quote-style = "single"
|
||||||
exclude = [
|
exclude = [
|
||||||
"sphinx/builders/latex/constants.py",
|
"sphinx/builders/latex/constants.py",
|
||||||
"sphinx/domains/math.py",
|
|
||||||
"sphinx/domains/python/_annotations.py",
|
"sphinx/domains/python/_annotations.py",
|
||||||
"sphinx/domains/python/__init__.py",
|
"sphinx/domains/python/__init__.py",
|
||||||
"sphinx/domains/python/_object.py",
|
"sphinx/domains/python/_object.py",
|
||||||
|
@ -27,8 +27,13 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class MathReferenceRole(XRefRole):
|
class MathReferenceRole(XRefRole):
|
||||||
def result_nodes(self, document: nodes.document, env: BuildEnvironment, node: Element,
|
def result_nodes(
|
||||||
is_ref: bool) -> tuple[list[Node], list[system_message]]:
|
self,
|
||||||
|
document: nodes.document,
|
||||||
|
env: BuildEnvironment,
|
||||||
|
node: Element,
|
||||||
|
is_ref: bool,
|
||||||
|
) -> tuple[list[Node], list[system_message]]:
|
||||||
node['refdomain'] = 'math'
|
node['refdomain'] = 'math'
|
||||||
return [node], []
|
return [node], []
|
||||||
|
|
||||||
@ -60,8 +65,12 @@ class MathDomain(Domain):
|
|||||||
def note_equation(self, docname: str, labelid: str, location: Any = None) -> None:
|
def note_equation(self, docname: str, labelid: str, location: Any = None) -> None:
|
||||||
if labelid in self.equations:
|
if labelid in self.equations:
|
||||||
other = self.equations[labelid][0]
|
other = self.equations[labelid][0]
|
||||||
logger.warning(__('duplicate label of equation %s, other instance in %s'),
|
logger.warning(
|
||||||
labelid, other, location=location)
|
__('duplicate label of equation %s, other instance in %s'),
|
||||||
|
labelid,
|
||||||
|
other,
|
||||||
|
location=location,
|
||||||
|
)
|
||||||
|
|
||||||
self.equations[labelid] = (docname, self.env.new_serialno('eqno') + 1)
|
self.equations[labelid] = (docname, self.env.new_serialno('eqno') + 1)
|
||||||
|
|
||||||
@ -71,8 +80,9 @@ class MathDomain(Domain):
|
|||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def process_doc(self, env: BuildEnvironment, docname: str,
|
def process_doc(
|
||||||
document: nodes.document) -> None:
|
self, env: BuildEnvironment, docname: str, document: nodes.document
|
||||||
|
) -> None:
|
||||||
def math_node(node: Node) -> bool:
|
def math_node(node: Node) -> bool:
|
||||||
return isinstance(node, nodes.math | nodes.math_block)
|
return isinstance(node, nodes.math | nodes.math_block)
|
||||||
|
|
||||||
@ -93,9 +103,16 @@ class MathDomain(Domain):
|
|||||||
for docname in docnames:
|
for docname in docnames:
|
||||||
self.data['has_equations'][docname] = otherdata['has_equations'][docname]
|
self.data['has_equations'][docname] = otherdata['has_equations'][docname]
|
||||||
|
|
||||||
def resolve_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder,
|
def resolve_xref(
|
||||||
typ: str, target: str, node: pending_xref, contnode: Element,
|
self,
|
||||||
) -> nodes.reference | None:
|
env: BuildEnvironment,
|
||||||
|
fromdocname: str,
|
||||||
|
builder: Builder,
|
||||||
|
typ: str,
|
||||||
|
target: str,
|
||||||
|
node: pending_xref,
|
||||||
|
contnode: Element,
|
||||||
|
) -> nodes.reference | None:
|
||||||
assert typ in {'eq', 'numref'}
|
assert typ in {'eq', 'numref'}
|
||||||
result = self.equations.get(target)
|
result = self.equations.get(target)
|
||||||
if result:
|
if result:
|
||||||
@ -104,7 +121,8 @@ class MathDomain(Domain):
|
|||||||
node_id = make_id('equation-%s' % target)
|
node_id = make_id('equation-%s' % target)
|
||||||
if env.config.math_numfig and env.config.numfig:
|
if env.config.math_numfig and env.config.numfig:
|
||||||
if docname in env.toc_fignumbers:
|
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 = '.'.join(map(str, numbers))
|
||||||
eqno = env.config.math_numsep.join(eqno.rsplit('.', 1))
|
eqno = env.config.math_numsep.join(eqno.rsplit('.', 1))
|
||||||
else:
|
else:
|
||||||
@ -113,21 +131,27 @@ class MathDomain(Domain):
|
|||||||
eqno = str(number)
|
eqno = str(number)
|
||||||
|
|
||||||
try:
|
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))
|
title = nodes.Text(eqref_format.format(number=eqno))
|
||||||
except KeyError as exc:
|
except KeyError as exc:
|
||||||
logger.warning(__('Invalid math_eqref_format: %r'), exc,
|
logger.warning(__('Invalid math_eqref_format: %r'), exc, location=node)
|
||||||
location=node)
|
title = nodes.Text('(%d)' % number)
|
||||||
title = nodes.Text("(%d)" % number)
|
|
||||||
title = nodes.Text("(%d)" % number)
|
|
||||||
return make_refnode(builder, fromdocname, docname, node_id, title)
|
return make_refnode(builder, fromdocname, docname, node_id, title)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder,
|
def resolve_any_xref(
|
||||||
target: str, node: pending_xref, contnode: Element,
|
self,
|
||||||
) -> list[tuple[str, nodes.reference]]:
|
env: BuildEnvironment,
|
||||||
refnode = self.resolve_xref(env, fromdocname, builder, 'eq', target, node, contnode)
|
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:
|
if refnode is None:
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
@ -143,7 +167,7 @@ class MathDomain(Domain):
|
|||||||
return (
|
return (
|
||||||
self.data['has_equations'].get(docname, False)
|
self.data['has_equations'].get(docname, False)
|
||||||
or any(map(self.has_equations, self.env.toctree_includes.get(docname, ())))
|
or any(map(self.has_equations, self.env.toctree_includes.get(docname, ())))
|
||||||
)
|
) # fmt: skip
|
||||||
|
|
||||||
|
|
||||||
def setup(app: Sphinx) -> ExtensionMetadata:
|
def setup(app: Sphinx) -> ExtensionMetadata:
|
||||||
|
Loading…
Reference in New Issue
Block a user