Update utils.py

Fixed two regex that wasn't properly escaped
This commit is contained in:
Henry Hsiao 2024-07-19 16:20:13 +08:00 committed by GitHub
parent 45a1cb80e1
commit 1029b8ec64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -473,7 +473,7 @@ def iter_to_str(iterable, sep=",", endsep=", and", addquote=False):
list_to_string = iter_to_str
iter_to_string = iter_to_str
re_empty = re.compile("\n\s*\n")
re_empty = re.compile("\n\\s*\n")
def compress_whitespace(text, max_linebreaks=1, max_spacing=2):
@ -494,7 +494,7 @@ def compress_whitespace(text, max_linebreaks=1, max_spacing=2):
# this allows the blank-line compression to eliminate them if needed
text = re_empty.sub("\n\n", text)
# replace groups of extra spaces with the maximum number of spaces
text = re.sub(f"(?<=\S) {{{max_spacing},}}", " " * max_spacing, text)
text = re.sub(fr"(?<=\S) {{{max_spacing},}}", " " * max_spacing, text)
# replace groups of extra newlines with the maximum number of newlines
text = re.sub(f"\n{{{max_linebreaks},}}", "\n" * max_linebreaks, text)
return text