Merge pull request #3421 from InspectorCaracal/refactor-object-display

Removes need for hardcoding line breaks into display hooks
This commit is contained in:
Griatch 2024-03-21 17:57:44 +01:00 committed by GitHub
commit 191b2a4aef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 4 deletions

View file

@ -472,6 +472,31 @@ 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")
def compress_whitespace(text, max_linebreaks=1, max_spacing=2):
"""
Removes extra sequential whitespace in a block of text. This will also remove any trailing
whitespace at the end.
Args:
text (str): A string which may contain excess internal whitespace.
Keyword args:
max_linebreaks (int): How many linebreak characters are allowed to occur in a row.
max_spacing (int): How many spaces are allowed to occur in a row.
"""
text = text.rstrip()
# replaces any non-visible lines that are just whitespace characters with actual empty lines
# 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)
# replace groups of extra newlines with the maximum number of newlines
text = re.sub(f"\n{{{max_linebreaks},}}", "\n"*max_linebreaks, text)
return text
def wildcard_to_regexp(instring):
"""