changeset 217:f7ec248192de

browser_side: display clickable URLs in chat text
author souliane <souliane@mailoo.org>
date Sun, 08 Sep 2013 12:34:00 +0200
parents 9827cda1a6b0
children 4e6467efd6bf
files browser_side/panels.py browser_side/tools.py public/libervia.css
diffstat 3 files changed, 30 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/panels.py	Sun Sep 08 13:40:01 2013 +0200
+++ b/browser_side/panels.py	Sun Sep 08 12:34:00 2013 +0200
@@ -40,7 +40,7 @@
 from radiocol import RadioColPanel
 from menu import Menu
 from jid import JID
-from tools import html_sanitize
+from tools import html_sanitize, addURLToText
 from datetime import datetime
 from time import time
 import dialog
@@ -274,7 +274,7 @@
             </div>
             """ % {"author": html_sanitize(self.author),
                    "timestamp": _datetime,
-                   "body": html_sanitize(mblog_entry.content)
+                   "body": addURLToText(html_sanitize(mblog_entry.content))
                     })
         self.avatar = Image(blog_panel.host.getAvatar(self.author))
         self.panel.add(self.avatar, "id_avatar")
@@ -533,7 +533,7 @@
                            {"timestamp": _date.strftime("%H:%M"),
                             "nick": "[%s]" % html_sanitize(nick),
                             "msg_class": ' '.join(_msg_class),
-                            "msg": html_sanitize(msg)}
+                            "msg": addURLToText(html_sanitize(msg))}
                            )
         self.setStyleName('chatText')
 
--- a/browser_side/tools.py	Sun Sep 08 13:40:01 2013 +0200
+++ b/browser_side/tools.py	Sun Sep 08 12:34:00 2013 +0200
@@ -20,18 +20,33 @@
 """
 
 from pyjamas.ui.DragWidget import DragWidget
+import re
 
 def html_sanitize(html):
     """Naive sanitization of HTML"""
     return html.replace('<','&lt;').replace('>','&gt;')
 
+
+def addURLToText(string):
+    """Check a text for what looks like an URL and make it clickable. Regexp
+    from http://daringfireball.net/2010/07/improved_regex_for_matching_urls"""
+
+    def repl(match):
+        url = match.group(0)
+        if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url):
+            url = "http://" + url
+        return '<a href="%s" target="_blank" class="url">%s</a>' % (url, match.group(0))
+    pattern = r"""(?i)\b((?:[a-z]{3,}://|(www|ftp)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/|mailto:|xmpp:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))"""
+    return re.sub(pattern, repl, string)
+
+
 class DragLabel(DragWidget):
 
     def __init__(self, text, _type):
         DragWidget.__init__(self)
         self._text = text
         self._type = _type
-    
+
     def onDragStart(self, event):
         dt = event.dataTransfer
         dt.setData('text/plain', "%s\n%s" % (self._text, self._type))
--- a/public/libervia.css	Sun Sep 08 13:40:01 2013 +0200
+++ b/public/libervia.css	Sun Sep 08 12:34:00 2013 +0200
@@ -1111,3 +1111,14 @@
     width: 100%;
     height: 100%;
 }
+
+/* URLs */
+
+a.url {
+    color: blue;
+    text-decoration: none
+}
+
+a:hover.url {
+    text-decoration: underline
+}
\ No newline at end of file