Merge branch 'master' into refactor_SphinxTranslator

This commit is contained in:
Takeshi KOMIYA authored and GitHub committed 2018-12-03 23:16:03 +09:00
commit b3d71ca683
12 files changed
+53 -44

No files matched your search

+1 -1
View File
@@ -591,7 +591,7 @@ class Sphinx:
@property
def enumerable_nodes(self):
# type: () -> Dict[Type[nodes.Element], Tuple[unicode, TitleGetter]]
# type: () -> Dict[Type[nodes.Node], Tuple[unicode, TitleGetter]]
warnings.warn('app.enumerable_nodes() is deprecated. '
'Use app.get_domain("std").enumerable_nodes instead.',
RemovedInSphinx30Warning, stacklevel=2)
+2 -2
View File
@@ -13,7 +13,7 @@ from difflib import unified_diff
from docutils import nodes
from docutils.parsers.rst import directives
from docutils.statemachine import ViewList
from docutils.statemachine import StringList
from six import text_type
from sphinx import addnodes
@@ -89,7 +89,7 @@ def container_wrapper(directive, literal_node, caption):
container_node = nodes.container('', literal_block=True,
classes=['literal-block-wrapper'])
parsed = nodes.Element()
directive.state.nested_parse(ViewList([caption], source=''),
directive.state.nested_parse(StringList([caption], source=''),
directive.content_offset, parsed)
if isinstance(parsed[0], nodes.system_message):
msg = __('Invalid caption: %s' % parsed[0].astext())
+2 -2
View File
@@ -160,7 +160,7 @@ class Domain:
#: role name -> a warning message if reference is missing
dangling_warnings = {} # type: Dict[unicode, unicode]
#: node_class -> (enum_node_type, title_getter)
enumerable_nodes = {} # type: Dict[Type[nodes.Element], Tuple[unicode, Callable]]
enumerable_nodes = {} # type: Dict[Type[nodes.Node], Tuple[unicode, Callable]]
#: data value for a fresh environment
initial_data = {} # type: Dict
@@ -345,7 +345,7 @@ class Domain:
return _('%s %s') % (self.label, type.lname)
def get_enumerable_node_type(self, node):
# type: (nodes.Element) -> unicode
# type: (nodes.Node) -> unicode
"""Get type of enumerable nodes (experimental)."""
enum_node_type, _ = self.enumerable_nodes.get(node.__class__, (None, None))
return enum_node_type
+4 -4
View File
@@ -525,7 +525,7 @@ class StandardDomain(Domain):
nodes.figure: ('figure', None),
nodes.table: ('table', None),
nodes.container: ('code-block', None),
} # type: Dict[Type[nodes.Element], Tuple[unicode, Callable]]
} # type: Dict[Type[nodes.Node], Tuple[unicode, Callable]]
def __init__(self, env):
# type: (BuildEnvironment) -> None
@@ -927,7 +927,7 @@ class StandardDomain(Domain):
return node.__class__ in self.enumerable_nodes
def get_numfig_title(self, node):
# type: (nodes.Element) -> unicode
# type: (nodes.Node) -> unicode
"""Get the title of enumerable nodes to refer them using its title"""
if self.is_enumerable_node(node):
_, title_getter = self.enumerable_nodes.get(node.__class__, (None, None))
@@ -941,7 +941,7 @@ class StandardDomain(Domain):
return None
def get_enumerable_node_type(self, node):
# type: (nodes.Element) -> unicode
# type: (nodes.Node) -> unicode
"""Get type of enumerable nodes."""
def has_child(node, cls):
# type: (nodes.Element, Type) -> bool
@@ -959,7 +959,7 @@ class StandardDomain(Domain):
return figtype
def get_figtype(self, node):
# type: (nodes.Element) -> unicode
# type: (nodes.Node) -> unicode
"""Get figure type of nodes.
.. deprecated:: 1.8
+1 -1
View File
@@ -231,7 +231,7 @@ class TocTreeCollector(EnvironmentCollector):
fignum_counter = {} # type: Dict[unicode, Dict[Tuple[int, ...], int]]
def get_figtype(node):
# type: (nodes.Element) -> unicode
# type: (nodes.Node) -> unicode
for domain in env.domains.values():
figtype = domain.get_enumerable_node_type(node)
if figtype:
+2 -3
View File
@@ -17,7 +17,7 @@ import sys
import warnings
from typing import Any
from docutils.statemachine import ViewList
from docutils.statemachine import StringList
from six import text_type
import sphinx
@@ -1177,8 +1177,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
parentmodule = safe_getattr(self.parent, '__module__', None)
if module and module != parentmodule:
classname = str(module) + u'.' + str(classname)
content = ViewList(
[_('alias of :class:`%s`') % classname], source='')
content = StringList([_('alias of :class:`%s`') % classname], source='')
super(ClassDocumenter, self).add_content(content, no_docstring=True)
else:
super(ClassDocumenter, self).add_content(more_content)
+4 -4
View File
@@ -64,7 +64,7 @@ from typing import List, cast
from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst.states import RSTStateMachine, state_classes
from docutils.statemachine import ViewList
from docutils.statemachine import StringList
from six import text_type
import sphinx
@@ -243,7 +243,7 @@ class Autosummary(SphinxDirective):
# type: () -> List[nodes.Node]
self.genopt = Options()
self.warnings = [] # type: List[nodes.Node]
self.result = ViewList()
self.result = StringList()
names = [x.strip().split()[0] for x in self.content
if x.strip() and re.search(r'^[~a-zA-Z_]', x.strip()[0])]
@@ -302,7 +302,7 @@ class Autosummary(SphinxDirective):
items.append((name, '', '', name))
continue
self.result = ViewList() # initialize for each documenter
self.result = StringList() # initialize for each documenter
full_name = real_name
if not isinstance(obj, ModuleType):
# give explicitly separated module name, so that members
@@ -377,7 +377,7 @@ class Autosummary(SphinxDirective):
source, line = self.state_machine.get_source_and_line()
for text in column_texts:
node = nodes.paragraph('')
vl = ViewList()
vl = StringList()
vl.append(text, '%s:%d:<autosummary>' % (source, line))
with switch_source_input(self.state, vl):
self.state.nested_parse(vl, 0, node)
+2 -2
View File
@@ -90,7 +90,7 @@ class SphinxComponentRegistry:
#: additional enumerable nodes
#: a dict of node class -> tuple of figtype and title_getter function
self.enumerable_nodes = {} # type: Dict[Type[nodes.Element], Tuple[unicode, TitleGetter]] # NOQA
self.enumerable_nodes = {} # type: Dict[Type[nodes.Node], Tuple[unicode, TitleGetter]] # NOQA
#: HTML inline and block math renderers
#: a dict of name -> tuple of visit function and depart function
@@ -438,7 +438,7 @@ class SphinxComponentRegistry:
self.latex_packages.append((name, options))
def add_enumerable_node(self, node, figtype, title_getter=None, override=False):
# type: (Type[nodes.Element], unicode, TitleGetter, bool) -> None
# type: (Type[nodes.Node], unicode, TitleGetter, bool) -> None
logger.debug('[app] adding enumerable node: (%r, %r, %r)', node, figtype, title_getter)
if node in self.enumerable_nodes and not override:
raise ExtensionError(__('enumerable_node %r already registered') % node)
+13 -4
View File
@@ -65,10 +65,14 @@ def assert_node(node, cls=None, xpath="", **kwargs):
if isinstance(cls[1], tuple):
assert_node(node, cls[1], xpath=xpath, **kwargs)
else:
assert isinstance(node, nodes.Element), \
'The node%s does not have any children' % xpath
assert len(node) == 1, \
'The node%s has %d child nodes, not one' % (xpath, len(node))
assert_node(node[0], cls[1:], xpath=xpath + "[0]", **kwargs)
elif isinstance(cls, tuple):
assert isinstance(node, nodes.Element), \
'The node%s does not have any items' % xpath
assert len(node) == len(cls), \
'The node%s has %d child nodes, not %r' % (xpath, len(node), len(cls))
for i, nodecls in enumerate(cls):
@@ -80,10 +84,15 @@ def assert_node(node, cls=None, xpath="", **kwargs):
assert isinstance(node, cls), \
'The node%s is not subclass of %r: %r' % (xpath, cls, node)
for key, value in kwargs.items():
assert key in node, 'The node%s does not have %r attribute: %r' % (xpath, key, node)
assert node[key] == value, \
'The node%s[%s] is not %r: %r' % (xpath, key, value, node[key])
if kwargs:
assert isinstance(node, nodes.Element), \
'The node%s does not have any attributes' % xpath
for key, value in kwargs.items():
assert key in node, \
'The node%s does not have %r attribute: %r' % (xpath, key, node)
assert node[key] == value, \
'The node%s[%s] is not %r: %r' % (xpath, key, value, node[key])
def etree_parse(path):
+2 -2
View File
@@ -38,7 +38,7 @@ if False:
# For type annotation
from types import ModuleType # NOQA
from typing import Any, Callable, Generator, List, Set, Tuple, Type # NOQA
from docutils.statemachine import State, ViewList # NOQA
from docutils.statemachine import State, StringList # NOQA
from sphinx.builders import Builder # NOQA
from sphinx.config import Config # NOQA
from sphinx.environment import BuildEnvironment # NOQA
@@ -325,7 +325,7 @@ def directive_helper(obj, has_content=None, argument_spec=None, **option_spec):
@contextmanager
def switch_source_input(state, content):
# type: (State, ViewList) -> Generator[None, None, None]
# type: (State, StringList) -> Generator[None, None, None]
"""Switch current source input of state temporarily."""
try:
# remember the original ``get_source_and_line()`` method
+2 -1
View File
@@ -26,6 +26,7 @@ from six import text_type
from sphinx import addnodes
from sphinx import highlighting
from sphinx.deprecation import RemovedInSphinx30Warning, RemovedInSphinx40Warning
from sphinx.domains.std import StandardDomain
from sphinx.errors import SphinxError
from sphinx.locale import admonitionlabels, _, __
from sphinx.util import split_into, logging
@@ -1877,7 +1878,7 @@ class LaTeXTranslator(SphinxTranslator):
while isinstance(next_node, nodes.target):
next_node = next_node.next_node(ascend=True)
domain = self.builder.env.get_domain('std')
domain = cast(StandardDomain, self.builder.env.get_domain('std'))
if isinstance(next_node, HYPERLINK_SUPPORT_NODES):
return
elif domain.get_enumerable_node_type(next_node) and domain.get_numfig_title(next_node):
+18 -18
View File
@@ -20,62 +20,62 @@ def test_samp():
# normal case
text = 'print 1+{variable}'
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
assert_node(ret, ([nodes.literal, ("print 1+",
[nodes.emphasis, "variable"])],))
assert_node(ret[0], [nodes.literal, ("print 1+",
[nodes.emphasis, "variable"])])
assert msg == []
# two emphasis items
text = 'print {1}+{variable}'
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
assert_node(ret, ([nodes.literal, ("print ",
[nodes.emphasis, "1"],
"+",
[nodes.emphasis, "variable"])],))
assert_node(ret[0], [nodes.literal, ("print ",
[nodes.emphasis, "1"],
"+",
[nodes.emphasis, "variable"])])
assert msg == []
# empty curly brace
text = 'print 1+{}'
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
assert_node(ret, ([nodes.literal, "print 1+{}"],))
assert_node(ret[0], [nodes.literal, "print 1+{}"])
assert msg == []
# half-opened variable
text = 'print 1+{variable'
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
assert_node(ret, ([nodes.literal, "print 1+{variable"],))
assert_node(ret[0], [nodes.literal, "print 1+{variable"])
assert msg == []
# nested
text = 'print 1+{{variable}}'
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
assert_node(ret, ([nodes.literal, ("print 1+",
[nodes.emphasis, "{variable"],
"}")],))
assert_node(ret[0], [nodes.literal, ("print 1+",
[nodes.emphasis, "{variable"],
"}")])
assert msg == []
# emphasized item only
text = '{variable}'
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
assert_node(ret, ([nodes.literal, nodes.emphasis, "variable"],))
assert_node(ret[0], [nodes.literal, nodes.emphasis, "variable"])
assert msg == []
# escaping
text = r'print 1+\{variable}'
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
assert_node(ret, ([nodes.literal, "print 1+{variable}"],))
assert_node(ret[0], [nodes.literal, "print 1+{variable}"])
assert msg == []
# escaping (2)
text = r'print 1+\{{variable}\}'
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
assert_node(ret, ([nodes.literal, ("print 1+{",
[nodes.emphasis, "variable"],
"}")],))
assert_node(ret[0], [nodes.literal, ("print 1+{",
[nodes.emphasis, "variable"],
"}")])
assert msg == []
# escape a backslash
text = r'print 1+\\{variable}'
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
assert_node(ret, ([nodes.literal, ("print 1+\\",
[nodes.emphasis, "variable"])],))
assert_node(ret[0], [nodes.literal, ("print 1+\\",
[nodes.emphasis, "variable"])])
assert msg == []