urls.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #-*- coding: utf-8 -*-
  2. import re
  3. from urlparse import urlparse
  4. from django.conf import settings
  5. from django.core.urlresolvers import resolve
  6. from django.http import Http404
  7. from misago.utils.strings import html_escape
  8. URL_RE = re.compile(r'^(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))$', re.UNICODE)
  9. def is_url(string):
  10. return URL_RE.search(string.strip()) != None
  11. def is_inner(string):
  12. try:
  13. resolve(string.strip())
  14. return True
  15. except Http404:
  16. return False
  17. def clean_inner(string):
  18. parsed = urlparse(string.strip())
  19. href = parsed.path
  20. if parsed.query:
  21. href += '?%s' % parsed.query
  22. if parsed.fragment:
  23. href += '#%s' % parsed.fragment
  24. return html_escape(href)
  25. def clean_outer(string):
  26. parsed = urlparse(string.strip())
  27. if not parsed.scheme:
  28. return 'http://%s' % string
  29. return string