refactor: Replace Directive by SphinxDirective

This commit is contained in:
Takeshi KOMIYA 2018-05-09 22:58:00 +09:00
parent 169297d0b7
commit 6c08963f25
13 changed files with 101 additions and 110 deletions

View File

@ -12,7 +12,7 @@
import re import re
from docutils import nodes 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 import addnodes
from sphinx.util.docfields import DocFieldTransformer from sphinx.util.docfields import DocFieldTransformer
@ -217,7 +217,7 @@ class DefaultRole(SphinxDirective):
return messages return messages
class DefaultDomain(Directive): class DefaultDomain(SphinxDirective):
""" """
Directive to (re-)set the default domain for this source file. Directive to (re-)set the default domain for this source file.
""" """

View File

@ -13,7 +13,7 @@ import re
from copy import deepcopy from copy import deepcopy
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive, directives from docutils.parsers.rst import directives
from six import iteritems, text_type from six import iteritems, text_type
from sphinx import addnodes from sphinx import addnodes
@ -24,6 +24,7 @@ from sphinx.locale import _, __
from sphinx.roles import XRefRole from sphinx.roles import XRefRole
from sphinx.util import logging from sphinx.util import logging
from sphinx.util.docfields import Field, GroupedField from sphinx.util.docfields import Field, GroupedField
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import make_refnode from sphinx.util.nodes import make_refnode
from sphinx.util.pycompat import UnicodeMixin from sphinx.util.pycompat import UnicodeMixin
@ -5828,7 +5829,7 @@ class CPPEnumeratorObject(CPPObject):
return parser.parse_declaration("enumerator") return parser.parse_declaration("enumerator")
class CPPNamespaceObject(Directive): class CPPNamespaceObject(SphinxDirective):
""" """
This directive is just to tell Sphinx that we're documenting stuff in This directive is just to tell Sphinx that we're documenting stuff in
namespace foo. namespace foo.
@ -5846,13 +5847,12 @@ class CPPNamespaceObject(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env rootSymbol = self.env.domaindata['cpp']['root_symbol']
rootSymbol = env.domaindata['cpp']['root_symbol']
if self.arguments[0].strip() in ('NULL', '0', 'nullptr'): if self.arguments[0].strip() in ('NULL', '0', 'nullptr'):
symbol = rootSymbol symbol = rootSymbol
stack = [] # type: List[Symbol] stack = [] # type: List[Symbol]
else: else:
parser = DefinitionParser(self.arguments[0], self, env.config) parser = DefinitionParser(self.arguments[0], self, self.config)
try: try:
ast = parser.parse_namespace_object() ast = parser.parse_namespace_object()
parser.assert_end() parser.assert_end()
@ -5862,13 +5862,13 @@ class CPPNamespaceObject(Directive):
ast = ASTNamespace(name, None) ast = ASTNamespace(name, None)
symbol = rootSymbol.add_name(ast.nestedName, ast.templatePrefix) symbol = rootSymbol.add_name(ast.nestedName, ast.templatePrefix)
stack = [symbol] stack = [symbol]
env.temp_data['cpp:parent_symbol'] = symbol self.env.temp_data['cpp:parent_symbol'] = symbol
env.temp_data['cpp:namespace_stack'] = stack self.env.temp_data['cpp:namespace_stack'] = stack
env.ref_context['cpp:parent_key'] = symbol.get_lookup_key() self.env.ref_context['cpp:parent_key'] = symbol.get_lookup_key()
return [] return []
class CPPNamespacePushObject(Directive): class CPPNamespacePushObject(SphinxDirective):
has_content = False has_content = False
required_arguments = 1 required_arguments = 1
optional_arguments = 0 optional_arguments = 0
@ -5881,10 +5881,9 @@ class CPPNamespacePushObject(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env
if self.arguments[0].strip() in ('NULL', '0', 'nullptr'): if self.arguments[0].strip() in ('NULL', '0', 'nullptr'):
return [] return []
parser = DefinitionParser(self.arguments[0], self, env.config) parser = DefinitionParser(self.arguments[0], self, self.config)
try: try:
ast = parser.parse_namespace_object() ast = parser.parse_namespace_object()
parser.assert_end() parser.assert_end()
@ -5892,19 +5891,19 @@ class CPPNamespacePushObject(Directive):
self.warn(e.description) self.warn(e.description)
name = _make_phony_error_name() name = _make_phony_error_name()
ast = ASTNamespace(name, None) 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: if not oldParent:
oldParent = env.domaindata['cpp']['root_symbol'] oldParent = self.env.domaindata['cpp']['root_symbol']
symbol = oldParent.add_name(ast.nestedName, ast.templatePrefix) 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) stack.append(symbol)
env.temp_data['cpp:parent_symbol'] = symbol self.env.temp_data['cpp:parent_symbol'] = symbol
env.temp_data['cpp:namespace_stack'] = stack self.env.temp_data['cpp:namespace_stack'] = stack
env.ref_context['cpp:parent_key'] = symbol.get_lookup_key() self.env.ref_context['cpp:parent_key'] = symbol.get_lookup_key()
return [] return []
class CPPNamespacePopObject(Directive): class CPPNamespacePopObject(SphinxDirective):
has_content = False has_content = False
required_arguments = 0 required_arguments = 0
optional_arguments = 0 optional_arguments = 0
@ -5917,8 +5916,7 @@ class CPPNamespacePopObject(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env stack = self.env.temp_data.get('cpp:namespace_stack', None)
stack = env.temp_data.get('cpp:namespace_stack', None)
if not stack or len(stack) == 0: if not stack or len(stack) == 0:
self.warn("C++ namespace pop on empty stack. Defaulting to gobal scope.") self.warn("C++ namespace pop on empty stack. Defaulting to gobal scope.")
stack = [] stack = []
@ -5927,10 +5925,10 @@ class CPPNamespacePopObject(Directive):
if len(stack) > 0: if len(stack) > 0:
symbol = stack[-1] symbol = stack[-1]
else: else:
symbol = env.domaindata['cpp']['root_symbol'] symbol = self.env.domaindata['cpp']['root_symbol']
env.temp_data['cpp:parent_symbol'] = symbol self.env.temp_data['cpp:parent_symbol'] = symbol
env.temp_data['cpp:namespace_stack'] = stack self.env.temp_data['cpp:namespace_stack'] = stack
env.ref_context['cpp:parent_key'] = symbol.get_lookup_key() self.env.ref_context['cpp:parent_key'] = symbol.get_lookup_key()
return [] return []

View File

@ -10,7 +10,7 @@
""" """
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive, directives from docutils.parsers.rst import directives
from sphinx import addnodes from sphinx import addnodes
from sphinx.directives import ObjectDescription from sphinx.directives import ObjectDescription
@ -19,6 +19,7 @@ from sphinx.domains.python import _pseudo_parse_arglist
from sphinx.locale import _ from sphinx.locale import _
from sphinx.roles import XRefRole from sphinx.roles import XRefRole
from sphinx.util.docfields import Field, GroupedField, TypedField from sphinx.util.docfields import Field, GroupedField, TypedField
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import make_refnode from sphinx.util.nodes import make_refnode
if False: if False:
@ -220,7 +221,7 @@ class JSConstructor(JSCallable):
allow_nesting = True allow_nesting = True
class JSModule(Directive): class JSModule(SphinxDirective):
""" """
Directive to mark description of a new JavaScript module. Directive to mark description of a new JavaScript module.
@ -249,16 +250,15 @@ class JSModule(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env
mod_name = self.arguments[0].strip() 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 noindex = 'noindex' in self.options
ret = [] ret = []
if not noindex: 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 # Make a duplicate entry in 'objects' to facilitate searching for
# the module in JavaScriptDomain.find_obj() # 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], targetnode = nodes.target('', '', ids=['module-' + mod_name],
ismod=True) ismod=True)
self.state.document.note_explicit_target(targetnode) self.state.document.note_explicit_target(targetnode)

View File

@ -12,7 +12,7 @@
import re import re
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive, directives from docutils.parsers.rst import directives
from six import iteritems from six import iteritems
from sphinx import addnodes, locale from sphinx import addnodes, locale
@ -23,6 +23,7 @@ from sphinx.locale import _, __
from sphinx.roles import XRefRole from sphinx.roles import XRefRole
from sphinx.util import logging from sphinx.util import logging
from sphinx.util.docfields import Field, GroupedField, TypedField from sphinx.util.docfields import Field, GroupedField, TypedField
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import make_refnode from sphinx.util.nodes import make_refnode
if False: if False:
@ -555,7 +556,7 @@ class PyDecoratorMethod(PyDecoratorMixin, PyClassmember):
return PyClassmember.run(self) return PyClassmember.run(self)
class PyModule(Directive): class PyModule(SphinxDirective):
""" """
Directive to mark description of a new module. Directive to mark description of a new module.
""" """
@ -573,19 +574,18 @@ class PyModule(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env
modname = self.arguments[0].strip() modname = self.arguments[0].strip()
noindex = 'noindex' in self.options noindex = 'noindex' in self.options
env.ref_context['py:module'] = modname self.env.ref_context['py:module'] = modname
ret = [] ret = []
if not noindex: if not noindex:
env.domaindata['py']['modules'][modname] = (env.docname, self.env.domaindata['py']['modules'][modname] = (self.env.docname,
self.options.get('synopsis', ''), self.options.get('synopsis', ''),
self.options.get('platform', ''), self.options.get('platform', ''),
'deprecated' in self.options) 'deprecated' in self.options)
# make a duplicate entry in 'objects' to facilitate searching for # make a duplicate entry in 'objects' to facilitate searching for
# the module in PythonDomain.find_obj() # 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], targetnode = nodes.target('', '', ids=['module-' + modname],
ismod=True) ismod=True)
self.state.document.note_explicit_target(targetnode) self.state.document.note_explicit_target(targetnode)
@ -599,7 +599,7 @@ class PyModule(Directive):
return ret return ret
class PyCurrentModule(Directive): class PyCurrentModule(SphinxDirective):
""" """
This directive is just to tell Sphinx that we're documenting This directive is just to tell Sphinx that we're documenting
stuff in module foo, but links to module foo won't lead here. stuff in module foo, but links to module foo won't lead here.
@ -613,12 +613,11 @@ class PyCurrentModule(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env
modname = self.arguments[0].strip() modname = self.arguments[0].strip()
if modname == 'None': if modname == 'None':
env.ref_context.pop('py:module', None) self.env.ref_context.pop('py:module', None)
else: else:
env.ref_context['py:module'] = modname self.env.ref_context['py:module'] = modname
return [] return []

View File

@ -15,7 +15,7 @@ import warnings
from copy import copy from copy import copy
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive, directives from docutils.parsers.rst import directives
from docutils.statemachine import ViewList from docutils.statemachine import ViewList
from six import iteritems from six import iteritems
@ -26,11 +26,13 @@ from sphinx.domains import Domain, ObjType
from sphinx.locale import _, __ from sphinx.locale import _, __
from sphinx.roles import XRefRole from sphinx.roles import XRefRole
from sphinx.util import ws_re, logging, docname_join from sphinx.util import ws_re, logging, docname_join
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import clean_astext, make_refnode from sphinx.util.nodes import clean_astext, make_refnode
if False: if False:
# For type annotation # For type annotation
from typing import Any, Callable, Dict, Iterator, List, Tuple, Type, Union # NOQA 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.application import Sphinx # NOQA
from sphinx.builders import Builder # NOQA from sphinx.builders import Builder # NOQA
from sphinx.environment import BuildEnvironment # NOQA from sphinx.environment import BuildEnvironment # NOQA
@ -107,7 +109,7 @@ class EnvVarXRefRole(XRefRole):
return [indexnode, targetnode, node], [] return [indexnode, targetnode, node], []
class Target(Directive): class Target(SphinxDirective):
""" """
Generic target for user-defined cross-reference types. Generic target for user-defined cross-reference types.
""" """
@ -121,7 +123,6 @@ class Target(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env
# normalize whitespace in fullname like XRefRole does # normalize whitespace in fullname like XRefRole does
fullname = ws_re.sub(' ', self.arguments[0].strip()) fullname = ws_re.sub(' ', self.arguments[0].strip())
targetname = '%s-%s' % (self.name, fullname) targetname = '%s-%s' % (self.name, fullname)
@ -141,8 +142,8 @@ class Target(Directive):
name = self.name name = self.name
if ':' in self.name: if ':' in self.name:
_, name = self.name.split(':', 1) _, name = self.name.split(':', 1)
env.domaindata['std']['objects'][name, fullname] = \ self.env.domaindata['std']['objects'][name, fullname] = \
env.docname, targetname self.env.docname, targetname
return ret return ret
@ -204,7 +205,7 @@ class Cmdoption(ObjectDescription):
signode['ids'][0], '', None)) signode['ids'][0], '', None))
class Program(Directive): class Program(SphinxDirective):
""" """
Directive to name the program for which options are documented. Directive to name the program for which options are documented.
""" """
@ -217,12 +218,11 @@ class Program(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env
program = ws_re.sub('-', self.arguments[0].strip()) program = ws_re.sub('-', self.arguments[0].strip())
if program == 'None': if program == 'None':
env.ref_context.pop('std:program', None) self.env.ref_context.pop('std:program', None)
else: else:
env.ref_context['std:program'] = program self.env.ref_context['std:program'] = program
return [] return []
@ -270,7 +270,7 @@ def make_glossary_term(env, textnodes, index_key, source, lineno, new_id=None):
return term return term
class Glossary(Directive): class Glossary(SphinxDirective):
""" """
Directive to create a glossary with cross-reference targets for :term: Directive to create a glossary with cross-reference targets for :term:
roles. roles.
@ -286,7 +286,6 @@ class Glossary(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env
node = addnodes.glossary() node = addnodes.glossary()
node.document = self.state.document node.document = self.state.document
@ -358,7 +357,7 @@ class Glossary(Directive):
textnodes, sysmsg = self.state.inline_text(parts[0], lineno) textnodes, sysmsg = self.state.inline_text(parts[0], lineno)
# use first classifier as a index key # 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 term.rawsource = line
system_messages.extend(sysmsg) system_messages.extend(sysmsg)
termtexts.append(term.astext()) termtexts.append(term.astext())
@ -403,7 +402,7 @@ def token_xrefs(text):
return retnodes return retnodes
class ProductionList(Directive): class ProductionList(SphinxDirective):
""" """
Directive to list grammar productions. Directive to list grammar productions.
""" """
@ -416,8 +415,7 @@ class ProductionList(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env objects = self.env.domaindata['std']['objects']
objects = env.domaindata['std']['objects']
node = addnodes.productionlist() node = addnodes.productionlist()
messages = [] # type: List[nodes.Node] messages = [] # type: List[nodes.Node]
i = 0 i = 0
@ -438,7 +436,7 @@ class ProductionList(Directive):
if idname not in self.state.document.ids: if idname not in self.state.document.ids:
subnode['ids'].append(idname) subnode['ids'].append(idname)
self.state.document.note_implicit_target(subnode, subnode) 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)) subnode.extend(token_xrefs(tokens))
node.append(subnode) node.append(subnode)
return [node] + messages return [node] + messages

View File

@ -8,13 +8,12 @@
""" """
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.statemachine import ViewList from docutils.statemachine import ViewList
from docutils.utils import assemble_option_dict from docutils.utils import assemble_option_dict
from sphinx.ext.autodoc import Options, get_documenters from sphinx.ext.autodoc import Options, get_documenters
from sphinx.util import logging 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 from sphinx.util.nodes import nested_parse_with_titles
if False: if False:
@ -91,7 +90,7 @@ def parse_generated_content(state, content, documenter):
return node.children return node.children
class AutodocDirective(Directive): class AutodocDirective(SphinxDirective):
"""A directive class for all autodoc directives. It works as a dispatcher of Documenters. """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 It invokes a Documenter on running. After the processing, it parses and returns
@ -105,7 +104,6 @@ class AutodocDirective(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
env = self.state.document.settings.env
reporter = self.state.document.reporter reporter = self.state.document.reporter
try: try:
@ -116,11 +114,11 @@ class AutodocDirective(Directive):
# look up target Documenter # look up target Documenter
objtype = self.name[4:] # strip prefix (auto-). 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 # process the options with the selected documenter's option_spec
try: 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: except (KeyError, ValueError, TypeError) as exc:
# an option is either unknown or has a wrong type # 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' % logger.error('An option to %s is either unknown or has an invalid value: %s' %
@ -128,7 +126,7 @@ class AutodocDirective(Directive):
return [] return []
# generate the output # 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 = doccls(params, self.arguments[0])
documenter.generate(more_content=self.content) documenter.generate(more_content=self.content)
if not params.result: if not params.result:

View File

@ -62,7 +62,7 @@ import warnings
from types import ModuleType from types import ModuleType
from docutils import nodes 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.parsers.rst.states import RSTStateMachine, state_classes
from docutils.statemachine import ViewList from docutils.statemachine import ViewList
from six import string_types from six import string_types
@ -78,7 +78,7 @@ from sphinx.ext.autodoc.importer import import_module
from sphinx.locale import __ from sphinx.locale import __
from sphinx.pycode import ModuleAnalyzer, PycodeError from sphinx.pycode import ModuleAnalyzer, PycodeError
from sphinx.util import import_object, rst, logging 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: if False:
# For type annotation # For type annotation
@ -220,7 +220,7 @@ def get_documenter(*args):
# -- .. autosummary:: ---------------------------------------------------------- # -- .. autosummary:: ----------------------------------------------------------
class Autosummary(Directive): class Autosummary(SphinxDirective):
""" """
Pretty table containing short signatures and summaries of functions etc. Pretty table containing short signatures and summaries of functions etc.
@ -244,7 +244,6 @@ class Autosummary(Directive):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
self.env = env = self.state.document.settings.env
self.genopt = Options() self.genopt = Options()
self.warnings = [] # type: List[nodes.Node] self.warnings = [] # type: List[nodes.Node]
self.result = ViewList() self.result = ViewList()
@ -255,14 +254,14 @@ class Autosummary(Directive):
nodes = self.get_table(items) nodes = self.get_table(items)
if 'toctree' in self.options: if 'toctree' in self.options:
dirname = posixpath.dirname(env.docname) dirname = posixpath.dirname(self.env.docname)
tree_prefix = self.options['toctree'].strip() tree_prefix = self.options['toctree'].strip()
docnames = [] docnames = []
for name, sig, summary, real_name in items: for name, sig, summary, real_name in items:
docname = posixpath.join(tree_prefix, real_name) docname = posixpath.join(tree_prefix, real_name)
docname = posixpath.normpath(posixpath.join(dirname, docname)) 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' self.warn('toctree references unknown document %r'
% docname) % docname)
docnames.append(docname) docnames.append(docname)
@ -283,9 +282,7 @@ class Autosummary(Directive):
"""Try to import the given names, and return a list of """Try to import the given names, and return a list of
``[(name, signature, summary_string, real_name), ...]``. ``[(name, signature, summary_string, real_name), ...]``.
""" """
env = self.state.document.settings.env prefixes = get_import_prefixes_from_env(self.env)
prefixes = get_import_prefixes_from_env(env)
items = [] # type: List[Tuple[unicode, unicode, unicode, unicode]] items = [] # type: List[Tuple[unicode, unicode, unicode, unicode]]

