# HG changeset patch # User souliane # Date 1378636440 -7200 # Node ID f7ec248192de4a6a5c77eb4b5d61b771e769b581 # Parent 9827cda1a6b03c88fb1ea21ae1a9ed0990afa2d5 browser_side: display clickable URLs in chat text diff -r 9827cda1a6b0 -r f7ec248192de browser_side/panels.py --- 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 @@ """ % {"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') diff -r 9827cda1a6b0 -r f7ec248192de browser_side/tools.py --- 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('<','<').replace('>','>') + +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 '%s' % (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)) diff -r 9827cda1a6b0 -r f7ec248192de public/libervia.css --- 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