Fixed some issues with the text2html converted that failed with colours in the web client.

This commit is contained in:
Griatch 2012-10-23 19:32:30 +02:00
parent c0a4f62e95
commit b26d93f86b

View file

@ -18,24 +18,27 @@ class TextToHTMLparser(object):
This class describes a parser for converting from ansi to html. This class describes a parser for converting from ansi to html.
""" """
# mapping html color name <-> ansi code.
# Obs order matters - longer ansi codes are replaced first.
colorcodes = [('white', '\033[1m\033[37m'),
('cyan', '\033[1m\033[36m'),
('blue', '\033[1m\033[34m'),
('red', '\033[1m\033[31m'),
('magenta', '\033[1m\033[35m'),
('lime', '\033[1m\033[32m'),
('yellow', '\033[1m\033[33m'),
('gray', '\033[37m'),
('teal', '\033[36m'),
('navy', '\033[34m'),
('maroon', '\033[31m'),
('purple', '\033[35m'),
('green', '\033[32m'),
('olive', '\033[33m')]
normalcode = '\033[0m'
tabstop = 4 tabstop = 4
# mapping html color name <-> ansi code.
# note that \[ is used here since they go into regexes.
colorcodes = [('white', '\033\[1m\033\[37m'),
('cyan', '\033\[1m\033\[36m'),
('blue', '\033\[1m\033\[34m'),
('red', '\033\[1m\033\[31m'),
('magenta', '\033\[1m\033\[35m'),
('lime', '\033\[1m\033\[32m'),
('yellow', '\033\[1m\033\[33m'),
('gray', '\033\[37m'),
('teal', '\033\[36m'),
('navy', '\033\[34m'),
('maroon', '\033\[31m'),
('purple', '\033\[35m'),
('green', '\033\[32m'),
('olive', '\033\[33m')]
normalcode = '\033\[0m'
bold = '\033\[1m'
underline = '\033\[4m'
codestop = "|".join(co[1] for co in colorcodes + [("", normalcode), ("", bold), ("", underline), ("", "$")])
re_string = re.compile(r'(?P<htmlchars>[<&>])|(?P<space>^[ \t]+)|(?P<lineend>\r\n|\r|\n)', re.S|re.M|re.I) re_string = re.compile(r'(?P<htmlchars>[<&>])|(?P<space>^[ \t]+)|(?P<lineend>\r\n|\r|\n)', re.S|re.M|re.I)
@ -43,21 +46,18 @@ class TextToHTMLparser(object):
"""Replace ansi colors with html color class names. """Replace ansi colors with html color class names.
Let the client choose how it will display colors, if it wishes to.""" Let the client choose how it will display colors, if it wishes to."""
for colorname, code in self.colorcodes: for colorname, code in self.colorcodes:
regexp = "(?:%s)(.*?)(?:%s)" % (code, self.normalcode) regexp = "(?:%s)(.*?)(?=%s)" % (code, self.codestop)
regexp = regexp.replace('[', r'\[')
text = re.sub(regexp, r'''<span class="%s">\1</span>''' % colorname, text) text = re.sub(regexp, r'''<span class="%s">\1</span>''' % colorname, text)
return text return re.sub(self.normalcode, "", text)
def re_bold(self, text): def re_bold(self, text):
"Replace ansi hilight with strong text element." "Replace ansi hilight with strong text element."
regexp = "(?:%s)(.*?)(?:%s)" % ('\033[1m', self.normalcode) regexp = "(?:%s)(.*?)(?=%s)" % (self.bold, self.codestop)
regexp = regexp.replace('[', r'\[')
return re.sub(regexp, r'<strong>\1</strong>', text) return re.sub(regexp, r'<strong>\1</strong>', text)
def re_underline(self, text): def re_underline(self, text):
"Replace ansi underline with html underline class name." "Replace ansi underline with html underline class name."
regexp = "(?:%s)(.*?)(?:%s)" % ('\033[4m', self.normalcode) regexp = "(?:%s)(.*?)(?=%s)" % (self.underline, self.codestop)
regexp = regexp.replace('[', r'\[')
return re.sub(regexp, r'<span class="underline">\1</span>', text) return re.sub(regexp, r'<span class="underline">\1</span>', text)
def remove_bells(self, text): def remove_bells(self, text):