Skip to content

Commit 2318f9a

Browse files
committed
Support indented arg descriptions
1 parent a2d9a88 commit 2318f9a

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

docstring_to_markdown/google.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22
from textwrap import dedent
3-
from typing import Dict, List, Union
3+
from typing import List
44

55
# All possible sections in Google style docstrings
66
SECTION_HEADERS: List[str] = [
@@ -68,6 +68,7 @@ def _parse(self, content: str) -> None:
6868
# Format section
6969
for part in parts:
7070
indentation = ""
71+
skip_first = False
7172

7273
if ":" in part[0]:
7374
spl = part[0].split(":")
@@ -76,11 +77,21 @@ def _parse(self, content: str) -> None:
7677
description = ":".join(spl[1:]).lstrip()
7778
indentation = (len(arg) + 6) * " "
7879

79-
self.content += "- `{}`: {}\n".format(arg, description)
80+
if description:
81+
self.content += "- `{}`: {}\n".format(arg, description)
82+
else:
83+
skip_first = True
84+
self.content += "- `{}`: ".format(arg)
8085
else:
8186
self.content += "- {}\n".format(part[0])
8287

83-
for line in part[1:]:
88+
for n, line in enumerate(part[1:]):
89+
if skip_first and n == 0:
90+
# This ensures that indented args get moved to the
91+
# previous line
92+
self.content += "{}\n".format(line.lstrip())
93+
continue
94+
8495
self.content += "{}{}\n".format(indentation, line.lstrip())
8596

8697
self.content = self.content.rstrip("\n")
@@ -89,7 +100,6 @@ def as_markdown(self) -> str:
89100
return "#### {}\n\n{}\n\n".format(self.name, self.content)
90101

91102

92-
93103
class GoogleDocstring:
94104
def __init__(self, docstring: str) -> None:
95105
self.sections: list[Section] = []

tests/test_google.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
spanning over several lines
7676
also with broken indentation
7777
b (str): Second arg
78+
c (str):
79+
On the next line
80+
And also multiple lines
7881
"""
7982

8083
MULTILINE_ARG_DESCRIPTION_MD = """Example.
@@ -85,6 +88,8 @@
8588
spanning over several lines
8689
also with broken indentation
8790
- `b (str)`: Second arg
91+
- `c (str)`: On the next line
92+
And also multiple lines
8893
"""
8994

9095
GOOGLE_CASES = {

0 commit comments

Comments
 (0)