2010-12-07 02:34:59 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
ANSI -> html converter
|
|
|
|
|
|
2012-10-23 19:32:30 +02:00
|
|
|
Credit for original idea and implementation
|
|
|
|
|
goes to Muhammad Alkarouri and his
|
2010-12-07 02:34:59 +00:00
|
|
|
snippet #577349 on http://code.activestate.com.
|
|
|
|
|
|
|
|
|
|
(extensively modified by Griatch 2010)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
import cgi
|
|
|
|
|
from src.utils import ansi
|
|
|
|
|
|
2010-12-07 21:46:26 +00:00
|
|
|
class TextToHTMLparser(object):
|
2010-12-07 02:34:59 +00:00
|
|
|
"""
|
|
|
|
|
This class describes a parser for converting from ansi to html.
|
2012-10-23 19:32:30 +02:00
|
|
|
"""
|
|
|
|
|
|
2010-12-07 02:34:59 +00:00
|
|
|
tabstop = 4
|
2012-10-23 19:32:30 +02:00
|
|
|
# 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), ("", "$")])
|
2010-12-07 02:34:59 +00:00
|
|
|
|
2012-02-21 08:27:30 +01:00
|
|
|
re_string = re.compile(r'(?P<htmlchars>[<&>])|(?P<space>^[ \t]+)|(?P<lineend>\r\n|\r|\n)', re.S|re.M|re.I)
|
2010-12-07 02:34:59 +00:00
|
|
|
|
|
|
|
|
def re_color(self, text):
|
2012-02-20 20:34:28 -08:00
|
|
|
"""Replace ansi colors with html color class names.
|
|
|
|
|
Let the client choose how it will display colors, if it wishes to."""
|
2010-12-07 02:34:59 +00:00
|
|
|
for colorname, code in self.colorcodes:
|
2012-10-23 19:32:30 +02:00
|
|
|
regexp = "(?:%s)(.*?)(?=%s)" % (code, self.codestop)
|
2012-02-20 20:34:28 -08:00
|
|
|
text = re.sub(regexp, r'''<span class="%s">\1</span>''' % colorname, text)
|
2012-10-23 19:32:30 +02:00
|
|
|
return re.sub(self.normalcode, "", text)
|
2010-12-07 02:34:59 +00:00
|
|
|
|
|
|
|
|
def re_bold(self, text):
|
2012-10-23 23:47:49 +02:00
|
|
|
"Clean out superfluous hilights rather than set <strong>to make it match the look of telnet."
|
|
|
|
|
#"Replace ansi hilight with strong text element."
|
|
|
|
|
#regexp = "(?:%s)(.*?)(?=%s)" % (self.bold, self.codestop)
|
|
|
|
|
#return re.sub(regexp, r'<strong>\1</strong>', text)
|
|
|
|
|
return re.sub(self.bold, "", text)
|
2010-12-07 02:34:59 +00:00
|
|
|
|
|
|
|
|
def re_underline(self, text):
|
2012-02-20 20:34:28 -08:00
|
|
|
"Replace ansi underline with html underline class name."
|
2012-10-23 19:32:30 +02:00
|
|
|
regexp = "(?:%s)(.*?)(?=%s)" % (self.underline, self.codestop)
|
2012-02-20 20:34:28 -08:00
|
|
|
return re.sub(regexp, r'<span class="underline">\1</span>', text)
|
2010-12-07 02:34:59 +00:00
|
|
|
|
|
|
|
|
def remove_bells(self, text):
|
|
|
|
|
"Remove ansi specials"
|
|
|
|
|
return text.replace('\07', '')
|
|
|
|
|
|
|
|
|
|
def remove_backspaces(self, text):
|
|
|
|
|
"Removes special escape sequences"
|
|
|
|
|
backspace_or_eol = r'(.\010)|(\033\[K)'
|
|
|
|
|
n = 1
|
|
|
|
|
while n > 0:
|
|
|
|
|
text, n = re.subn(backspace_or_eol, '', text, 1)
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
def convert_linebreaks(self, text):
|
|
|
|
|
"Extra method for cleaning linebreaks"
|
|
|
|
|
return text.replace(r'\n', r'<br>')
|
|
|
|
|
|
2010-12-07 22:27:50 +00:00
|
|
|
def convert_urls(self, text):
|
|
|
|
|
"Replace urls (http://...) by valid HTML"
|
|
|
|
|
regexp = r"((ftp|www|http)(\W+\S+[^).,:;?\]\}(\<span\>) \r\n$]+))"
|
2012-10-23 19:32:30 +02:00
|
|
|
# -> added target to output prevent the web browser from attempting to
|
2012-02-20 20:34:28 -08:00
|
|
|
# change pages (and losing our webclient session).
|
|
|
|
|
return re.sub(regexp, r'<a href="\1" target="_blank">\1</a>', text)
|
2010-12-07 22:27:50 +00:00
|
|
|
|
2010-12-07 02:34:59 +00:00
|
|
|
def do_sub(self, m):
|
|
|
|
|
"Helper method to be passed to re.sub."
|
|
|
|
|
c = m.groupdict()
|
|
|
|
|
if c['htmlchars']:
|
|
|
|
|
return cgi.escape(c['htmlchars'])
|
|
|
|
|
if c['lineend']:
|
|
|
|
|
return '<br>'
|
2012-02-20 23:25:22 +01:00
|
|
|
elif c['space'] == '\t':
|
|
|
|
|
return ' '*self.tabstop
|
2010-12-07 02:34:59 +00:00
|
|
|
elif c['space']:
|
|
|
|
|
t = m.group().replace('\t', ' '*self.tabstop)
|
|
|
|
|
t = t.replace(' ', ' ')
|
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
|
def parse(self, text):
|
|
|
|
|
"""
|
2012-10-23 19:32:30 +02:00
|
|
|
Main access function, converts a text containing
|
|
|
|
|
ansi codes into html statements.
|
2010-12-07 02:34:59 +00:00
|
|
|
"""
|
|
|
|
|
|
2012-10-23 19:32:30 +02:00
|
|
|
# parse everything to ansi first
|
2010-12-07 02:34:59 +00:00
|
|
|
text = ansi.parse_ansi(text)
|
|
|
|
|
|
|
|
|
|
# convert all ansi to html
|
|
|
|
|
result = re.sub(self.re_string, self.do_sub, text)
|
|
|
|
|
result = self.re_color(result)
|
|
|
|
|
result = self.re_bold(result)
|
|
|
|
|
result = self.re_underline(result)
|
|
|
|
|
result = self.remove_bells(result)
|
|
|
|
|
result = self.convert_linebreaks(result)
|
|
|
|
|
result = self.remove_backspaces(result)
|
2010-12-07 22:27:50 +00:00
|
|
|
result = self.convert_urls(result)
|
2010-12-07 02:34:59 +00:00
|
|
|
# clean out eventual ansi that was missed
|
|
|
|
|
result = ansi.parse_ansi(result, strip_ansi=True)
|
2012-10-23 19:32:30 +02:00
|
|
|
|
|
|
|
|
return result
|
2010-12-07 02:34:59 +00:00
|
|
|
|
2010-12-07 22:27:50 +00:00
|
|
|
HTML_PARSER = TextToHTMLparser()
|
2010-12-07 02:34:59 +00:00
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Access function
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def parse_html(string, parser=HTML_PARSER):
|
|
|
|
|
"""
|
|
|
|
|
Parses a string, replace ansi markup with html
|
|
|
|
|
"""
|
|
|
|
|
return parser.parse(string)
|