preserve indentation levels
This commit is contained in:
parent
8562fb8167
commit
054724cc97
3 changed files with 23 additions and 14 deletions
|
|
@ -474,14 +474,14 @@ iter_to_string = iter_to_str
|
|||
|
||||
re_empty = re.compile("\n\s*\n")
|
||||
|
||||
def strip_extra_whitespace(text, max_linebreaks=1, max_spacing=2):
|
||||
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.
|
||||
|
|
@ -492,11 +492,11 @@ def strip_extra_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" {{{max_spacing},}}", " "*max_spacing, text)
|
||||
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):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue