Merge branch 'fix-webclient-whitespace' of https://github.com/InspectorCaracal/evennia into InspectorCaracal-fix-webclient-whitespace
This commit is contained in:
commit
2bbda066dd
3 changed files with 13 additions and 72 deletions
|
|
@ -120,13 +120,6 @@ class TestText2Html(TestCase):
|
||||||
)
|
)
|
||||||
# TODO: doesn't URL encode correctly
|
# TODO: doesn't URL encode correctly
|
||||||
|
|
||||||
def test_re_double_space(self):
|
|
||||||
parser = text2html.HTML_PARSER
|
|
||||||
self.assertEqual("foo", parser.re_double_space("foo"))
|
|
||||||
self.assertEqual(
|
|
||||||
"a red foo", parser.re_double_space("a red foo")
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_sub_mxp_links(self):
|
def test_sub_mxp_links(self):
|
||||||
parser = text2html.HTML_PARSER
|
parser = text2html.HTML_PARSER
|
||||||
mocked_match = mock.Mock()
|
mocked_match = mock.Mock()
|
||||||
|
|
@ -156,7 +149,7 @@ class TestText2Html(TestCase):
|
||||||
"tab": "\t",
|
"tab": "\t",
|
||||||
"space": "",
|
"space": "",
|
||||||
}
|
}
|
||||||
self.assertEqual(" ", parser.sub_text(mocked_match))
|
self.assertEqual(" ", parser.sub_text(mocked_match))
|
||||||
|
|
||||||
mocked_match.groupdict.return_value = {
|
mocked_match.groupdict.return_value = {
|
||||||
"htmlchars": "",
|
"htmlchars": "",
|
||||||
|
|
@ -165,7 +158,7 @@ class TestText2Html(TestCase):
|
||||||
"space": " ",
|
"space": " ",
|
||||||
"spacestart": " ",
|
"spacestart": " ",
|
||||||
}
|
}
|
||||||
self.assertEqual(" ", parser.sub_text(mocked_match))
|
self.assertEqual(" ", parser.sub_text(mocked_match))
|
||||||
|
|
||||||
mocked_match.groupdict.return_value = {
|
mocked_match.groupdict.return_value = {
|
||||||
"htmlchars": "",
|
"htmlchars": "",
|
||||||
|
|
@ -181,24 +174,13 @@ class TestText2Html(TestCase):
|
||||||
parser = text2html.HTML_PARSER
|
parser = text2html.HTML_PARSER
|
||||||
parser.tabstop = 4
|
parser.tabstop = 4
|
||||||
# single tab
|
# single tab
|
||||||
self.assertEqual(parser.parse("foo|>foo"), "foo foo")
|
self.assertEqual(parser.parse("foo|>foo"), "foo foo")
|
||||||
|
|
||||||
# space and tab
|
# space and tab
|
||||||
self.assertEqual(parser.parse("foo |>foo"), "foo foo")
|
self.assertEqual(parser.parse("foo |>foo"), "foo foo")
|
||||||
|
|
||||||
# space, tab, space
|
# space, tab, space
|
||||||
self.assertEqual(parser.parse("foo |> foo"), "foo foo")
|
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):
|
def test_parse_html(self):
|
||||||
self.assertEqual("foo", text2html.parse_html("foo"))
|
self.assertEqual("foo", text2html.parse_html("foo"))
|
||||||
|
|
|
||||||
|
|
@ -79,11 +79,11 @@ class TextToHTMLparser(object):
|
||||||
# create stop markers
|
# create stop markers
|
||||||
fgstop = "(?:\033\[1m|\033\[22m){0,1}\033\[3[0-8].*?m|\033\[0m|$"
|
fgstop = "(?:\033\[1m|\033\[22m){0,1}\033\[3[0-8].*?m|\033\[0m|$"
|
||||||
bgstop = "(?:\033\[1m|\033\[22m){0,1}\033\[4[0-8].*?m|\033\[0m|$"
|
bgstop = "(?:\033\[1m|\033\[22m){0,1}\033\[4[0-8].*?m|\033\[0m|$"
|
||||||
bgfgstop = bgstop[:-2] + r"(\s*)" + fgstop
|
bgfgstop = bgstop[:-2] + fgstop
|
||||||
|
|
||||||
fgstart = "((?:\033\[1m|\033\[22m){0,1}\033\[3[0-8].*?m)"
|
fgstart = "((?:\033\[1m|\033\[22m){0,1}\033\[3[0-8].*?m)"
|
||||||
bgstart = "((?:\033\[1m|\033\[22m){0,1}\033\[4[0-8].*?m)"
|
bgstart = "((?:\033\[1m|\033\[22m){0,1}\033\[4[0-8].*?m)"
|
||||||
bgfgstart = bgstart + r"(\s*)" + "((?:\033\[1m|\033\[22m){0,1}\033\[[3-4][0-8].*?m){0,1}"
|
bgfgstart = bgstart + r"((?:\033\[1m|\033\[22m){0,1}\033\[[3-4][0-8].*?m){0,1}"
|
||||||
|
|
||||||
# extract color markers, tagging the start marker and the text marked
|
# extract color markers, tagging the start marker and the text marked
|
||||||
re_fgs = re.compile(fgstart + "(.*?)(?=" + fgstop + ")")
|
re_fgs = re.compile(fgstart + "(.*?)(?=" + fgstop + ")")
|
||||||
|
|
@ -97,12 +97,9 @@ class TextToHTMLparser(object):
|
||||||
re_blink = re.compile("(?:%s)(.*?)(?=%s|%s)" % (blink.replace("[", r"\["), fgstop, bgstop))
|
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_inverse = re.compile("(?:%s)(.*?)(?=%s|%s)" % (inverse.replace("[", r"\["), fgstop, bgstop))
|
||||||
re_string = re.compile(
|
re_string = re.compile(
|
||||||
r"(?P<htmlchars>[<&>])|(?P<tab>[\t]+)|(?P<space> +)|"
|
r"(?P<htmlchars>[<&>])|(?P<tab>[\t]+)|(?P<lineend>\r\n|\r|\n)",
|
||||||
r"(?P<spacestart>^ )|(?P<lineend>\r\n|\r|\n)",
|
|
||||||
re.S | re.M | re.I,
|
re.S | re.M | re.I,
|
||||||
)
|
)
|
||||||
re_dblspace = re.compile(r" {2,}", re.M)
|
|
||||||
re_invisiblespace = re.compile(r"( <.*?>)( )")
|
|
||||||
re_url = re.compile(
|
re_url = re.compile(
|
||||||
r'(?<!=")((?:ftp|www|https?)\W+(?:(?!\.(?:\s|$)|&\w+;)[^"\',;$*^\\(){}<>\[\]\s])+)(\.(?:\s|$)|&\w+;|)'
|
r'(?<!=")((?:ftp|www|https?)\W+(?:(?!\.(?:\s|$)|&\w+;)[^"\',;$*^\\(){}<>\[\]\s])+)(\.(?:\s|$)|&\w+;|)'
|
||||||
)
|
)
|
||||||
|
|
@ -111,20 +108,16 @@ class TextToHTMLparser(object):
|
||||||
|
|
||||||
def _sub_bgfg(self, colormatch):
|
def _sub_bgfg(self, colormatch):
|
||||||
# print("colormatch.groups()", colormatch.groups())
|
# print("colormatch.groups()", colormatch.groups())
|
||||||
bgcode, prespace, fgcode, text, postspace = colormatch.groups()
|
bgcode, fgcode, text = colormatch.groups()
|
||||||
if not fgcode:
|
if not fgcode:
|
||||||
ret = r"""<span class="%s">%s%s%s</span>""" % (
|
ret = r"""<span class="%s">%s</span>""" % (
|
||||||
self.bg_colormap.get(bgcode, self.fg_colormap.get(bgcode, "err")),
|
self.bg_colormap.get(bgcode, self.fg_colormap.get(bgcode, "err")),
|
||||||
prespace and " " * len(prespace) or "",
|
|
||||||
postspace and " " * len(postspace) or "",
|
|
||||||
text,
|
text,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
ret = r"""<span class="%s"><span class="%s">%s%s%s</span></span>""" % (
|
ret = r"""<span class="%s"><span class="%s">%s</span></span>""" % (
|
||||||
self.bg_colormap.get(bgcode, self.fg_colormap.get(bgcode, "err")),
|
self.bg_colormap.get(bgcode, self.fg_colormap.get(bgcode, "err")),
|
||||||
self.fg_colormap.get(fgcode, self.bg_colormap.get(fgcode, "err")),
|
self.fg_colormap.get(fgcode, self.bg_colormap.get(fgcode, "err")),
|
||||||
prespace and " " * len(prespace) or "",
|
|
||||||
postspace and " " * len(postspace) or "",
|
|
||||||
text,
|
text,
|
||||||
)
|
)
|
||||||
return ret
|
return ret
|
||||||
|
|
@ -265,20 +258,6 @@ class TextToHTMLparser(object):
|
||||||
# change pages (and losing our webclient session).
|
# change pages (and losing our webclient session).
|
||||||
return self.re_url.sub(r'<a href="\1" target="_blank">\1</a>\2', text)
|
return self.re_url.sub(r'<a href="\1" target="_blank">\1</a>\2', text)
|
||||||
|
|
||||||
def re_double_space(self, text):
|
|
||||||
"""
|
|
||||||
HTML will swallow any normal space after the first, so if any slipped
|
|
||||||
through we must make sure to replace them with " "
|
|
||||||
"""
|
|
||||||
return self.re_dblspace.sub(self.sub_dblspace, text)
|
|
||||||
|
|
||||||
def re_invisible_space(self, text):
|
|
||||||
"""
|
|
||||||
If two spaces are separated by an invisble html element, they act as a
|
|
||||||
hidden double-space and the last of them should be replaced by
|
|
||||||
"""
|
|
||||||
return self.re_invisiblespace.sub(self.sub_invisiblespace, text)
|
|
||||||
|
|
||||||
def sub_mxp_links(self, match):
|
def sub_mxp_links(self, match):
|
||||||
"""
|
"""
|
||||||
Helper method to be passed to re.sub,
|
Helper method to be passed to re.sub,
|
||||||
|
|
@ -332,28 +311,10 @@ class TextToHTMLparser(object):
|
||||||
elif cdict["lineend"]:
|
elif cdict["lineend"]:
|
||||||
return "<br>"
|
return "<br>"
|
||||||
elif cdict["tab"]:
|
elif cdict["tab"]:
|
||||||
text = cdict["tab"].replace("\t", " " + " " * (self.tabstop - 1))
|
text = cdict["tab"].replace("\t", " " * (self.tabstop))
|
||||||
return text
|
|
||||||
elif cdict["space"] or cdict["spacestart"]:
|
|
||||||
text = cdict["space"]
|
|
||||||
text = " " if len(text) == 1 else " " + text[1:].replace(" ", " ")
|
|
||||||
return text
|
return text
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def sub_dblspace(self, match):
|
|
||||||
"clean up double-spaces"
|
|
||||||
return " " + " " * (len(match.group()) - 1)
|
|
||||||
|
|
||||||
def sub_invisiblespace(self, match):
|
|
||||||
"clean up invisible spaces"
|
|
||||||
return match.group(1) + " "
|
|
||||||
|
|
||||||
def handle_single_first_space(self, text):
|
|
||||||
"Don't swallow an initial lone space"
|
|
||||||
if text.startswith(" "):
|
|
||||||
return " " + text[1:]
|
|
||||||
return text
|
|
||||||
|
|
||||||
def parse(self, text, strip_ansi=False):
|
def parse(self, text, strip_ansi=False):
|
||||||
"""
|
"""
|
||||||
Main access function, converts a text containing ANSI codes
|
Main access function, converts a text containing ANSI codes
|
||||||
|
|
@ -383,9 +344,6 @@ class TextToHTMLparser(object):
|
||||||
result = self.convert_linebreaks(result)
|
result = self.convert_linebreaks(result)
|
||||||
result = self.remove_backspaces(result)
|
result = self.remove_backspaces(result)
|
||||||
result = self.convert_urls(result)
|
result = self.convert_urls(result)
|
||||||
result = self.re_double_space(result)
|
|
||||||
result = self.re_invisible_space(result)
|
|
||||||
result = self.handle_single_first_space(result)
|
|
||||||
# clean out eventual ansi that was missed
|
# clean out eventual ansi that was missed
|
||||||
## result = parse_ansi(result, strip_ansi=True)
|
## result = parse_ansi(result, strip_ansi=True)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,7 @@ div {margin:0px;}
|
||||||
.out {
|
.out {
|
||||||
color: #aaa;
|
color: #aaa;
|
||||||
background-color: #000;
|
background-color: #000;
|
||||||
|
white-space: pre-wrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Error messages (red) */
|
/* Error messages (red) */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue