preserve indentation levels

This commit is contained in:
Cal 2024-03-20 16:06:42 -06:00
parent 8562fb8167
commit 054724cc97
3 changed files with 23 additions and 14 deletions

View file

@ -30,7 +30,7 @@ from evennia.utils.utils import (
iter_to_str,
lazy_property,
make_iter,
strip_extra_whitespace,
compress_whitespace,
to_str,
variable_from_module,
)
@ -1446,7 +1446,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
str: The final formatted output.
"""
return strip_extra_whitespace(appearance).strip()
return compress_whitespace(appearance).strip()
def return_appearance(self, looker, **kwargs):
"""

View file

@ -54,21 +54,30 @@ class TestDedent(TestCase):
self.assertEqual(expected_string, utils.dedent(input_string))
class TestStripWhitespace(TestCase):
def test_strip_extra_whitespace(self):
class TestCompressWhitespace(TestCase):
def test_compress_whitespace(self):
# No text, return no text
self.assertEqual("", utils.strip_extra_whitespace(""))
self.assertEqual("", utils.compress_whitespace(""))
# If no whitespace is exceeded, should return the same
self.assertEqual("One line\nTwo spaces", utils.strip_extra_whitespace("One line\nTwo spaces"))
self.assertEqual("One line\nTwo spaces", utils.compress_whitespace("One line\nTwo spaces"))
# Extra newlines are removed
self.assertEqual("First line\nSecond line", utils.strip_extra_whitespace("First line\n\nSecond line"))
self.assertEqual("First line\nSecond line", utils.compress_whitespace("First line\n\nSecond line"))
# Extra spaces are removed
self.assertEqual("Too many spaces", utils.strip_extra_whitespace("Too many spaces"))
self.assertEqual("Too many spaces", utils.compress_whitespace("Too many spaces"))
# "Invisible" extra lines with whitespace are removed
self.assertEqual("First line\nSecond line", utils.strip_extra_whitespace("First line\n \n \nSecond line"))
self.assertEqual("First line\nSecond line", utils.compress_whitespace("First line\n \n \nSecond line"))
# Max kwargs are respected
self.assertEqual("First line\n\nSecond line", utils.strip_extra_whitespace("First line\n\nSecond line", max_spacing=1, max_linebreaks=2))
self.assertEqual("First line\n\nSecond line", utils.compress_whitespace("First line\n\nSecond line", max_spacing=1, max_linebreaks=2))
def test_preserve_indents(self):
"""Ensure that indentation spacing is preserved."""
indented = """\
Hanging Indents
they're great
let's keep them\
"""
# since there is no doubled-up spacing besides indents, input should equal output
self.assertEqual(indented, utils.compress_whitespace(indented))
class TestListToString(TestCase):
"""

View file

@ -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):
"""