-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bac7731
commit 62d2d71
Showing
10 changed files
with
38 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
import textwrap | ||
|
||
|
||
def word_wrap_except_code_blocks(text: str, width: int = 80) -> str: | ||
""" | ||
Wraps text except for code blocks for nice terminal formatting. | ||
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 (str): The text to wrap. | ||
width (int): The width of the lines to wrap at, passed to `textwrap.fill`. | ||
Returns: | ||
The wrapped text. | ||
""" | ||
blocks = text.split("```") | ||
for i in range(len(blocks)): | ||
if i % 2 == 0: | ||
paras = blocks[i].split("\n") | ||
wrapped = [textwrap.fill(para, width=width) for para in paras] | ||
blocks[i] = "\n".join(wrapped) | ||
|
||
return "```".join(blocks) |