diff --git a/evennia/utils/tests/test_text2html.py b/evennia/utils/tests/test_text2html.py index f6466466e1..9e331f630d 100644 --- a/evennia/utils/tests/test_text2html.py +++ b/evennia/utils/tests/test_text2html.py @@ -140,63 +140,93 @@ class TestText2Html(TestCase): def test_sub_text(self): parser = text2html.HTML_PARSER + mocked_match = mock.Mock() + mocked_match.groupdict.return_value = {"htmlchars": "foo"} self.assertEqual("foo", parser.sub_text(mocked_match)) + mocked_match.groupdict.return_value = {"htmlchars": "", "lineend": "foo"} self.assertEqual("
", parser.sub_text(mocked_match)) - mocked_match.groupdict.return_value = {"htmlchars": "", "lineend": "", "firstspace": "foo"} - self.assertEqual("  ", parser.sub_text(mocked_match)) + parser.tabstop = 2 mocked_match.groupdict.return_value = { "htmlchars": "", "lineend": "", - "firstspace": "", - "space": "\t", + "tab": "\t", + "space": "", } - self.assertEqual("   ", parser.sub_text(mocked_match)) + self.assertEqual("  ", parser.sub_text(mocked_match)) + mocked_match.groupdict.return_value = { "htmlchars": "", "lineend": "", - "firstspace": "", + "tab": "\t\t", "space": " ", "spacestart": " ", } - mocked_match.group.return_value = " \t " - self.assertEqual("    ", parser.sub_text(mocked_match)) + self.assertEqual("    ", + parser.sub_text(mocked_match)) + mocked_match.groupdict.return_value = { "htmlchars": "", "lineend": "", - "firstspace": "", + "tab": "", "space": "", "spacestart": "", } self.assertEqual(None, parser.sub_text(mocked_match)) + def test_parse_tab_to_html(self): + """Test entire parse mechanism""" + parser = text2html.HTML_PARSER + parser.tabstop = 4 + # single tab + self.assertEqual(parser.parse("foo|-foo"), + "foo    foo") + + # space and tab + self.assertEqual(parser.parse("foo |-foo"), + "foo     foo") + + # space, tab, space + self.assertEqual(parser.parse("foo |- foo"), + "foo      foo") + + def test_parse_space_to_html(self): + """test space parsing - a single space should be kept, two or more + should get  """ + parser = text2html.HTML_PARSER + # single space + self.assertEqual(parser.parse("foo foo"), + "foo foo") + # double space + self.assertEqual(parser.parse("foo foo"), + "foo  foo") + # triple space + self.assertEqual(parser.parse("foo foo"), + "foo   foo") + def test_parse_html(self): self.assertEqual("foo", text2html.parse_html("foo")) self.maxDiff = None self.assertEqual( - """Hello World!!""", - text2html.parse_html( - ansi.ANSI_BLINK - + ansi.ANSI_BACK_CYAN - + "Hello " - + ansi.ANSI_NORMAL - + ansi.ANSI_UNDERLINE - + ansi.ANSI_RED - + "W" - + ansi.ANSI_GREEN - + "o" - + ansi.ANSI_YELLOW - + "r" - + ansi.ANSI_BLUE - + "l" - + ansi.ANSI_MAGENTA - + "d" - + ansi.ANSI_CYAN - + "!" - + ansi.ANSI_BACK_GREEN - + "!" - ), + # TODO: note that the blink is currently *not* correctly aborted + # with |n here! This is probably not possible to correctly handle + # with regex - a stateful parser may be needed. + # blink back-cyan normal underline red green yellow blue magenta cyan back-green + text2html.parse_html("|^|[CHello|n|u|rW|go|yr|bl|md|c!|[G!"), + '' + 'Hello' # noqa + '' + 'W' # noqa + 'o' + 'r' + 'l' + 'd' + '!' + '!' # noqa + '' + '' + '' ) diff --git a/evennia/utils/text2html.py b/evennia/utils/text2html.py index 33d1a48ff3..847466aff7 100644 --- a/evennia/utils/text2html.py +++ b/evennia/utils/text2html.py @@ -97,7 +97,7 @@ class TextToHTMLparser(object): re_blink = re.compile("(?:%s)(.*?)(?=%s|%s)" % (blink.replace("[", r"\["), fgstop, bgstop)) re_inverse = re.compile("(?:%s)(.*?)(?=%s|%s)" % (inverse.replace("[", r"\["), fgstop, bgstop)) re_string = re.compile( - r"(?P[<&>])|(?P(?<=\S) )|(?P [ \t]+)|" + r"(?P[<&>])|(?P[\t]+)|(?P +)|" r"(?P^ )|(?P\r\n|\r|\n)", re.S | re.M | re.I, ) @@ -307,13 +307,12 @@ class TextToHTMLparser(object): return html_escape(cdict["htmlchars"]) elif cdict["lineend"]: return "
" - elif cdict["firstspace"]: - return "  " - elif cdict["space"] == "\t": - return " " if self.tabstop == 1 else " " + " " * self.tabstop + elif cdict["tab"]: + text = cdict["tab"].replace("\t", " " * self.tabstop) + return text elif cdict["space"] or cdict["spacestart"]: - text = match.group().replace("\t", " " * self.tabstop) - text = text.replace(" ", " ") + text = cdict["space"] + text = " " if len(text) == 1 else " " + text[1:].replace(" ", " ") return text return None