Mercurial > libervia-backend
changeset 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 | e1c64a5b4588 |
children | 64dd7c0f4feb |
files | frontends/src/tools/strings.py |
diffstat | 1 files changed, 14 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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 <http://www.gnu.org/licenses/>. +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 '<a href="%s" target="_blank" class="url">%s</a>' % (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)