Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions docstring_to_markdown/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def _split(self, line: str) -> List[str]:
self._columns_end
)
fragment = line[start:end].strip()
fragment = rst_to_markdown(fragment, extract_signature=False)
self._max_sizes[i] = max(self._max_sizes[i], len(fragment))
fragments.append(fragment)
return fragments
Expand Down Expand Up @@ -647,7 +648,7 @@ def initiate_parsing(self, line: str, current_language: str) -> IBlockBeginning:
]


def rst_to_markdown(text: str) -> str:
def rst_to_markdown(text: str, extract_signature: bool = True) -> str:
"""
Try to parse docstrings in following formats to markdown:
- https://www.python.org/dev/peps/pep-0287/
Expand Down Expand Up @@ -693,10 +694,12 @@ def flush_buffer():

for line in text.split('\n'):
if is_first_line:
signature_match = re.match(r'^(?P<name>\S+)\((?P<params>.*)\)$', line)
if signature_match and signature_match.group('name').isidentifier():
markdown += '```python\n' + line + '\n```\n'
continue
if extract_signature:
signature_match = re.match(r'^(?P<name>\S+)\((?P<params>.*)\)$', line)
if signature_match and signature_match.group('name').isidentifier():
markdown += '```python\n' + line + '\n```\n'
continue
is_first_line = False

trimmed_line = line.lstrip()

Expand Down
22 changes: 22 additions & 0 deletions tests/test_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,24 @@ def func(): pass
"""


SIMPLE_TABLE_WITH_MARKUP = """
============================== =======================================
Scalar Type Array Type
============================== =======================================
:class:`pandas.Interval` :class:`pandas.arrays.IntervalArray`
:class:`bool` :class:`pandas.arrays.BooleanArray`
============================== =======================================
"""


SIMPLE_TABLE_WITH_MARKUP_MARKDOWN = """
| Scalar Type | Array Type |
| ----------------- | ----------------------------- |
| `pandas.Interval` | `pandas.arrays.IntervalArray` |
| `bool` | `pandas.arrays.BooleanArray` |
"""


SIMPLE_TABLE_2 = """
.. warning:: This is a standard simple table

Expand Down Expand Up @@ -782,6 +800,10 @@ def foo():
'rst': SIMPLE_TABLE,
'md': SIMPLE_TABLE_MARKDOWN
},
'converts syntax within table': {
'rst': SIMPLE_TABLE_WITH_MARKUP,
'md': SIMPLE_TABLE_WITH_MARKUP_MARKDOWN
},
'converts standard simple table': {
'rst': SIMPLE_TABLE_2,
'md': SIMPLE_TABLE_2_MARKDOWN
Expand Down