href-ize urls
authorTero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 13:19:00 +0200
changeset 69 1f182913b1f2
parent 68 8157c41b3236
child 70 72edbbb414a7
href-ize urls
log_formatter.py
static/irclogs.css
templates/channel_date.tmpl
templates/channel_search.tmpl
templates/channel_view.tmpl
templates/lines.tmpl
--- 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 <a href>'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 '<a href="%(url_link)s">%(url_html)s</a>' % 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 "</pre>"
--- a/static/irclogs.css	Mon Feb 09 12:59:03 2009 +0200
+++ b/static/irclogs.css	Mon Feb 09 13:19:00 2009 +0200
@@ -252,7 +252,7 @@
     width: 15em;
 }
 
-input[type=submit] {
+fieldset input[type=submit] {
     width: 8em;
 }
 
@@ -283,3 +283,7 @@
     text-align: left;
 }
 
+/* Log lines */
+div#lines a {
+    color: black;
+}
--- a/templates/channel_date.tmpl	Mon Feb 09 12:59:03 2009 +0200
+++ b/templates/channel_date.tmpl	Mon Feb 09 13:19:00 2009 +0200
@@ -2,8 +2,4 @@
 
 <div id="title">${channel.title} :: Logs for ${h.fmt_date(date)}</div>
 
-% for line in lines :
-${line}
-% endfor
-
-
+<%include file="lines.tmpl" />
--- a/templates/channel_search.tmpl	Mon Feb 09 12:59:03 2009 +0200
+++ b/templates/channel_search.tmpl	Mon Feb 09 13:19:00 2009 +0200
@@ -32,7 +32,5 @@
 % else :
 <div id="title">${channel.title} :: Search '${search_query}'</div>
 
-% for line in lines:
-${line}
-% endfor
+<%include file="lines.tmpl" />
 % endif
--- a/templates/channel_view.tmpl	Mon Feb 09 12:59:03 2009 +0200
+++ b/templates/channel_view.tmpl	Mon Feb 09 13:19:00 2009 +0200
@@ -2,8 +2,4 @@
 
 <div id="title">${channel.title} :: Last ${count} lines</div>
 
-% for line in lines :
-${line}
-% endfor
-
-
+<%include file="lines.tmpl" />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/lines.tmpl	Mon Feb 09 13:19:00 2009 +0200
@@ -0,0 +1,5 @@
+<div id="lines">
+% for line in lines:
+${line}
+% endfor
+</div>