tools: addURLToText moved from libervia to sat_frontends/tools/strings

This commit is contained in:
souliane 2013-12-18 15:37:30 +01:00
commit 13a644531f

View file

@ -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 @@ 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 '<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)