Adds in support for links

This commit is contained in:
Simon Vermeersch 2014-10-05 17:44:35 +02:00
parent ef23cfceb9
commit cde64692ff
5 changed files with 96 additions and 7 deletions

View file

@ -172,7 +172,13 @@ class ANSIParser(object):
"""
return self.ansi_regex.sub("", string)
def parse_ansi(self, string, strip_ansi=False, xterm256=False):
def strip_mxp(self, string):
"""
Strips all MXP codes from a string.
"""
return self.mxp_sub.sub(r'\2', string)
def parse_ansi(self, string, strip_ansi=False, xterm256=False, mxp=False):
"""
Parses a string, subbing color codes according to
the stored mapping.
@ -196,6 +202,7 @@ class ANSIParser(object):
return _PARSE_CACHE[cachekey]
self.do_xterm256 = xterm256
self.do_mxp = mxp
in_string = utils.to_str(string)
# do string replacement
@ -209,8 +216,12 @@ class ANSIParser(object):
if strip_ansi:
# remove all ansi codes (including those manually
# inserted in string)
parsed_string = self.strip_mxp(parsed_string)
return self.strip_raw_codes(parsed_string)
if not mxp:
parsed_string = self.strip_mxp(parsed_string)
# cache and crop old cache
_PARSE_CACHE[cachekey] = parsed_string
if len(_PARSE_CACHE) > _PARSE_CACHE_SIZE:
@ -303,11 +314,14 @@ class ANSIParser(object):
(r'\{\[[0-5]{3}', "") # {[123 - background colour
]
mxp_re = r'\{lc(.*?)\{lt(.*?)\{le'
# prepare regex matching
#ansi_sub = [(re.compile(sub[0], re.DOTALL), sub[1])
# for sub in ansi_map]
xterm256_sub = re.compile(r"|".join([tup[0] for tup in xterm256_map]), re.DOTALL)
ansi_sub = re.compile(r"|".join([re.escape(tup[0]) for tup in mux_ansi_map + ext_ansi_map]), re.DOTALL)
mxp_sub = re.compile(mxp_re, re.DOTALL)
# used by regex replacer to correctly map ansi sequences
ansi_map = dict(mux_ansi_map + ext_ansi_map)
@ -326,12 +340,12 @@ ANSI_PARSER = ANSIParser()
# Access function
#
def parse_ansi(string, strip_ansi=False, parser=ANSI_PARSER, xterm256=False):
def parse_ansi(string, strip_ansi=False, parser=ANSI_PARSER, xterm256=False, mxp=False):
"""
Parses a string, subbing color codes as needed.
"""
return parser.parse_ansi(string, strip_ansi=strip_ansi, xterm256=xterm256)
return parser.parse_ansi(string, strip_ansi=strip_ansi, xterm256=xterm256, mxp=mxp)
def strip_raw_ansi(string, parser=ANSI_PARSER):

View file

@ -77,6 +77,7 @@ class TextToHTMLparser(object):
re_hilite = re.compile("(?:%s)(.*)(?=%s)" % (hilite.replace("[", r"\["), fgstop))
re_uline = re.compile("(?:%s)(.*?)(?=%s)" % (ANSI_UNDERLINE.replace("[", r"\["), fgstop))
re_string = re.compile(r'(?P<htmlchars>[<&>])|(?P<space> [ \t]+)|(?P<lineend>\r\n|\r|\n)', re.S|re.M|re.I)
re_link = re.compile(r'\{lc(.*?)\{lt(.*?)\{le', re.DOTALL)
def re_color(self, text):
"""
@ -119,6 +120,13 @@ class TextToHTMLparser(object):
# change pages (and losing our webclient session).
return re.sub(regexp, r'<a href="\1" target="_blank">\1</a>', text)
def convert_links(self, text):
"""
Replaces links with HTML code
"""
html = "<a href='#' onclick='websocket.send(\"\\1\"); return false;'>\\2</a>"
return self.re_link.sub(html, text)
def do_sub(self, m):
"Helper method to be passed to re.sub."
c = m.groupdict()
@ -139,7 +147,7 @@ class TextToHTMLparser(object):
ansi codes into html statements.
"""
# parse everything to ansi first
text = parse_ansi(text, strip_ansi=strip_ansi, xterm256=False)
text = parse_ansi(text, strip_ansi=strip_ansi, xterm256=False, mxp=True)
# convert all ansi to html
result = re.sub(self.re_string, self.do_sub, text)
result = self.re_color(result)
@ -149,6 +157,7 @@ class TextToHTMLparser(object):
result = self.convert_linebreaks(result)
result = self.remove_backspaces(result)
result = self.convert_urls(result)
result = self.convert_links(result)
# clean out eventual ansi that was missed
#result = parse_ansi(result, strip_ansi=True)