comparison frontends/src/tools/strings.py @ 769:8b6137f7b4e1

tools: addURLToText moved from libervia to sat_frontends/tools/strings
author souliane <souliane@mailoo.org>
date Wed, 18 Dec 2013 15:37:30 +0100
parents 56aa0e98c92e
children 1fe00f0c9a91
comparison
equal deleted inserted replaced
768:e1c64a5b4588 769:8b6137f7b4e1
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 import re
19 21
20 22
21 def getURLParams(url): 23 def getURLParams(url):
22 """This comes from pyjamas.Location.makeUrlDict with a small change 24 """This comes from pyjamas.Location.makeUrlDict with a small change
23 to also parse full URLs, and parameters with no value specified 25 to also parse full URLs, and parameters with no value specified
38 continue 40 continue
39 kv = pair.split("=", 1) 41 kv = pair.split("=", 1)
40 dict_[kv[0]] = kv[1] if len(kv) > 1 else "" 42 dict_[kv[0]] = kv[1] if len(kv) > 1 else ""
41 return dict_ 43 return dict_
42 44
45
46 def addURLToText(string):
47 """Check a text for what looks like an URL and make it clickable. Regexp
48 from http://daringfireball.net/2010/07/improved_regex_for_matching_urls"""
49
50 def repl(match):
51 url = match.group(0)
52 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url):
53 url = "http://" + url
54 return '<a href="%s" target="_blank" class="url">%s</a>' % (url, match.group(0))
55 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`!()\[\]{};:'".,<>?]))"""
56 return re.sub(pattern, repl, string)