diff options
Diffstat (limited to 'sphinx/ext/mathbase.py')
-rw-r--r-- | sphinx/ext/mathbase.py | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/sphinx/ext/mathbase.py b/sphinx/ext/mathbase.py index 3cc734537..e6a6929e6 100644 --- a/sphinx/ext/mathbase.py +++ b/sphinx/ext/mathbase.py @@ -11,19 +11,21 @@ from docutils import nodes, utils from docutils.nodes import make_id -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from sphinx.config import string_classes from sphinx.domains import Domain from sphinx.locale import __ from sphinx.roles import XRefRole from sphinx.util import logging +from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import make_refnode, set_source_info if False: # For type annotation from typing import Any, Callable, Dict, Iterable, List, Tuple # NOQA from docutils.parsers.rst.states import Inliner # NOQA + from docutils.writers.html4css1 import Writer # NOQA from sphinx.application import Sphinx # NOQA from sphinx.builders import Builder # NOQA from sphinx.environment import BuildEnvironment # NOQA @@ -61,6 +63,9 @@ class MathDomain(Domain): dangling_warnings = { 'eq': 'equation not found: %(target)s', } + enumerable_nodes = { # node_class -> (figtype, title_getter) + displaymath: ('displaymath', None), + } # type: Dict[nodes.Node, Tuple[unicode, Callable]] def clear_doc(self, docname): # type: (unicode) -> None @@ -97,7 +102,7 @@ class MathDomain(Domain): eqref_format = env.config.math_eqref_format or "({number})" title = nodes.Text(eqref_format.format(number=number)) except KeyError as exc: - logger.warning('Invalid math_eqref_format: %r', exc, + logger.warning(__('Invalid math_eqref_format: %r'), exc, location=node) title = nodes.Text("(%d)" % number) title = nodes.Text("(%d)" % number) @@ -136,6 +141,7 @@ class MathDomain(Domain): def get_node_equation_number(writer, node): + # type: (Writer, nodes.Node) -> unicode if writer.builder.config.math_numfig and writer.builder.config.numfig: figtype = 'displaymath' if writer.builder.name == 'singlehtml': @@ -207,7 +213,7 @@ def is_in_section_title(node): return False -class MathDirective(Directive): +class MathDirective(SphinxDirective): has_content = True required_arguments = 0 @@ -233,7 +239,7 @@ class MathDirective(Directive): if 'label' in self.options: node['label'] = self.options['label'] node['nowrap'] = 'nowrap' in self.options - node['docname'] = self.state.document.settings.env.docname + node['docname'] = self.env.docname ret = [node] set_source_info(self, node) if hasattr(self, 'src'): @@ -244,21 +250,20 @@ class MathDirective(Directive): def add_target(self, ret): # type: (List[nodes.Node]) -> None node = ret[0] - env = self.state.document.settings.env # assign label automatically if math_number_all enabled - if node['label'] == '' or (env.config.math_number_all and not node['label']): - seq = env.new_serialno('sphinx.ext.math#equations') - node['label'] = "%s:%d" % (env.docname, seq) + if node['label'] == '' or (self.config.math_number_all and not node['label']): + seq = self.env.new_serialno('sphinx.ext.math#equations') + node['label'] = "%s:%d" % (self.env.docname, seq) # no targets and numbers are needed if not node['label']: return # register label to domain - domain = env.get_domain('math') + domain = self.env.get_domain('math') try: - eqno = domain.add_equation(env, env.docname, node['label']) + eqno = domain.add_equation(self.env, self.env.docname, node['label']) # type: ignore # NOQA node['number'] = eqno # add target node @@ -307,7 +312,7 @@ def latex_visit_eqref(self, node): ref = '\\ref{%s}' % label self.body.append(eqref_format.format(number=ref)) except KeyError as exc: - logger.warning('Invalid math_eqref_format: %r', exc, + logger.warning(__('Invalid math_eqref_format: %r'), exc, location=node) self.body.append('\\eqref{%s}' % label) else: @@ -376,12 +381,12 @@ def setup_math(app, htmlinlinevisitors, htmldisplayvisitors): man=(man_visit_math, None), texinfo=(texinfo_visit_math, None), html=htmlinlinevisitors) - app.add_enumerable_node(displaymath, 'displaymath', - latex=(latex_visit_displaymath, None), - text=(text_visit_displaymath, None), - man=(man_visit_displaymath, man_depart_displaymath), - texinfo=(texinfo_visit_displaymath, texinfo_depart_displaymath), - html=htmldisplayvisitors) + app.add_node(displaymath, + latex=(latex_visit_displaymath, None), + text=(text_visit_displaymath, None), + man=(man_visit_displaymath, man_depart_displaymath), + texinfo=(texinfo_visit_displaymath, texinfo_depart_displaymath), + html=htmldisplayvisitors) app.add_node(eqref, latex=(latex_visit_eqref, None)) app.add_role('math', math_role) app.add_role('eq', EqXRefRole(warn_dangling=True)) |