diff --git a/sphinx/directives/__init__.py b/sphinx/directives/__init__.py index 81978b722..1177a258e 100644 --- a/sphinx/directives/__init__.py +++ b/sphinx/directives/__init__.py @@ -12,7 +12,7 @@ import re from docutils import nodes -from docutils.parsers.rst import Directive, directives, roles +from docutils.parsers.rst import directives, roles from sphinx import addnodes from sphinx.util.docfields import DocFieldTransformer @@ -217,7 +217,7 @@ class DefaultRole(SphinxDirective): return messages -class DefaultDomain(Directive): +class DefaultDomain(SphinxDirective): """ Directive to (re-)set the default domain for this source file. """ diff --git a/sphinx/domains/cpp.py b/sphinx/domains/cpp.py index f4234b0db..2998792ae 100644 --- a/sphinx/domains/cpp.py +++ b/sphinx/domains/cpp.py @@ -13,7 +13,7 @@ import re from copy import deepcopy from docutils import nodes -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from six import iteritems, text_type from sphinx import addnodes @@ -24,6 +24,7 @@ from sphinx.locale import _, __ from sphinx.roles import XRefRole from sphinx.util import logging from sphinx.util.docfields import Field, GroupedField +from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import make_refnode from sphinx.util.pycompat import UnicodeMixin @@ -5828,7 +5829,7 @@ class CPPEnumeratorObject(CPPObject): return parser.parse_declaration("enumerator") -class CPPNamespaceObject(Directive): +class CPPNamespaceObject(SphinxDirective): """ This directive is just to tell Sphinx that we're documenting stuff in namespace foo. @@ -5846,13 +5847,12 @@ class CPPNamespaceObject(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env - rootSymbol = env.domaindata['cpp']['root_symbol'] + rootSymbol = self.env.domaindata['cpp']['root_symbol'] if self.arguments[0].strip() in ('NULL', '0', 'nullptr'): symbol = rootSymbol stack = [] # type: List[Symbol] else: - parser = DefinitionParser(self.arguments[0], self, env.config) + parser = DefinitionParser(self.arguments[0], self, self.config) try: ast = parser.parse_namespace_object() parser.assert_end() @@ -5862,13 +5862,13 @@ class CPPNamespaceObject(Directive): ast = ASTNamespace(name, None) symbol = rootSymbol.add_name(ast.nestedName, ast.templatePrefix) stack = [symbol] - env.temp_data['cpp:parent_symbol'] = symbol - env.temp_data['cpp:namespace_stack'] = stack - env.ref_context['cpp:parent_key'] = symbol.get_lookup_key() + self.env.temp_data['cpp:parent_symbol'] = symbol + self.env.temp_data['cpp:namespace_stack'] = stack + self.env.ref_context['cpp:parent_key'] = symbol.get_lookup_key() return [] -class CPPNamespacePushObject(Directive): +class CPPNamespacePushObject(SphinxDirective): has_content = False required_arguments = 1 optional_arguments = 0 @@ -5881,10 +5881,9 @@ class CPPNamespacePushObject(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env if self.arguments[0].strip() in ('NULL', '0', 'nullptr'): return [] - parser = DefinitionParser(self.arguments[0], self, env.config) + parser = DefinitionParser(self.arguments[0], self, self.config) try: ast = parser.parse_namespace_object() parser.assert_end() @@ -5892,19 +5891,19 @@ class CPPNamespacePushObject(Directive): self.warn(e.description) name = _make_phony_error_name() ast = ASTNamespace(name, None) - oldParent = env.temp_data.get('cpp:parent_symbol', None) + oldParent = self.env.temp_data.get('cpp:parent_symbol', None) if not oldParent: - oldParent = env.domaindata['cpp']['root_symbol'] + oldParent = self.env.domaindata['cpp']['root_symbol'] symbol = oldParent.add_name(ast.nestedName, ast.templatePrefix) - stack = env.temp_data.get('cpp:namespace_stack', []) + stack = self.env.temp_data.get('cpp:namespace_stack', []) stack.append(symbol) - env.temp_data['cpp:parent_symbol'] = symbol - env.temp_data['cpp:namespace_stack'] = stack - env.ref_context['cpp:parent_key'] = symbol.get_lookup_key() + self.env.temp_data['cpp:parent_symbol'] = symbol + self.env.temp_data['cpp:namespace_stack'] = stack + self.env.ref_context['cpp:parent_key'] = symbol.get_lookup_key() return [] -class CPPNamespacePopObject(Directive): +class CPPNamespacePopObject(SphinxDirective): has_content = False required_arguments = 0 optional_arguments = 0 @@ -5917,8 +5916,7 @@ class CPPNamespacePopObject(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env - stack = env.temp_data.get('cpp:namespace_stack', None) + stack = self.env.temp_data.get('cpp:namespace_stack', None) if not stack or len(stack) == 0: self.warn("C++ namespace pop on empty stack. Defaulting to gobal scope.") stack = [] @@ -5927,10 +5925,10 @@ class CPPNamespacePopObject(Directive): if len(stack) > 0: symbol = stack[-1] else: - symbol = env.domaindata['cpp']['root_symbol'] - env.temp_data['cpp:parent_symbol'] = symbol - env.temp_data['cpp:namespace_stack'] = stack - env.ref_context['cpp:parent_key'] = symbol.get_lookup_key() + symbol = self.env.domaindata['cpp']['root_symbol'] + self.env.temp_data['cpp:parent_symbol'] = symbol + self.env.temp_data['cpp:namespace_stack'] = stack + self.env.ref_context['cpp:parent_key'] = symbol.get_lookup_key() return [] diff --git a/sphinx/domains/javascript.py b/sphinx/domains/javascript.py index 0ea6114d6..511a058c8 100644 --- a/sphinx/domains/javascript.py +++ b/sphinx/domains/javascript.py @@ -10,7 +10,7 @@ """ from docutils import nodes -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from sphinx import addnodes from sphinx.directives import ObjectDescription @@ -19,6 +19,7 @@ from sphinx.domains.python import _pseudo_parse_arglist from sphinx.locale import _ from sphinx.roles import XRefRole from sphinx.util.docfields import Field, GroupedField, TypedField +from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import make_refnode if False: @@ -220,7 +221,7 @@ class JSConstructor(JSCallable): allow_nesting = True -class JSModule(Directive): +class JSModule(SphinxDirective): """ Directive to mark description of a new JavaScript module. @@ -249,16 +250,15 @@ class JSModule(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env mod_name = self.arguments[0].strip() - env.ref_context['js:module'] = mod_name + self.env.ref_context['js:module'] = mod_name noindex = 'noindex' in self.options ret = [] if not noindex: - env.domaindata['js']['modules'][mod_name] = env.docname + self.env.domaindata['js']['modules'][mod_name] = self.env.docname # Make a duplicate entry in 'objects' to facilitate searching for # the module in JavaScriptDomain.find_obj() - env.domaindata['js']['objects'][mod_name] = (env.docname, 'module') + self.env.domaindata['js']['objects'][mod_name] = (self.env.docname, 'module') targetnode = nodes.target('', '', ids=['module-' + mod_name], ismod=True) self.state.document.note_explicit_target(targetnode) diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 1daac3227..0fe4b5eb4 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -12,7 +12,7 @@ import re from docutils import nodes -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from six import iteritems from sphinx import addnodes, locale @@ -23,6 +23,7 @@ from sphinx.locale import _, __ from sphinx.roles import XRefRole from sphinx.util import logging from sphinx.util.docfields import Field, GroupedField, TypedField +from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import make_refnode if False: @@ -555,7 +556,7 @@ class PyDecoratorMethod(PyDecoratorMixin, PyClassmember): return PyClassmember.run(self) -class PyModule(Directive): +class PyModule(SphinxDirective): """ Directive to mark description of a new module. """ @@ -573,19 +574,18 @@ class PyModule(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env modname = self.arguments[0].strip() noindex = 'noindex' in self.options - env.ref_context['py:module'] = modname + self.env.ref_context['py:module'] = modname ret = [] if not noindex: - env.domaindata['py']['modules'][modname] = (env.docname, - self.options.get('synopsis', ''), - self.options.get('platform', ''), - 'deprecated' in self.options) + self.env.domaindata['py']['modules'][modname] = (self.env.docname, + self.options.get('synopsis', ''), + self.options.get('platform', ''), + 'deprecated' in self.options) # make a duplicate entry in 'objects' to facilitate searching for # the module in PythonDomain.find_obj() - env.domaindata['py']['objects'][modname] = (env.docname, 'module') + self.env.domaindata['py']['objects'][modname] = (self.env.docname, 'module') targetnode = nodes.target('', '', ids=['module-' + modname], ismod=True) self.state.document.note_explicit_target(targetnode) @@ -599,7 +599,7 @@ class PyModule(Directive): return ret -class PyCurrentModule(Directive): +class PyCurrentModule(SphinxDirective): """ This directive is just to tell Sphinx that we're documenting stuff in module foo, but links to module foo won't lead here. @@ -613,12 +613,11 @@ class PyCurrentModule(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env modname = self.arguments[0].strip() if modname == 'None': - env.ref_context.pop('py:module', None) + self.env.ref_context.pop('py:module', None) else: - env.ref_context['py:module'] = modname + self.env.ref_context['py:module'] = modname return [] diff --git a/sphinx/domains/std.py b/sphinx/domains/std.py index 6518091f6..bc6b4de6c 100644 --- a/sphinx/domains/std.py +++ b/sphinx/domains/std.py @@ -15,7 +15,7 @@ import warnings from copy import copy from docutils import nodes -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from docutils.statemachine import ViewList from six import iteritems @@ -26,11 +26,13 @@ from sphinx.domains import Domain, ObjType from sphinx.locale import _, __ from sphinx.roles import XRefRole from sphinx.util import ws_re, logging, docname_join +from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import clean_astext, make_refnode if False: # For type annotation from typing import Any, Callable, Dict, Iterator, List, Tuple, Type, Union # NOQA + from docutils.parsers.rst import Directive # NOQA from sphinx.application import Sphinx # NOQA from sphinx.builders import Builder # NOQA from sphinx.environment import BuildEnvironment # NOQA @@ -107,7 +109,7 @@ class EnvVarXRefRole(XRefRole): return [indexnode, targetnode, node], [] -class Target(Directive): +class Target(SphinxDirective): """ Generic target for user-defined cross-reference types. """ @@ -121,7 +123,6 @@ class Target(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env # normalize whitespace in fullname like XRefRole does fullname = ws_re.sub(' ', self.arguments[0].strip()) targetname = '%s-%s' % (self.name, fullname) @@ -141,8 +142,8 @@ class Target(Directive): name = self.name if ':' in self.name: _, name = self.name.split(':', 1) - env.domaindata['std']['objects'][name, fullname] = \ - env.docname, targetname + self.env.domaindata['std']['objects'][name, fullname] = \ + self.env.docname, targetname return ret @@ -204,7 +205,7 @@ class Cmdoption(ObjectDescription): signode['ids'][0], '', None)) -class Program(Directive): +class Program(SphinxDirective): """ Directive to name the program for which options are documented. """ @@ -217,12 +218,11 @@ class Program(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env program = ws_re.sub('-', self.arguments[0].strip()) if program == 'None': - env.ref_context.pop('std:program', None) + self.env.ref_context.pop('std:program', None) else: - env.ref_context['std:program'] = program + self.env.ref_context['std:program'] = program return [] @@ -270,7 +270,7 @@ def make_glossary_term(env, textnodes, index_key, source, lineno, new_id=None): return term -class Glossary(Directive): +class Glossary(SphinxDirective): """ Directive to create a glossary with cross-reference targets for :term: roles. @@ -286,7 +286,6 @@ class Glossary(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env node = addnodes.glossary() node.document = self.state.document @@ -358,7 +357,7 @@ class Glossary(Directive): textnodes, sysmsg = self.state.inline_text(parts[0], lineno) # use first classifier as a index key - term = make_glossary_term(env, textnodes, parts[1], source, lineno) + term = make_glossary_term(self.env, textnodes, parts[1], source, lineno) term.rawsource = line system_messages.extend(sysmsg) termtexts.append(term.astext()) @@ -403,7 +402,7 @@ def token_xrefs(text): return retnodes -class ProductionList(Directive): +class ProductionList(SphinxDirective): """ Directive to list grammar productions. """ @@ -416,8 +415,7 @@ class ProductionList(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env - objects = env.domaindata['std']['objects'] + objects = self.env.domaindata['std']['objects'] node = addnodes.productionlist() messages = [] # type: List[nodes.Node] i = 0 @@ -438,7 +436,7 @@ class ProductionList(Directive): if idname not in self.state.document.ids: subnode['ids'].append(idname) self.state.document.note_implicit_target(subnode, subnode) - objects['token', subnode['tokenname']] = env.docname, idname + objects['token', subnode['tokenname']] = self.env.docname, idname subnode.extend(token_xrefs(tokens)) node.append(subnode) return [node] + messages diff --git a/sphinx/ext/autodoc/directive.py b/sphinx/ext/autodoc/directive.py index 77b3f7aaf..64d19fcc7 100644 --- a/sphinx/ext/autodoc/directive.py +++ b/sphinx/ext/autodoc/directive.py @@ -8,13 +8,12 @@ """ from docutils import nodes -from docutils.parsers.rst import Directive from docutils.statemachine import ViewList from docutils.utils import assemble_option_dict from sphinx.ext.autodoc import Options, get_documenters from sphinx.util import logging -from sphinx.util.docutils import switch_source_input +from sphinx.util.docutils import SphinxDirective, switch_source_input from sphinx.util.nodes import nested_parse_with_titles if False: @@ -91,7 +90,7 @@ def parse_generated_content(state, content, documenter): return node.children -class AutodocDirective(Directive): +class AutodocDirective(SphinxDirective): """A directive class for all autodoc directives. It works as a dispatcher of Documenters. It invokes a Documenter on running. After the processing, it parses and returns @@ -105,7 +104,6 @@ class AutodocDirective(Directive): def run(self): # type: () -> List[nodes.Node] - env = self.state.document.settings.env reporter = self.state.document.reporter try: @@ -116,11 +114,11 @@ class AutodocDirective(Directive): # look up target Documenter objtype = self.name[4:] # strip prefix (auto-). - doccls = get_documenters(env.app)[objtype] + doccls = get_documenters(self.env.app)[objtype] # process the options with the selected documenter's option_spec try: - documenter_options = process_documenter_options(doccls, env.config, self.options) + documenter_options = process_documenter_options(doccls, self.config, self.options) except (KeyError, ValueError, TypeError) as exc: # an option is either unknown or has a wrong type logger.error('An option to %s is either unknown or has an invalid value: %s' % @@ -128,7 +126,7 @@ class AutodocDirective(Directive): return [] # generate the output - params = DocumenterBridge(env, reporter, documenter_options, lineno) + params = DocumenterBridge(self.env, reporter, documenter_options, lineno) documenter = doccls(params, self.arguments[0]) documenter.generate(more_content=self.content) if not params.result: diff --git a/sphinx/ext/autosummary/__init__.py b/sphinx/ext/autosummary/__init__.py index 80a2b4e63..8c6bafbd9 100644 --- a/sphinx/ext/autosummary/__init__.py +++ b/sphinx/ext/autosummary/__init__.py @@ -62,7 +62,7 @@ import warnings from types import ModuleType from docutils import nodes -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from docutils.parsers.rst.states import RSTStateMachine, state_classes from docutils.statemachine import ViewList from six import string_types @@ -78,7 +78,7 @@ from sphinx.ext.autodoc.importer import import_module from sphinx.locale import __ from sphinx.pycode import ModuleAnalyzer, PycodeError from sphinx.util import import_object, rst, logging -from sphinx.util.docutils import NullReporter, new_document +from sphinx.util.docutils import NullReporter, SphinxDirective, new_document if False: # For type annotation @@ -220,7 +220,7 @@ def get_documenter(*args): # -- .. autosummary:: ---------------------------------------------------------- -class Autosummary(Directive): +class Autosummary(SphinxDirective): """ Pretty table containing short signatures and summaries of functions etc. @@ -244,7 +244,6 @@ class Autosummary(Directive): def run(self): # type: () -> List[nodes.Node] - self.env = env = self.state.document.settings.env self.genopt = Options() self.warnings = [] # type: List[nodes.Node] self.result = ViewList() @@ -255,14 +254,14 @@ class Autosummary(Directive): nodes = self.get_table(items) if 'toctree' in self.options: - dirname = posixpath.dirname(env.docname) + dirname = posixpath.dirname(self.env.docname) tree_prefix = self.options['toctree'].strip() docnames = [] for name, sig, summary, real_name in items: docname = posixpath.join(tree_prefix, real_name) docname = posixpath.normpath(posixpath.join(dirname, docname)) - if docname not in env.found_docs: + if docname not in self.env.found_docs: self.warn('toctree references unknown document %r' % docname) docnames.append(docname) @@ -283,9 +282,7 @@ class Autosummary(Directive): """Try to import the given names, and return a list of ``[(name, signature, summary_string, real_name), ...]``. """ - env = self.state.document.settings.env - - prefixes = get_import_prefixes_from_env(env) + prefixes = get_import_prefixes_from_env(self.env) items = [] # type: List[Tuple[unicode, unicode, unicode, unicode]] diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index b86775d5e..68877371c 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -19,7 +19,7 @@ import time from os import path from docutils import nodes -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from packaging.specifiers import SpecifierSet, InvalidSpecifier from packaging.version import Version from six import itervalues, StringIO, binary_type, text_type, PY2 @@ -29,6 +29,7 @@ from sphinx.builders import Builder from sphinx.locale import __ from sphinx.util import force_decode, logging from sphinx.util.console import bold # type: ignore +from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import set_source_info from sphinx.util.osutil import fs_encoding, relpath @@ -77,7 +78,7 @@ def is_allowed_version(spec, version): # set up the necessary directives -class TestDirective(Directive): +class TestDirective(SphinxDirective): """ Base class for doctest-related directives. """ diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index 127146f94..11d9e54a0 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -18,7 +18,7 @@ from os import path from subprocess import Popen, PIPE from docutils import nodes -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from docutils.statemachine import ViewList from six import text_type @@ -26,11 +26,13 @@ import sphinx from sphinx.errors import SphinxError from sphinx.locale import _, __ from sphinx.util import logging +from sphinx.util.docutils import SphinxDirective from sphinx.util.i18n import search_image_for_language from sphinx.util.osutil import ensuredir, ENOENT, EPIPE, EINVAL if False: # For type annotation + from docutils.parsers.rst import Directive # NOQA from typing import Any, Dict, List, Tuple # NOQA from sphinx.application import Sphinx # NOQA @@ -111,7 +113,7 @@ def align_spec(argument): return directives.choice(argument, ('left', 'center', 'right')) -class Graphviz(Directive): +class Graphviz(SphinxDirective): """ Directive to insert arbitrary dot markup. """ @@ -135,12 +137,11 @@ class Graphviz(Directive): return [document.reporter.warning( __('Graphviz directive cannot have both content and ' 'a filename argument'), line=self.lineno)] - env = self.state.document.settings.env - argument = search_image_for_language(self.arguments[0], env) - rel_filename, filename = env.relfn2path(argument) - env.note_dependency(rel_filename) + argument = search_image_for_language(self.arguments[0], self.env) + rel_filename, filename = self.env.relfn2path(argument) + self.env.note_dependency(rel_filename) try: - with codecs.open(filename, 'r', 'utf-8') as fp: + with codecs.open(filename, 'r', 'utf-8') as fp: # type: ignore dotcode = fp.read() except (IOError, OSError): return [document.reporter.warning( @@ -170,7 +171,7 @@ class Graphviz(Directive): return [node] -class GraphvizSimple(Directive): +class GraphvizSimple(SphinxDirective): """ Directive to insert arbitrary dot markup. """ diff --git a/sphinx/ext/ifconfig.py b/sphinx/ext/ifconfig.py index 864e7a185..f22a37e92 100644 --- a/sphinx/ext/ifconfig.py +++ b/sphinx/ext/ifconfig.py @@ -21,9 +21,9 @@ """ from docutils import nodes -from docutils.parsers.rst import Directive import sphinx +from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import set_source_info if False: @@ -36,7 +36,7 @@ class ifconfig(nodes.Element): pass -class IfConfig(Directive): +class IfConfig(SphinxDirective): has_content = True required_arguments = 1 diff --git a/sphinx/ext/inheritance_diagram.py b/sphinx/ext/inheritance_diagram.py index 14593ac99..d91848aa1 100644 --- a/sphinx/ext/inheritance_diagram.py +++ b/sphinx/ext/inheritance_diagram.py @@ -42,7 +42,7 @@ import sys from hashlib import md5 from docutils import nodes -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from six import text_type from six.moves import builtins @@ -51,6 +51,7 @@ from sphinx.ext.graphviz import render_dot_html, render_dot_latex, \ render_dot_texinfo, figure_wrapper from sphinx.pycode import ModuleAnalyzer from sphinx.util import force_decode +from sphinx.util.docutils import SphinxDirective if False: # For type annotation @@ -322,7 +323,7 @@ class inheritance_diagram(nodes.General, nodes.Element): pass -class InheritanceDiagram(Directive): +class InheritanceDiagram(SphinxDirective): """ Run when the inheritance_diagram directive is first encountered. """ @@ -341,9 +342,8 @@ class InheritanceDiagram(Directive): # type: () -> List[nodes.Node] node = inheritance_diagram() node.document = self.state.document - env = self.state.document.settings.env class_names = self.arguments[0].split() - class_role = env.get_domain('py').role('class') + class_role = self.env.get_domain('py').role('class') # Store the original content for use as a hash node['parts'] = self.options.get('parts', 0) node['content'] = ', '.join(class_names) @@ -356,10 +356,10 @@ class InheritanceDiagram(Directive): # Create a graph starting with the list of classes try: graph = InheritanceGraph( - class_names, env.ref_context.get('py:module'), + class_names, self.env.ref_context.get('py:module'), parts=node['parts'], private_bases='private-bases' in self.options, - aliases=env.config.inheritance_alias, + aliases=self.config.inheritance_alias, top_classes=node['top-classes']) except InheritanceException as err: return [node.document.reporter.warning(err.args[0], diff --git a/sphinx/ext/mathbase.py b/sphinx/ext/mathbase.py index 4789b117e..e6a6929e6 100644 --- a/sphinx/ext/mathbase.py +++ b/sphinx/ext/mathbase.py @@ -11,13 +11,14 @@ 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: @@ -212,7 +213,7 @@ def is_in_section_title(node): return False -class MathDirective(Directive): +class MathDirective(SphinxDirective): has_content = True required_arguments = 0 @@ -238,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'): @@ -249,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 diff --git a/sphinx/ext/todo.py b/sphinx/ext/todo.py index c7ef16b51..d7413ac61 100644 --- a/sphinx/ext/todo.py +++ b/sphinx/ext/todo.py @@ -13,7 +13,6 @@ """ from docutils import nodes -from docutils.parsers.rst import Directive from docutils.parsers.rst import directives from docutils.parsers.rst.directives.admonitions import BaseAdmonition @@ -21,6 +20,7 @@ import sphinx from sphinx.environment import NoUri from sphinx.locale import _, __ from sphinx.util import logging +from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import set_source_info from sphinx.util.texescape import tex_escape_map @@ -41,7 +41,7 @@ class todolist(nodes.General, nodes.Element): pass -class Todo(BaseAdmonition): +class Todo(BaseAdmonition, SphinxDirective): """ A todo entry, displayed (if configured) in the form of an admonition. """ @@ -67,10 +67,9 @@ class Todo(BaseAdmonition): todo.insert(0, nodes.title(text=_('Todo'))) set_source_info(self, todo) - env = self.state.document.settings.env - targetid = 'index-%s' % env.new_serialno('index') + targetid = 'index-%s' % self.env.new_serialno('index') # Stash the target to be retrieved later in latex_visit_todo_node. - todo['targetref'] = '%s:%s' % (env.docname, targetid) + todo['targetref'] = '%s:%s' % (self.env.docname, targetid) targetnode = nodes.target('', '', ids=[targetid]) return [targetnode, todo] @@ -107,7 +106,7 @@ def process_todos(app, doctree): location=node) -class TodoList(Directive): +class TodoList(SphinxDirective): """ A list of all todo entries. """