Skip to content

Commit cf6b285

Browse files
authored
Merge pull request #9 from krassowski/feature/simple-table-with-indent
Parse indented tables
2 parents 4373604 + 31fcc43 commit cf6b285

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

docstring_to_markdown/rst.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,22 @@ class State(IntEnum):
198198
PARSING_ROWS = auto()
199199
FINISHED = auto()
200200

201-
outer_border_pattern = r'^=+( +=+)+$'
201+
outer_border_pattern = r'^(\s*)=+( +=+)+$'
202202

203203
_state: int
204204
_column_starts: List[int]
205205
_columns: List[str]
206206
_rows: List[List[str]]
207207
_max_sizes: List[int]
208+
_indent: str
208209

209210
def _reset_state(self):
210211
self._state = TableParser.State.AWAITS
211212
self._column_starts = []
212213
self._columns = []
213214
self._rows = []
214215
self._max_sizes = []
216+
self._indent = ''
215217

216218
def can_parse(self, line: str) -> bool:
217219
return bool(re.match(self.outer_border_pattern, line))
@@ -220,6 +222,7 @@ def initiate_parsing(self, line: str, current_language: str) -> IBlockBeginning:
220222
self._reset_state()
221223
match = re.match(self.outer_border_pattern, line)
222224
assert match
225+
self._indent = match.group(1) or ''
223226
self._column_starts = []
224227
previous = ' '
225228
for i, char in enumerate(line):
@@ -263,7 +266,7 @@ def _wrap(self, row: List[str], align=str.ljust) -> str:
263266
align(e, self._max_sizes[i])
264267
for i, e in enumerate(row)
265268
]
266-
return '| ' + (' | '.join(padded_row)) + ' |\n'
269+
return self._indent + '| ' + (' | '.join(padded_row)) + ' |\n'
267270

268271
def finish_consumption(self, final: bool) -> str:
269272
result = self._wrap(self._columns, align=str.center)

tests/test_rst.py

+43
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,45 @@ def func(): pass
491491
| True | False | False |
492492
"""
493493

494+
SIMPLE_TABLE_IN_PARAMS = """
495+
Create an array.
496+
Parameters
497+
----------
498+
object : array_like
499+
An array, any object exposing the array interface, an object whose
500+
__array__ method returns an array, or any (nested) sequence.
501+
order : {'K', 'A', 'C', 'F'}, optional
502+
Specify the memory layout of the array.
503+
If object is an array the following holds.
504+
===== ========= ===================================================
505+
order no copy copy=True
506+
===== ========= ===================================================
507+
'K' unchanged F & C order preserved, otherwise most similar order
508+
'F' F order F order
509+
===== ========= ===================================================
510+
When ``copy=False`` and a copy is made for other reasons...
511+
subok : bool, optional
512+
If True, then sub-classes will be passed-through, otherwise
513+
"""
514+
515+
SIMPLE_TABLE_IN_PARAMS_MARKDOWN = r"""
516+
Create an array.
517+
#### Parameters
518+
519+
- `object`: array_like
520+
An array, any object exposing the array interface, an object whose
521+
\_\_array\_\_ method returns an array, or any (nested) sequence.
522+
- `order`: {'K', 'A', 'C', 'F'}, optional
523+
Specify the memory layout of the array.
524+
If object is an array the following holds.
525+
| order | no copy | copy=True |
526+
| ----- | --------- | --------------------------------------------------- |
527+
| 'K' | unchanged | F & C order preserved, otherwise most similar order |
528+
| 'F' | F order | F order |
529+
When ``copy=False`` and a copy is made for other reasons...
530+
- `subok`: bool, optional
531+
If True, then sub-classes will be passed-through, otherwise
532+
"""
494533

495534
INTEGRATION = """
496535
Return a fixed frequency DatetimeIndex.
@@ -618,6 +657,10 @@ def func(): pass
618657
'converts standard simple table': {
619658
'rst': SIMPLE_TABLE_2,
620659
'md': SIMPLE_TABLE_2_MARKDOWN
660+
},
661+
'converts indented simple table': {
662+
'rst': SIMPLE_TABLE_IN_PARAMS,
663+
'md': SIMPLE_TABLE_IN_PARAMS_MARKDOWN
621664
}
622665
}
623666

0 commit comments

Comments
 (0)