comparison browser_side/tools.py @ 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 c2639c9f86ea
children d7c41c84d062
comparison
equal deleted inserted replaced
216:9827cda1a6b0 217:f7ec248192de
18 You should have received a copy of the GNU Affero General Public License 18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 from pyjamas.ui.DragWidget import DragWidget 22 from pyjamas.ui.DragWidget import DragWidget
23 import re
23 24
24 def html_sanitize(html): 25 def html_sanitize(html):
25 """Naive sanitization of HTML""" 26 """Naive sanitization of HTML"""
26 return html.replace('<','&lt;').replace('>','&gt;') 27 return html.replace('<','&lt;').replace('>','&gt;')
28
29
30 def addURLToText(string):
31 """Check a text for what looks like an URL and make it clickable. Regexp
32 from http://daringfireball.net/2010/07/improved_regex_for_matching_urls"""
33
34 def repl(match):
35 url = match.group(0)
36 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url):
37 url = "http://" + url
38 return '<a href="%s" target="_blank" class="url">%s</a>' % (url, match.group(0))
39 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`!()\[\]{};:'".,<>?]))"""
40 return re.sub(pattern, repl, string)
41
27 42
28 class DragLabel(DragWidget): 43 class DragLabel(DragWidget):
29 44
30 def __init__(self, text, _type): 45 def __init__(self, text, _type):
31 DragWidget.__init__(self) 46 DragWidget.__init__(self)
32 self._text = text 47 self._text = text
33 self._type = _type 48 self._type = _type
34 49
35 def onDragStart(self, event): 50 def onDragStart(self, event):
36 dt = event.dataTransfer 51 dt = event.dataTransfer
37 dt.setData('text/plain', "%s\n%s" % (self._text, self._type)) 52 dt.setData('text/plain', "%s\n%s" % (self._text, self._type))
38 dt.setDragImage(self.getElement(), 15, 15) 53 dt.setDragImage(self.getElement(), 15, 15)
39 54