Fixed a string error in the webclient that could appear on some server setups. Added URL parsing to the webclient.
This commit is contained in:
parent
e7b3916aec
commit
7e898842b9
4 changed files with 13 additions and 8 deletions
137
src/utils/text2html.py
Normal file
137
src/utils/text2html.py
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
|
||||
"""
|
||||
ANSI -> html converter
|
||||
|
||||
Credit for original idea and implementation
|
||||
goes to Muhammad Alkarouri and his
|
||||
snippet #577349 on http://code.activestate.com.
|
||||
|
||||
(extensively modified by Griatch 2010)
|
||||
"""
|
||||
|
||||
import re
|
||||
import cgi
|
||||
from src.utils import ansi
|
||||
|
||||
class TextToHTMLparser(object):
|
||||
"""
|
||||
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
|
||||
|
||||
re_string = re.compile(r'(?P<htmlchars>[<&>])|(?P<space>^[ \t]+)|(?P<lineend>\r\n|\r|\n)|(?P<protocal>(^|\s)((http|ftp)://.*?))(\s|$)',
|
||||
re.S|re.M|re.I)
|
||||
|
||||
def re_color(self, text):
|
||||
"Replace ansi colors with html color tags"
|
||||
for colorname, code in self.colorcodes:
|
||||
regexp = "(?:%s)(.*?)(?:%s)" % (code, self.normalcode)
|
||||
regexp = regexp.replace('[', r'\[')
|
||||
text = re.sub(regexp, r'''<span style="color: %s">\1</span>''' % colorname, text)
|
||||
return text
|
||||
|
||||
def re_bold(self, text):
|
||||
"Replace ansi hilight with bold text"
|
||||
regexp = "(?:%s)(.*?)(?:%s)" % ('\033[1m', self.normalcode)
|
||||
regexp = regexp.replace('[', r'\[')
|
||||
return re.sub(regexp, r'<span style="font-weight:bold">\1</span>', text)
|
||||
|
||||
def re_underline(self, text):
|
||||
"Replace ansi underline with html equivalent"
|
||||
regexp = "(?:%s)(.*?)(?:%s)" % ('\033[4m', self.normalcode)
|
||||
regexp = regexp.replace('[', r'\[')
|
||||
return re.sub(regexp, r'<span style="text-decoration: underline">\1</span>', text)
|
||||
|
||||
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>')
|
||||
|
||||
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>'
|
||||
elif c['space']:
|
||||
t = m.group().replace('\t', ' '*self.tabstop)
|
||||
t = t.replace(' ', ' ')
|
||||
return t
|
||||
elif c['space'] == '\t':
|
||||
return ' '*self.tabstop
|
||||
else:
|
||||
url = m.group('protocal')
|
||||
if url.startswith(' '):
|
||||
prefix = ' '
|
||||
url = url[1:]
|
||||
else:
|
||||
prefix = ''
|
||||
last = m.groups()[-1]
|
||||
if last in ['\n', '\r', '\r\n']:
|
||||
last = '<br>'
|
||||
return '%s%s' % (prefix, url)
|
||||
|
||||
def parse(self, text):
|
||||
"""
|
||||
Main access function, converts a text containing
|
||||
ansi codes into html statements.
|
||||
"""
|
||||
|
||||
# parse everything to ansi first
|
||||
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)
|
||||
|
||||
# clean out eventual ansi that was missed
|
||||
result = ansi.parse_ansi(result, strip_ansi=True)
|
||||
|
||||
return result
|
||||
|
||||
HTML_PARSER = TextToTMLparser()
|
||||
|
||||
#
|
||||
# Access function
|
||||
#
|
||||
|
||||
def parse_html(string, parser=HTML_PARSER):
|
||||
"""
|
||||
Parses a string, replace ansi markup with html
|
||||
"""
|
||||
return parser.parse(string)
|
||||
Loading…
Add table
Add a link
Reference in a new issue