@@ -63,20 +63,37 @@ def rst(
6363 # do not convert it.
6464 # (This makes code generation significantly faster; calling out to pandoc
6565 # is by far the most expensive thing we do.)
66- if not re .search (r"(?<!\w)_|_(?!\w)| [|*`\ [\]]" , text ):
66+ if not re .search (r"[|*`_ [\]]" , text ):
6767 answer = wrap (
6868 text ,
6969 indent = indent ,
7070 offset = indent + 3 ,
7171 width = width - indent ,
7272 )
7373 else :
74- # Convert from CommonMark to ReStructured Text.
75- answer = _convert_pandoc (
76- str (text ),
77- width - indent ,
78- str (source_format )
79- ).replace ('\n ' , f"\n { ' ' * indent } " )
74+ # Check if snake_case-only AND NOT a list.
75+ # This regex excludes underscores surrounded by word characters, meaning it finds "real" formatting.
76+ has_real_formatting = re .search (r"(?<!\w)_|_(?!\w)|[|*`\[\]]" , text )
77+ is_list = re .search (r"^\s*([-+*]|\d+\.)\s+" , text , re .MULTILINE )
78+
79+ if not has_real_formatting and not is_list :
80+ # Mimic Pandoc behavior for plain text with snake_case:
81+ # 1. Collapse multiple spaces (Markdown behavior)
82+ text_normalized = re .sub (r'[ \t]+' , ' ' , text )
83+ # 2. Wrap without the specific 'docstring first line' offset
84+ answer = wrap (
85+ text_normalized ,
86+ indent = indent ,
87+ offset = 0 ,
88+ width = width - indent ,
89+ )
90+ else :
91+ # Convert from CommonMark to ReStructured Text.
92+ answer = _convert_pandoc (
93+ str (text ),
94+ width - indent ,
95+ str (source_format )
96+ ).replace ('\n ' , f"\n { ' ' * indent } " )
8097
8198 # Add a newline to the end of the document if any line breaks are
8299 # already present.
0 commit comments