Skip to content

Commit 1fddc85

Browse files
committed
Escape double dunder fixes #2
1 parent e48356c commit 1fddc85

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

docstring_to_markdown/rst.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def __init__(self, pattern: str, replacement: str, name: Union[str, None] = None
6565
)
6666
]
6767

68+
ESCAPING_RULES: List[Directive] = [
69+
Directive(
70+
pattern=r'__(?P<text>\S+)__',
71+
replacement=r'\_\_\g<text>\_\_'
72+
)
73+
]
74+
6875

6976
def _find_directive_pattern(name: str):
7077
return [
@@ -306,6 +313,11 @@ def initiate_parsing(self, line: str, current_language: str) -> IBlockBeginning:
306313
for section in _RST_SECTIONS
307314
}
308315

316+
DIRECTIVES = [
317+
*RST_DIRECTIVES,
318+
*ESCAPING_RULES
319+
]
320+
309321

310322
def rst_to_markdown(text: str):
311323
"""
@@ -342,7 +354,7 @@ def flush_buffer():
342354
nonlocal lines_buffer
343355
lines = '\n'.join(lines_buffer)
344356
# rst markup handling
345-
for directive in RST_DIRECTIVES:
357+
for directive in DIRECTIVES:
346358
lines = re.sub(directive.pattern, directive.replacement, lines)
347359

348360
for (section, header) in RST_SECTIONS.items():
@@ -353,7 +365,7 @@ def flush_buffer():
353365

354366
for line in text.split('\n'):
355367
if is_first_line:
356-
signature_match = re.match(r'^(?P<name>\w+)\((?P<params>.*)\)$', line)
368+
signature_match = re.match(r'^(?P<name>\S+)\((?P<params>.*)\)$', line)
357369
if signature_match and signature_match.group('name').isidentifier():
358370
markdown += '```python\n' + line + '\n```\n'
359371
continue
@@ -384,9 +396,6 @@ def flush_buffer():
384396

385397
# ok, we are not in any code block (it may start with the next line, but this line is clear - or empty)
386398

387-
if trimmed_line.rstrip() in RST_SECTIONS:
388-
most_recent_section = trimmed_line.rstrip()
389-
390399
# lists handling: items detection
391400
match = re.match(r'^(?P<argument>[^: ]+) : (?P<type>.+)$', trimmed_line)
392401
if match:
@@ -395,6 +404,9 @@ def flush_buffer():
395404
kwargs_or_args_match = re.match(r'^(?P<other_args>\*\*kwargs|\*args)$', trimmed_line)
396405
if kwargs_or_args_match:
397406
line = '- `' + kwargs_or_args_match.group('other_args') + '`'
407+
else:
408+
if trimmed_line.rstrip() in RST_SECTIONS:
409+
most_recent_section = trimmed_line.rstrip()
398410

399411
# change highlight language if requested
400412
# this should not conflict with the parsers starting above

tests/test_rst.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,14 @@ def func(): pass
294294
"""
295295

296296
INITIAL_SIGNATURE = """\
297-
absolute(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
297+
absolute1(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
298298
299299
Calculate the absolute value element-wise.
300300
"""
301301

302302
INITIAL_SIGNATURE_MARKDOWN = """\
303303
```python
304-
absolute(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
304+
absolute1(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
305305
```
306306
307307
Calculate the absolute value element-wise.
@@ -461,7 +461,13 @@ def func(): pass
461461
'converts warnings': {
462462
'rst': WARNING_BLOCK,
463463
'md': WARNING_BLOCK_MARKDOWN
464-
}
464+
},
465+
'escapes double dunders': {
466+
# this is guaranteed to not be any rst markup as per
467+
# https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#inline-markup-recognition-rules
468+
'rst': '__init__',
469+
'md': r'\_\_init\_\_'
470+
},
465471
}
466472

467473

0 commit comments

Comments
 (0)