diff --git a/frontends/src/tools/strings.py b/frontends/src/tools/strings.py index 6b050fc1..3c8add32 100644 --- a/frontends/src/tools/strings.py +++ b/frontends/src/tools/strings.py @@ -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 @@ def getURLParams(url): 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)