Handle webclient tabs; refactor html regex structure a little
This commit is contained in:
parent
d985894677
commit
668551a0d5
2 changed files with 67 additions and 38 deletions
|
|
@ -140,63 +140,93 @@ class TestText2Html(TestCase):
|
||||||
|
|
||||||
def test_sub_text(self):
|
def test_sub_text(self):
|
||||||
parser = text2html.HTML_PARSER
|
parser = text2html.HTML_PARSER
|
||||||
|
|
||||||
mocked_match = mock.Mock()
|
mocked_match = mock.Mock()
|
||||||
|
|
||||||
mocked_match.groupdict.return_value = {"htmlchars": "foo"}
|
mocked_match.groupdict.return_value = {"htmlchars": "foo"}
|
||||||
self.assertEqual("foo", parser.sub_text(mocked_match))
|
self.assertEqual("foo", parser.sub_text(mocked_match))
|
||||||
|
|
||||||
mocked_match.groupdict.return_value = {"htmlchars": "", "lineend": "foo"}
|
mocked_match.groupdict.return_value = {"htmlchars": "", "lineend": "foo"}
|
||||||
self.assertEqual("<br>", parser.sub_text(mocked_match))
|
self.assertEqual("<br>", parser.sub_text(mocked_match))
|
||||||
mocked_match.groupdict.return_value = {"htmlchars": "", "lineend": "", "firstspace": "foo"}
|
|
||||||
self.assertEqual(" ", parser.sub_text(mocked_match))
|
|
||||||
parser.tabstop = 2
|
parser.tabstop = 2
|
||||||
mocked_match.groupdict.return_value = {
|
mocked_match.groupdict.return_value = {
|
||||||
"htmlchars": "",
|
"htmlchars": "",
|
||||||
"lineend": "",
|
"lineend": "",
|
||||||
"firstspace": "",
|
"tab": "\t",
|
||||||
"space": "\t",
|
"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": "",
|
||||||
"lineend": "",
|
"lineend": "",
|
||||||
"firstspace": "",
|
"tab": "\t\t",
|
||||||
"space": " ",
|
"space": " ",
|
||||||
"spacestart": " ",
|
"spacestart": " ",
|
||||||
}
|
}
|
||||||
mocked_match.group.return_value = " \t "
|
self.assertEqual(" ",
|
||||||
self.assertEqual(" ", parser.sub_text(mocked_match))
|
parser.sub_text(mocked_match))
|
||||||
|
|
||||||
mocked_match.groupdict.return_value = {
|
mocked_match.groupdict.return_value = {
|
||||||
"htmlchars": "",
|
"htmlchars": "",
|
||||||
"lineend": "",
|
"lineend": "",
|
||||||
"firstspace": "",
|
"tab": "",
|
||||||
"space": "",
|
"space": "",
|
||||||
"spacestart": "",
|
"spacestart": "",
|
||||||
}
|
}
|
||||||
self.assertEqual(None, parser.sub_text(mocked_match))
|
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):
|
def test_parse_html(self):
|
||||||
self.assertEqual("foo", text2html.parse_html("foo"))
|
self.assertEqual("foo", text2html.parse_html("foo"))
|
||||||
self.maxDiff = None
|
self.maxDiff = None
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"""<span class="blink"><span class="bgcolor-006">Hello </span><span class="underline"><span class="err">W</span><span class="err">o</span><span class="err">r</span><span class="err">l</span><span class="err">d</span><span class="err">!<span class="bgcolor-002">!</span></span></span></span>""",
|
# TODO: note that the blink is currently *not* correctly aborted
|
||||||
text2html.parse_html(
|
# with |n here! This is probably not possible to correctly handle
|
||||||
ansi.ANSI_BLINK
|
# with regex - a stateful parser may be needed.
|
||||||
+ ansi.ANSI_BACK_CYAN
|
# blink back-cyan normal underline red green yellow blue magenta cyan back-green
|
||||||
+ "Hello "
|
text2html.parse_html("|^|[CHello|n|u|rW|go|yr|bl|md|c!|[G!"),
|
||||||
+ ansi.ANSI_NORMAL
|
'<span class="blink">'
|
||||||
+ ansi.ANSI_UNDERLINE
|
'<span class="bgcolor-006">Hello</span>' # noqa
|
||||||
+ ansi.ANSI_RED
|
'<span class="underline">'
|
||||||
+ "W"
|
'<span class="color-009">W</span>' # noqa
|
||||||
+ ansi.ANSI_GREEN
|
'<span class="color-010">o</span>'
|
||||||
+ "o"
|
'<span class="color-011">r</span>'
|
||||||
+ ansi.ANSI_YELLOW
|
'<span class="color-012">l</span>'
|
||||||
+ "r"
|
'<span class="color-013">d</span>'
|
||||||
+ ansi.ANSI_BLUE
|
'<span class="color-014">!'
|
||||||
+ "l"
|
'<span class="bgcolor-002">!</span>' # noqa
|
||||||
+ ansi.ANSI_MAGENTA
|
'</span>'
|
||||||
+ "d"
|
'</span>'
|
||||||
+ ansi.ANSI_CYAN
|
'</span>'
|
||||||
+ "!"
|
|
||||||
+ ansi.ANSI_BACK_GREEN
|
|
||||||
+ "!"
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,7 @@ 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<firstspace>(?<=\S) )|(?P<space> [ \t]+)|"
|
r"(?P<htmlchars>[<&>])|(?P<tab>[\t]+)|(?P<space> +)|"
|
||||||
r"(?P<spacestart>^ )|(?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,
|
||||||
)
|
)
|
||||||
|
|
@ -307,13 +307,12 @@ class TextToHTMLparser(object):
|
||||||
return html_escape(cdict["htmlchars"])
|
return html_escape(cdict["htmlchars"])
|
||||||
elif cdict["lineend"]:
|
elif cdict["lineend"]:
|
||||||
return "<br>"
|
return "<br>"
|
||||||
elif cdict["firstspace"]:
|
elif cdict["tab"]:
|
||||||
return " "
|
text = cdict["tab"].replace("\t", " " * self.tabstop)
|
||||||
elif cdict["space"] == "\t":
|
return text
|
||||||
return " " if self.tabstop == 1 else " " + " " * self.tabstop
|
|
||||||
elif cdict["space"] or cdict["spacestart"]:
|
elif cdict["space"] or cdict["spacestart"]:
|
||||||
text = match.group().replace("\t", " " * self.tabstop)
|
text = cdict["space"]
|
||||||
text = text.replace(" ", " ")
|
text = " " if len(text) == 1 else " " + text[1:].replace(" ", " ")
|
||||||
return text
|
return text
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue