diff -r 8157c41b3236 -r 1f182913b1f2 log_formatter.py --- a/log_formatter.py Mon Feb 09 12:59:03 2009 +0200 +++ b/log_formatter.py Mon Feb 09 13:19:00 2009 +0200 @@ -2,8 +2,7 @@ Format LogLines into some other representation """ -# for escape -import cgi +import re, xml.sax.saxutils from log_line import LogTypes @@ -72,6 +71,29 @@ abstract +class BaseHTMLFormatter (object) : + """ + Implements some HTML-formatting utils + """ + + URL_REGEXP = re.compile(r"http://\S+") + + def _process_links (self, line) : + """ + Processed the rendered line, adding in 's for things that look like URLs, returning the new line. + + The line should already be escaped + """ + + def _encode_url (match) : + # encode URL + url_html = match.group(0) + url_link = xml.sax.saxutils.unescape(url_html) + + return '%(url_html)s' % dict(url_link=url_link, url_html=url_html) + + return self.URL_REGEXP.sub(_encode_url, line) + class IrssiTextFormatter (LogFormatter) : """ Implements format_txt for irssi-style output @@ -88,7 +110,7 @@ # using __TYPES yield self._format_line_text(line, self.__FMT, full_timestamps) -class IrssiFormatter (IrssiTextFormatter) : +class IrssiFormatter (IrssiTextFormatter, BaseHTMLFormatter) : """ Implements plain black-and-white irssi-style formatting """ @@ -107,7 +129,13 @@ # format using IrssiTextFormatter for line in self.format_txt(lines, full_timestamps) : # escape HTML - yield cgi.escape(line) + line = xml.sax.saxutils.escape(line) + + # process links + line = self._process_links(line) + + # yield + yield line # close pre yield ""