# Path: django_toolkit/url/shorten.py
# def shorten_url(url, length=32, strip_www=True, strip_path=True, ellipsis=False):
#     """
#     Shorten a URL by chopping out the middle.
# 
#     For example if supplied with http://subdomain.example.com.au and length 16
#     the following would be returned.
# 
#     sub...le.com.au
#     """
#     if '://' not in url:
#         # Ensure we have a protocol
#         url = 'http://%s' % url
# 
#     parsed_url = urlparse(url)
#     ext = tldextract.extract(parsed_url.netloc)
#     if ext.subdomain and (not strip_www or (strip_www and ext.subdomain != 'www')):
#         shortened = u'%s.%s' % (ext.subdomain, ext.domain)
#     else:
#         shortened = u'%s' % ext.domain
# 
#     if ext.tld:
#         shortened = u'%s.%s' % (shortened, ext.tld)
# 
#     if not strip_path:
#         if parsed_url.path:
#             shortened += parsed_url.path
# 
#     if len(shortened) <= length:
#         return shortened
#     else:
#         domain = shortened
#         i = length + 1 - 3
#         left = right = int(i/2)
#         if not i % 2:
#             right -= 1
#         if ellipsis:
#             shortened = u"%s\u2026%s" % (domain[:left], domain[-right:])
#         else:
#             shortened = '%s...%s' % (domain[:left], domain[-right:])
# 
#         return shortened
# 
# def netloc_no_www(url):
#     """
#     For a given URL return the netloc with any www. striped.
#     """
#     ext = tldextract.extract(url)
#     if ext.subdomain and ext.subdomain != 'www':
#         return '%s.%s.%s' % (ext.subdomain, ext.domain, ext.tld) 
#     else:
#         return '%s.%s' % (ext.domain, ext.tld)


from django.utils import unittest
from django_toolkit.url.shorten import shorten_url, netloc_no_www
        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexample.com.au/path/to/strip/', 16), 'areally...com.au')

    def test_large_even(self):
        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexamplee.com.au', 16), 'areally...com.au')

    def test_large_odd_length(self):
        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexamplee.com.au', 17), 'areally....com.au')

    def test_tld(self):
        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexamplee.com', 32), 'areallylongsubd...ngexamplee.com')

    def test_no_tld(self):
        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexampleecom12', 32), 'areallylongsubd...gexampleecom12')

    def test_no_www(self):
        self.assertEquals(self.shorten_url('www.example.com', 16), 'example.com')

    def test_www(self):
        self.assertEquals(self.shorten_url('www.example.com', 16, strip_www=False), 'www.example.com')

    def test_with_path(self):
        self.assertEquals(self.shorten_url('www.example.com/au', 32, strip_www=False, strip_path=False), 'www.example.com/au')
        self.assertEquals(self.shorten_url('www.example.com/au', 16, strip_path=False), 'example.com/au')
        self.assertEquals(self.shorten_url('www.example.com/au', 16, strip_www=False, strip_path=False), 'www.exa...com/au')
        self.assertEquals(self.shorten_url('www.example.com/au/page/n/', 16, strip_www=False, strip_path=False), 'www.exa...age/n/')
        self.assertEquals(self.shorten_url('www.example.com.au/au/page/n/', 16, strip_www=False, strip_path=False), 'www.exa...age/n/')
        self.assertEquals(self.shorten_url('http://www.example.com.au/au/page/n/', 32, strip_www=False, strip_path=False), 'www.example.com.au/au/page/n/')
        self.assertEquals(self.shorten_url('http://www.example.com.au/au/page/n/', 24, strip_www=False, strip_path=False), 'www.example...au/page/n/')

    def test_ellipsis(self):

# Path: django_toolkit/url/shorten.py
# def shorten_url(url, length=32, strip_www=True, strip_path=True, ellipsis=False):
#     """
#     Shorten a URL by chopping out the middle.
# 
#     For example if supplied with http://subdomain.example.com.au and length 16
#     the following would be returned.
# 
#     sub...le.com.au
#     """
#     if '://' not in url:
#         # Ensure we have a protocol
#         url = 'http://%s' % url
# 
#     parsed_url = urlparse(url)
#     ext = tldextract.extract(parsed_url.netloc)
#     if ext.subdomain and (not strip_www or (strip_www and ext.subdomain != 'www')):
#         shortened = u'%s.%s' % (ext.subdomain, ext.domain)
#     else:
#         shortened = u'%s' % ext.domain
# 
#     if ext.tld:
#         shortened = u'%s.%s' % (shortened, ext.tld)
# 
#     if not strip_path:
#         if parsed_url.path:
#             shortened += parsed_url.path
# 
#     if len(shortened) <= length:
#         return shortened
#     else:
#         domain = shortened
#         i = length + 1 - 3
#         left = right = int(i/2)
#         if not i % 2:
#             right -= 1
#         if ellipsis:
#             shortened = u"%s\u2026%s" % (domain[:left], domain[-right:])
#         else:
#             shortened = '%s...%s' % (domain[:left], domain[-right:])
# 
#         return shortened
# 
# def netloc_no_www(url):
#     """
#     For a given URL return the netloc with any www. striped.
#     """
#     ext = tldextract.extract(url)
#     if ext.subdomain and ext.subdomain != 'www':
#         return '%s.%s.%s' % (ext.subdomain, ext.domain, ext.tld) 
#     else:
#         return '%s.%s' % (ext.domain, ext.tld)


# Path: django_toolkit/tests/url/shorten.py
from django.utils import unittest
from django_toolkit.url.shorten import shorten_url, netloc_no_www

        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexample.com.au/path/to/strip/', 16), 'areally...com.au')

    def test_large_even(self):
        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexamplee.com.au', 16), 'areally...com.au')

    def test_large_odd_length(self):
        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexamplee.com.au', 17), 'areally....com.au')

    def test_tld(self):
        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexamplee.com', 32), 'areallylongsubd...ngexamplee.com')

    def test_no_tld(self):
        self.assertEquals(self.shorten_url('areallylongsubdomain.areallylongexampleecom12', 32), 'areallylongsubd...gexampleecom12')

    def test_no_www(self):
        self.assertEquals(self.shorten_url('www.example.com', 16), 'example.com')

    def test_www(self):
        self.assertEquals(self.shorten_url('www.example.com', 16, strip_www=False), 'www.example.com')

    def test_with_path(self):
        self.assertEquals(self.shorten_url('www.example.com/au', 32, strip_www=False, strip_path=False), 'www.example.com/au')
        self.assertEquals(self.shorten_url('www.example.com/au', 16, strip_path=False), 'example.com/au')
        self.assertEquals(self.shorten_url('www.example.com/au', 16, strip_www=False, strip_path=False), 'www.exa...com/au')
        self.assertEquals(self.shorten_url('www.example.com/au/page/n/', 16, strip_www=False, strip_path=False), 'www.exa...age/n/')
        self.assertEquals(self.shorten_url('www.example.com.au/au/page/n/', 16, strip_www=False, strip_path=False), 'www.exa...age/n/')
        self.assertEquals(self.shorten_url('http://www.example.com.au/au/page/n/', 32, strip_www=False, strip_path=False), 'www.example.com.au/au/page/n/')
        self.assertEquals(self.shorten_url('http://www.example.com.au/au/page/n/', 24, strip_www=False, strip_path=False), 'www.example...au/page/n/')

    def test_ellipsis(self):

        self.assertEquals(self.shorten_url('www.example.com/au', 32, strip_www=False, strip_path=False, ellipsis=True), 'www.example.com/au')