@@ -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
6976def _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
310322def 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
0 commit comments