# HG changeset patch # User souliane # Date 1387377450 -3600 # Node ID 8b6137f7b4e10905db89c250809cff2284c1436c # Parent e1c64a5b45884be0276f5f02ec0b6b40060f9bac tools: addURLToText moved from libervia to sat_frontends/tools/strings diff -r e1c64a5b4588 -r 8b6137f7b4e1 frontends/src/tools/strings.py --- a/frontends/src/tools/strings.py Wed Dec 18 10:39:19 2013 +0100 +++ b/frontends/src/tools/strings.py Wed Dec 18 15:37:30 2013 +0100 @@ -17,6 +17,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import re + def getURLParams(url): """This comes from pyjamas.Location.makeUrlDict with a small change @@ -40,3 +42,15 @@ dict_[kv[0]] = kv[1] if len(kv) > 1 else "" return dict_ + +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)