comparison browser_side/tools.py @ 306:52b1afd7ac3f

server_side: display rich text in blogs (addURLToText is now in sat_frontends/tools/strings)
author souliane <souliane@mailoo.org>
date Wed, 18 Dec 2013 15:39:13 +0100
parents 0eba1c4f9c6f
children 6abd099c7007
comparison
equal deleted inserted replaced
305:d002cd631271 306:52b1afd7ac3f
20 """ 20 """
21 21
22 from pyjamas.ui.DragWidget import DragWidget 22 from pyjamas.ui.DragWidget import DragWidget
23 from pyjamas.ui.FileUpload import FileUpload 23 from pyjamas.ui.FileUpload import FileUpload
24 from pyjamas import Window 24 from pyjamas import Window
25 import re
26 from nativedom import NativeDOM 25 from nativedom import NativeDOM
27 from sat_frontends.tools import xml 26 from sat_frontends.tools import xml
28 27
29 dom = NativeDOM() 28 dom = NativeDOM()
30 29
36 35
37 def inlineRoot(xhtml): 36 def inlineRoot(xhtml):
38 """ make root element inline """ 37 """ make root element inline """
39 doc = dom.parseString(xhtml) 38 doc = dom.parseString(xhtml)
40 return xml.inlineRoot(doc) 39 return xml.inlineRoot(doc)
41
42
43 def addURLToText(string):
44 """Check a text for what looks like an URL and make it clickable. Regexp
45 from http://daringfireball.net/2010/07/improved_regex_for_matching_urls"""
46
47 def repl(match):
48 url = match.group(0)
49 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url):
50 url = "http://" + url
51 return '<a href="%s" target="_blank" class="url">%s</a>' % (url, match.group(0))
52 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`!()\[\]{};:'".,<>?]))"""
53 return re.sub(pattern, repl, string)
54 40
55 41
56 def setPresenceStyle(item, state, base_style="contact"): 42 def setPresenceStyle(item, state, base_style="contact"):
57 """ 43 """
58 @item: any UI element 44 @item: any UI element