diff --git a/docstring_parser/common.py b/docstring_parser/common.py index 880e378..6c596f5 100644 --- a/docstring_parser/common.py +++ b/docstring_parser/common.py @@ -51,7 +51,7 @@ class DocstringMeta: """ def __init__( - self, args: T.List[str], description: T.Optional[str] + self, args: T.List[str], description: T.Optional[str], ) -> None: """Initialize self. @@ -140,11 +140,13 @@ def __init__( args: T.List[str], snippet: T.Optional[str], description: T.Optional[str], + post_snippet: T.Optional[str] = None, ) -> None: """Initialize self.""" super().__init__(args, description) self.snippet = snippet self.description = description + self.post_snippet = post_snippet class Docstring: diff --git a/docstring_parser/numpydoc.py b/docstring_parser/numpydoc.py index eca5233..f76e26a 100644 --- a/docstring_parser/numpydoc.py +++ b/docstring_parser/numpydoc.py @@ -238,28 +238,52 @@ class ExamplesSection(Section): [ 6586976, 22740995]]) """ - def parse(self, text: str) -> T.Iterable[DocstringMeta]: + def parse(self, text: str) -> T.Iterable[DocstringExample]: """Parse ``DocstringExample`` objects from the body of this section. :param text: section body text. Should be cleaned with ``inspect.cleandoc`` before parsing. """ - lines = dedent(text).strip().splitlines() + lines = [x.rstrip() for x in dedent(text).strip().splitlines()] while lines: snippet_lines = [] description_lines = [] + post_snippet_lines = [] + + # Parse description of snippet + while lines: + if lines[0].startswith(">>>"): + break + description_lines.append(lines.pop(0)) + + # Parse code of snippet while lines: - if not lines[0].startswith(">>>"): + if not lines[0].startswith(">>>") and not lines[0].startswith('...'): break snippet_lines.append(lines.pop(0)) + + # Parse output of snippet while lines: - if lines[0].startswith(">>>"): + # Bail out at blank lines + if not lines[0]: + lines.pop(0) break - description_lines.append(lines.pop(0)) + # Bail out if a new snippet is started + elif lines[0].startswith(">>>"): + break + else: + snippet_lines.append(lines.pop(0)) + + # if there is following text, but no more snippets, make this a post description. + if not [x for x in lines if '>>>' in x]: + post_snippet_lines.extend(lines) + lines = [] + yield DocstringExample( [self.key], - snippet="\n".join(snippet_lines) if snippet_lines else None, - description="\n".join(description_lines), + snippet="\n".join(snippet_lines).strip() if snippet_lines else None, + description="\n".join(description_lines).strip(), + post_snippet="\n".join(post_snippet_lines).strip(), )