Skip to content

Commit d8f2f8b

Browse files
authored
Merge pull request #19 from krassowski/syntax-in-tables
Parse syntax within tables
2 parents 6fbc1a9 + eafa943 commit d8f2f8b

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

docstring_to_markdown/rst.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ def _split(self, line: str) -> List[str]:
402402
self._columns_end
403403
)
404404
fragment = line[start:end].strip()
405+
fragment = rst_to_markdown(fragment, extract_signature=False)
405406
self._max_sizes[i] = max(self._max_sizes[i], len(fragment))
406407
fragments.append(fragment)
407408
return fragments
@@ -647,7 +648,7 @@ def initiate_parsing(self, line: str, current_language: str) -> IBlockBeginning:
647648
]
648649

649650

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

694695
for line in text.split('\n'):
695696
if is_first_line:
696-
signature_match = re.match(r'^(?P<name>\S+)\((?P<params>.*)\)$', line)
697-
if signature_match and signature_match.group('name').isidentifier():
698-
markdown += '```python\n' + line + '\n```\n'
699-
continue
697+
if extract_signature:
698+
signature_match = re.match(r'^(?P<name>\S+)\((?P<params>.*)\)$', line)
699+
if signature_match and signature_match.group('name').isidentifier():
700+
markdown += '```python\n' + line + '\n```\n'
701+
continue
702+
is_first_line = False
700703

701704
trimmed_line = line.lstrip()
702705

tests/test_rst.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,24 @@ def func(): pass
471471
"""
472472

473473

474+
SIMPLE_TABLE_WITH_MARKUP = """
475+
============================== =======================================
476+
Scalar Type Array Type
477+
============================== =======================================
478+
:class:`pandas.Interval` :class:`pandas.arrays.IntervalArray`
479+
:class:`bool` :class:`pandas.arrays.BooleanArray`
480+
============================== =======================================
481+
"""
482+
483+
484+
SIMPLE_TABLE_WITH_MARKUP_MARKDOWN = """
485+
| Scalar Type | Array Type |
486+
| ----------------- | ----------------------------- |
487+
| `pandas.Interval` | `pandas.arrays.IntervalArray` |
488+
| `bool` | `pandas.arrays.BooleanArray` |
489+
"""
490+
491+
474492
SIMPLE_TABLE_2 = """
475493
.. warning:: This is a standard simple table
476494
@@ -782,6 +800,10 @@ def foo():
782800
'rst': SIMPLE_TABLE,
783801
'md': SIMPLE_TABLE_MARKDOWN
784802
},
803+
'converts syntax within table': {
804+
'rst': SIMPLE_TABLE_WITH_MARKUP,
805+
'md': SIMPLE_TABLE_WITH_MARKUP_MARKDOWN
806+
},
785807
'converts standard simple table': {
786808
'rst': SIMPLE_TABLE_2,
787809
'md': SIMPLE_TABLE_2_MARKDOWN

0 commit comments

Comments
 (0)