View File

@ -19,7 +19,7 @@ import time
from os import path from os import path
from docutils import nodes 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.specifiers import SpecifierSet, InvalidSpecifier
from packaging.version import Version from packaging.version import Version
from six import itervalues, StringIO, binary_type, text_type, PY2 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.locale import __
from sphinx.util import force_decode, logging from sphinx.util import force_decode, logging
from sphinx.util.console import bold # type: ignore 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.nodes import set_source_info
from sphinx.util.osutil import fs_encoding, relpath from sphinx.util.osutil import fs_encoding, relpath
@ -77,7 +78,7 @@ def is_allowed_version(spec, version):
# set up the necessary directives # set up the necessary directives
class TestDirective(Directive): class TestDirective(SphinxDirective):
""" """
Base class for doctest-related directives. Base class for doctest-related directives.
""" """

View File

@ -18,7 +18,7 @@ from os import path
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive, directives from docutils.parsers.rst import directives
from docutils.statemachine import ViewList from docutils.statemachine import ViewList
from six import text_type from six import text_type
@ -26,11 +26,13 @@ import sphinx
from sphinx.errors import SphinxError from sphinx.errors import SphinxError
from sphinx.locale import _, __ from sphinx.locale import _, __
from sphinx.util import logging from sphinx.util import logging
from sphinx.util.docutils import SphinxDirective
from sphinx.util.i18n import search_image_for_language from sphinx.util.i18n import search_image_for_language
from sphinx.util.osutil import ensuredir, ENOENT, EPIPE, EINVAL from sphinx.util.osutil import ensuredir, ENOENT, EPIPE, EINVAL
if False: if False:
# For type annotation # For type annotation
from docutils.parsers.rst import Directive # NOQA
from typing import Any, Dict, List, Tuple # NOQA from typing import Any, Dict, List, Tuple # NOQA
from sphinx.application import Sphinx # NOQA from sphinx.application import Sphinx # NOQA
@ -111,7 +113,7 @@ def align_spec(argument):
return directives.choice(argument, ('left', 'center', 'right')) return directives.choice(argument, ('left', 'center', 'right'))
class Graphviz(Directive): class Graphviz(SphinxDirective):
""" """
Directive to insert arbitrary dot markup. Directive to insert arbitrary dot markup.
""" """
@ -135,12 +137,11 @@ class Graphviz(Directive):
return [document.reporter.warning( return [document.reporter.warning(
__('Graphviz directive cannot have both content and ' __('Graphviz directive cannot have both content and '
'a filename argument'), line=self.lineno)] 'a filename argument'), line=self.lineno)]
env = self.state.document.settings.env argument = search_image_for_language(self.arguments[0], self.env)
argument = search_image_for_language(self.arguments[0], env) rel_filename, filename = self.env.relfn2path(argument)
rel_filename, filename = env.relfn2path(argument) self.env.note_dependency(rel_filename)
env.note_dependency(rel_filename)
try: try:
with codecs.open(filename, 'r', 'utf-8') as fp: with codecs.open(filename, 'r', 'utf-8') as fp: # type: ignore
dotcode = fp.read() dotcode = fp.read()
except (IOError, OSError): except (IOError, OSError):
return [document.reporter.warning( return [document.reporter.warning(
@ -170,7 +171,7 @@ class Graphviz(Directive):
return [node] return [node]
class GraphvizSimple(Directive): class GraphvizSimple(SphinxDirective):
""" """
Directive to insert arbitrary dot markup. Directive to insert arbitrary dot markup.
""" """

View File

@ -21,9 +21,9 @@
""" """
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive
import sphinx import sphinx
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import set_source_info from sphinx.util.nodes import set_source_info
if False: if False:
@ -36,7 +36,7 @@ class ifconfig(nodes.Element):
pass pass
class IfConfig(Directive): class IfConfig(SphinxDirective):
has_content = True has_content = True
required_arguments = 1 required_arguments = 1

View File

@ -42,7 +42,7 @@ import sys
from hashlib import md5 from hashlib import md5
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive, directives from docutils.parsers.rst import directives
from six import text_type from six import text_type
from six.moves import builtins 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 render_dot_texinfo, figure_wrapper
from sphinx.pycode import ModuleAnalyzer from sphinx.pycode import ModuleAnalyzer
from sphinx.util import force_decode from sphinx.util import force_decode
from sphinx.util.docutils import SphinxDirective
if False: if False:
# For type annotation # For type annotation
@ -322,7 +323,7 @@ class inheritance_diagram(nodes.General, nodes.Element):
pass pass
class InheritanceDiagram(Directive): class InheritanceDiagram(SphinxDirective):
""" """
Run when the inheritance_diagram directive is first encountered. Run when the inheritance_diagram directive is first encountered.
""" """
@ -341,9 +342,8 @@ class InheritanceDiagram(Directive):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
node = inheritance_diagram() node = inheritance_diagram()
node.document = self.state.document node.document = self.state.document
env = self.state.document.settings.env
class_names = self.arguments[0].split() 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 # Store the original content for use as a hash
node['parts'] = self.options.get('parts', 0) node['parts'] = self.options.get('parts', 0)
node['content'] = ', '.join(class_names) node['content'] = ', '.join(class_names)
@ -356,10 +356,10 @@ class InheritanceDiagram(Directive):
# Create a graph starting with the list of classes # Create a graph starting with the list of classes
try: try:
graph = InheritanceGraph( graph = InheritanceGraph(
class_names, env.ref_context.get('py:module'), class_names, self.env.ref_context.get('py:module'),
parts=node['parts'], parts=node['parts'],
private_bases='private-bases' in self.options, private_bases='private-bases' in self.options,
aliases=env.config.inheritance_alias, aliases=self.config.inheritance_alias,
top_classes=node['top-classes']) top_classes=node['top-classes'])
except InheritanceException as err: except InheritanceException as err:
return [node.document.reporter.warning(err.args[0], return [node.document.reporter.warning(err.args[0],

View File

@ -11,13 +11,14 @@
from docutils import nodes, utils from docutils import nodes, utils
from docutils.nodes import make_id 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.config import string_classes
from sphinx.domains import Domain from sphinx.domains import Domain
from sphinx.locale import __ from sphinx.locale import __
from sphinx.roles import XRefRole from sphinx.roles import XRefRole
from sphinx.util import logging from sphinx.util import logging
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import make_refnode, set_source_info from sphinx.util.nodes import make_refnode, set_source_info
if False: if False:
@ -212,7 +213,7 @@ def is_in_section_title(node):
return False return False
class MathDirective(Directive): class MathDirective(SphinxDirective):
has_content = True has_content = True
required_arguments = 0 required_arguments = 0
@ -238,7 +239,7 @@ class MathDirective(Directive):
if 'label' in self.options: if 'label' in self.options:
node['label'] = self.options['label'] node['label'] = self.options['label']
node['nowrap'] = 'nowrap' in self.options node['nowrap'] = 'nowrap' in self.options
node['docname'] = self.state.document.settings.env.docname node['docname'] = self.env.docname
ret = [node] ret = [node]
set_source_info(self, node) set_source_info(self, node)
if hasattr(self, 'src'): if hasattr(self, 'src'):
@ -249,21 +250,20 @@ class MathDirective(Directive):
def add_target(self, ret): def add_target(self, ret):
# type: (List[nodes.Node]) -> None # type: (List[nodes.Node]) -> None
node = ret[0] node = ret[0]
env = self.state.document.settings.env
# assign label automatically if math_number_all enabled # assign label automatically if math_number_all enabled
if node['label'] == '' or (env.config.math_number_all and not node['label']): if node['label'] == '' or (self.config.math_number_all and not node['label']):
seq = env.new_serialno('sphinx.ext.math#equations') seq = self.env.new_serialno('sphinx.ext.math#equations')
node['label'] = "%s:%d" % (env.docname, seq) node['label'] = "%s:%d" % (self.env.docname, seq)
# no targets and numbers are needed # no targets and numbers are needed
if not node['label']: if not node['label']:
return return
# register label to domain # register label to domain
domain = env.get_domain('math') domain = self.env.get_domain('math')
try: 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 node['number'] = eqno
# add target node # add target node

View File

@ -13,7 +13,6 @@
""" """
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.parsers.rst import directives from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.admonitions import BaseAdmonition from docutils.parsers.rst.directives.admonitions import BaseAdmonition
@ -21,6 +20,7 @@ import sphinx
from sphinx.environment import NoUri from sphinx.environment import NoUri
from sphinx.locale import _, __ from sphinx.locale import _, __
from sphinx.util import logging from sphinx.util import logging
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import set_source_info from sphinx.util.nodes import set_source_info
from sphinx.util.texescape import tex_escape_map from sphinx.util.texescape import tex_escape_map
@ -41,7 +41,7 @@ class todolist(nodes.General, nodes.Element):
pass pass
class Todo(BaseAdmonition): class Todo(BaseAdmonition, SphinxDirective):
""" """
A todo entry, displayed (if configured) in the form of an admonition. 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'))) todo.insert(0, nodes.title(text=_('Todo')))
set_source_info(self, todo) set_source_info(self, todo)
env = self.state.document.settings.env targetid = 'index-%s' % self.env.new_serialno('index')
targetid = 'index-%s' % env.new_serialno('index')
# Stash the target to be retrieved later in latex_visit_todo_node. # 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]) targetnode = nodes.target('', '', ids=[targetid])
return [targetnode, todo] return [targetnode, todo]
@ -107,7 +106,7 @@ def process_todos(app, doctree):
location=node) location=node)
class TodoList(Directive): class TodoList(SphinxDirective):
""" """
A list of all todo entries. A list of all todo entries.
""" """