Fix #4563: autosummary: Incorrect end of line punctuation detection

This commit is contained in:
Takeshi KOMIYA 2018-02-18 12:09:24 +09:00
parent 20613edc03
commit d40db5efa2
3 changed files with 34 additions and 10 deletions

View File

@ -32,6 +32,7 @@ Bugs fixed
* autosummary: The interface of ``sphinx.ext.autosummary.get_documenter()`` has * autosummary: The interface of ``sphinx.ext.autosummary.get_documenter()`` has
been changed been changed
* #4630: Have order on msgids in sphinx.pot deterministic * #4630: Have order on msgids in sphinx.pot deterministic
* #4563: autosummary: Incorrect end of line punctuation detection
Testing Testing
-------- --------

View File

@ -64,6 +64,7 @@ from typing import TYPE_CHECKING
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import Directive, directives from docutils.parsers.rst import Directive, directives
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
from six import text_type from six import text_type
@ -77,6 +78,7 @@ from sphinx.ext.autodoc.directive import DocumenterBridge, Options
from sphinx.ext.autodoc.importer import import_module from sphinx.ext.autodoc.importer import import_module
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 new_document
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Any, Dict, List, Tuple, Type, Union # NOQA from typing import Any, Dict, List, Tuple, Type, Union # NOQA
@ -340,7 +342,7 @@ class Autosummary(Directive):
# -- Grab the summary # -- Grab the summary
documenter.add_content(None) documenter.add_content(None)
summary = extract_summary(self.result.data[:]) summary = extract_summary(self.result.data[:], self.state.document)
items.append((display_name, sig, summary, real_name)) items.append((display_name, sig, summary, real_name))
@ -447,8 +449,8 @@ def mangle_signature(sig, max_chars=30):
return u"(%s)" % sig return u"(%s)" % sig
def extract_summary(doc): def extract_summary(doc, document):
# type: (List[unicode]) -> unicode # type: (List[unicode], Any) -> unicode
"""Extract summary from docstring.""" """Extract summary from docstring."""
# Skip a blank lines at the top # Skip a blank lines at the top
@ -464,13 +466,19 @@ def extract_summary(doc):
break break
# Try to find the "first sentence", which may span multiple lines # Try to find the "first sentence", which may span multiple lines
m = re.search(r"^([A-Z].*?\.)(?:\s|$)", " ".join(doc).strip()) sentences = " ".join(doc).split('.')
if m: if len(sentences) == 1:
summary = m.group(1).strip() summary = sentences[0].strip()
elif doc:
summary = doc[0].strip()
else: else:
summary = '' summary = ''
state_machine = RSTStateMachine(state_classes, 'Body')
while sentences:
summary += sentences.pop(0) + '.'
node = new_document('', document.settings)
state_machine.run([summary], node)
if not node.traverse(nodes.system_message):
# considered as that splitting by period does not break inline markups
break
return summary return summary

View File

@ -56,11 +56,26 @@ def test_mangle_signature():
def test_extract_summary(): def test_extract_summary():
from sphinx.util.docutils import new_document
from mock import Mock
settings = Mock(language_code='',
id_prefix='',
auto_id_prefix='',
pep_reference=False,
rfc_reference=False)
document = new_document('', settings)
# normal case
doc = ['', doc = ['',
'This is a first sentence. And second one.', 'This is a first sentence. And second one.',
'', '',
'Second block is here'] 'Second block is here']
assert extract_summary(doc) == 'This is a first sentence.' assert extract_summary(doc, document) == 'This is a first sentence.'
# inliner case
doc = ['This sentence contains *emphasis text having dots.*,',
'it does not break sentence.']
assert extract_summary(doc, document) == ' '.join(doc)
@pytest.mark.sphinx('dummy', **default_kw) @pytest.mark.sphinx('dummy', **default_kw)
@ -101,7 +116,7 @@ def test_get_items_summary(make_app, app_params):
expected_values = { expected_values = {
'withSentence': 'I have a sentence which spans multiple lines.', 'withSentence': 'I have a sentence which spans multiple lines.',
'noSentence': "this doesn't start with a", 'noSentence': "this doesn't start with a capital.",
'emptyLine': "This is the real summary", 'emptyLine': "This is the real summary",
'module_attr': 'This is a module attribute', 'module_attr': 'This is a module attribute',
'C.class_attr': 'This is a class attribute', 'C.class_attr': 'This is a class attribute',