Skip to content

Commit 960a68e

Browse files
authored
Merge pull request #95 from danielj-n/main
Prevent latex formatting from running on code blocks and inline code
2 parents 4c5dbeb + ef839ae commit 960a68e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

chatblade/printer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,31 @@ def extract_messages(messages, args):
8787
print(message.content.strip())
8888

8989
def 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, "\0CODE_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, "\0CODE_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("\0CODE_BLOCK\0", code_block, 1)
109+
for code_inline in code_inlines:
110+
msg = msg.replace("\0CODE_INLINE\0", code_inline, 1)
111+
97112
return msg
98113

114+
99115
def detect_and_format_message(msg, cutoff=None, theme=None):
100116
# convert any latex markup to ASCII.
101117
msg = format_latex(msg)

0 commit comments

Comments
 (0)