@@ -65,6 +65,13 @@ def __init__(self, pattern: str, replacement: str, name: Union[str, None] = None
65
65
)
66
66
]
67
67
68
+ ESCAPING_RULES : List [Directive ] = [
69
+ Directive (
70
+ pattern = r'__(?P<text>\S+)__' ,
71
+ replacement = r'\_\_\g<text>\_\_'
72
+ )
73
+ ]
74
+
68
75
69
76
def _find_directive_pattern (name : str ):
70
77
return [
@@ -306,6 +313,11 @@ def initiate_parsing(self, line: str, current_language: str) -> IBlockBeginning:
306
313
for section in _RST_SECTIONS
307
314
}
308
315
316
+ DIRECTIVES = [
317
+ * RST_DIRECTIVES ,
318
+ * ESCAPING_RULES
319
+ ]
320
+
309
321
310
322
def rst_to_markdown (text : str ):
311
323
"""
@@ -342,7 +354,7 @@ def flush_buffer():
342
354
nonlocal lines_buffer
343
355
lines = '\n ' .join (lines_buffer )
344
356
# rst markup handling
345
- for directive in RST_DIRECTIVES :
357
+ for directive in DIRECTIVES :
346
358
lines = re .sub (directive .pattern , directive .replacement , lines )
347
359
348
360
for (section , header ) in RST_SECTIONS .items ():
@@ -353,7 +365,7 @@ def flush_buffer():
353
365
354
366
for line in text .split ('\n ' ):
355
367
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 )
357
369
if signature_match and signature_match .group ('name' ).isidentifier ():
358
370
markdown += '```python\n ' + line + '\n ```\n '
359
371
continue
@@ -384,9 +396,6 @@ def flush_buffer():
384
396
385
397
# ok, we are not in any code block (it may start with the next line, but this line is clear - or empty)
386
398
387
- if trimmed_line .rstrip () in RST_SECTIONS :
388
- most_recent_section = trimmed_line .rstrip ()
389
-
390
399
# lists handling: items detection
391
400
match = re .match (r'^(?P<argument>[^: ]+) : (?P<type>.+)$' , trimmed_line )
392
401
if match :
@@ -395,6 +404,9 @@ def flush_buffer():
395
404
kwargs_or_args_match = re .match (r'^(?P<other_args>\*\*kwargs|\*args)$' , trimmed_line )
396
405
if kwargs_or_args_match :
397
406
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 ()
398
410
399
411
# change highlight language if requested
400
412
# this should not conflict with the parsers starting above
0 commit comments