Skip to content

Commit

Permalink
Updated to handle when ``` is not at start of line. Fixes plasma-umas…
Browse files Browse the repository at this point in the history
  • Loading branch information
Infinite15 committed Sep 30, 2023
1 parent fc7e650 commit 2bc941f
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/chatdbg/chatdbg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,54 @@ def get_model() -> str:

return model


def word_wrap_except_code_blocks(text: str) -> str:
"""
Wraps text except for code blocks.
Splits the text into paragraphs and wraps each paragraph,
except for paragraphs that are inside of code blocks denoted
by ` ``` `. Returns the updated text.
Args:
text: The text to wrap.
Returns:
The wrapped text.
"""
# Split text into paragraphs
paragraphs = text.split("\n\n")
wrapped_paragraphs = []
# Check if currently in a code block.
in_code_block = False
# Loop through each paragraph and apply appropriate wrapping.
for paragraph in paragraphs:
# Check for the presence of triple quotes in the paragraph
if "```" in paragraph:
# Split paragraph by triple quotes
parts = paragraph.split("```")
for i, part in enumerate(parts):
# If we are inside a code block, do not wrap the text
if in_code_block:
wrapped_paragraphs.append(part)
else:
# Otherwise, apply text wrapping to the part
wrapped_paragraphs.append(textwrap.fill(part))
# Toggle the in_code_block flag for each triple quote encountered
if i < len(parts) - 1:
wrapped_paragraphs.append("```")
in_code_block = not in_code_block
else:
# If the paragraph does not contain triple quotes and is not inside a code block, wrap the text
if not in_code_block:
wrapped_paragraphs.append(textwrap.fill(paragraph))
else:
wrapped_paragraphs.append(paragraph)
# Join all paragraphs into a single string
wrapped_text = "\n\n".join(wrapped_paragraphs)
return wrapped_text

def word_wrap_except_code_blocks_previous(text: str) -> str:
"""Wraps text except for code blocks.
Splits the text into paragraphs and wraps each paragraph,
Expand Down

0 comments on commit 2bc941f

Please sign in to comment.