@@ -87,15 +87,31 @@ def extract_messages(messages, args):
8787 print (message .content .strip ())
8888
8989def format_latex (msg ):
90+ # Replace code blocks and inline code with markers. Use null delimiters to
91+ # hopefully avoid any overlap with anything chatgpt could ever output.
92+ code_block_pattern = re .compile (r"```[\w]*\n.*?\n```" , re .DOTALL )
93+ code_blocks = re .findall (code_block_pattern , msg )
94+ msg = re .sub (code_block_pattern , "\0 CODE_BLOCK\0 " , msg )
95+ code_inline_pattern = re .compile (r"`.*?`" , re .DOTALL )
96+ code_inlines = re .findall (code_inline_pattern , msg )
97+ msg = re .sub (code_inline_pattern , "\0 CODE_INLINE\0 " , msg )
98+
9099 converter = LatexNodes2Text ()
91100 msg = converter .latex_to_text (msg )
92101
93102 # do no change code blocks to smart quotes, this will break the markdown
94103 # parser.
95104 msg = msg .replace ("“" , "``" )
96105
106+ # Restore the code blocks and inline code at the markers
107+ for code_block in code_blocks :
108+ msg = msg .replace ("\0 CODE_BLOCK\0 " , code_block , 1 )
109+ for code_inline in code_inlines :
110+ msg = msg .replace ("\0 CODE_INLINE\0 " , code_inline , 1 )
111+
97112 return msg
98113
114+
99115def detect_and_format_message (msg , cutoff = None , theme = None ):
100116 # convert any latex markup to ASCII.
101117 msg = format_latex (msg )
0 commit comments