def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
 trim_url = (lambda x, limit=trim_url_limit: (((limit is not None) and ((len(x) > limit) and ('%s...' % x[:max(0, (limit - 3))]))) or x))
 safe_input = isinstance(text, SafeData)
 words = word_split_re.split(force_unicode(text))
 for (i, word) in enumerate(words):
  match = None
  if (('.' in word) or ('@' in word) or (':' in word)):
   (lead, middle, trail) = ('', word, '')
   for punctuation in TRAILING_PUNCTUATION:
    if middle.endswith(punctuation):
     middle = middle[:(- len(punctuation))]
     trail = (punctuation + trail)
   for (opening, closing) in WRAPPING_PUNCTUATION:
    if middle.startswith(opening):
     middle = middle[len(opening):]
     lead = (lead + opening)
    if (middle.endswith(closing) and (middle.count(closing) == (middle.count(opening) + 1))):
     middle = middle[:(- len(closing))]
     trail = (closing + trail)
   url = None
   nofollow_attr = (' rel="nofollow"' if nofollow else '')
   if simple_url_re.match(middle):
    url = smart_urlquote(middle)
   elif simple_url_2_re.match(middle):
    url = smart_urlquote(('http://%s' % middle))
   elif ((not (':' in middle)) and simple_email_re.match(middle)):
    (local, domain) = middle.rsplit('@', 1)
    try:
     domain = domain.encode('idna')
    except UnicodeError:
     continue
    url = ('mailto:%s@%s' % (local, domain))
    nofollow_attr = ''
   if url:
    trimmed = trim_url(middle)
    if (autoescape and (not safe_input)):
     (lead, trail) = (escape(lead), escape(trail))
     (url, trimmed) = (escape(url), escape(trimmed))
    middle = ('<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed))
    words[i] = mark_safe(('%s%s%s' % (lead, middle, trail)))
   elif safe_input:
    words[i] = mark_safe(word)
   elif autoescape:
    words[i] = escape(word)
  elif safe_input:
   words[i] = mark_safe(word)
  elif autoescape:
   words[i] = escape(word)
 return u''.join(words)

def urlize ( text , trim_url_limit = None , nofollow = False , autoescape = False ) : trim_url = ( lambda x , limit = trim_url_limit : ( ( ( limit is not None ) and ( ( len ( x ) > limit ) and ( '%s...' % x [ : max ( 0 , ( limit - 3 ) ) ] ) ) ) or x ) ) safe_input = isinstance ( text , SafeData ) words = word_split_re . split ( force_unicode ( text ) ) for ( i , word ) in enumerate ( words ) : match = None if ( ( '.' in word ) or ( '@' in word ) or ( ':' in word ) ) : ( lead , middle , trail ) = ( '' , word , '' ) for punctuation in TRAILING_PUNCTUATION : if middle . endswith ( punctuation ) : middle = middle [ : ( - len ( punctuation ) ) ] trail = ( punctuation + trail ) for ( opening , closing ) in WRAPPING_PUNCTUATION : if middle . startswith ( opening ) : middle = middle [ len ( opening ) : ] lead = ( lead + opening ) if ( middle . endswith ( closing ) and ( middle . count ( closing ) == ( middle . count ( opening ) + 1 ) ) ) : middle = middle [ : ( - len ( closing ) ) ] trail = ( closing + trail ) url = None nofollow_attr = ( ' rel="nofollow"' if nofollow else '' ) if simple_url_re . match ( middle ) : url = smart_urlquote ( middle ) elif simple_url_2_re . match ( middle ) : url = smart_urlquote ( ( 'http://%s' % middle ) ) elif ( ( not ( ':' in middle ) ) and simple_email_re . match ( middle ) ) : ( local , domain ) = middle . rsplit ( '@' , 1 ) try : domain = domain . encode ( 'idna' ) except UnicodeError : continue url = ( 'mailto:%s@%s' % ( local , domain ) ) nofollow_attr = '' if url : trimmed = trim_url ( middle ) if ( autoescape and ( not safe_input ) ) : ( lead , trail ) = ( escape ( lead ) , escape ( trail ) ) ( url , trimmed ) = ( escape ( url ) , escape ( trimmed ) ) middle = ( '<a href="%s"%s>%s</a>' % ( url , nofollow_attr , trimmed ) ) words [ i ] = mark_safe ( ( '%s%s%s' % ( lead , middle , trail ) ) ) elif safe_input : words [ i ] = mark_safe ( word ) elif autoescape : words [ i ] = escape ( word ) elif safe_input : words [ i ] = mark_safe ( word ) elif autoescape : words [ i ] = escape ( word ) return u'' . join ( words )
converts urls in plain text into clickable links .