def ultralize(text, mini=False,trim_url_limit=None, nofollow=False, autoescape=False): text = unicodedata.normalize('NFKD', text).encode('ascii','ignore') 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)) nofollow_attr = nofollow and ' rel="nofollow"' or '' has_stuff = False for i, word in enumerate(words): if word == "fun1": word = "http://bleachexile.com/exec/images/emoticons/th_funone3kk.gif" match = None if '.' in word or '@' in word or ':' in word: match = punctuation_re.match(word) if match: lead, middle, trail = match.groups() # Make URL we want to point to. url = None if middle.startswith('http://') or middle.startswith('https://'): url = urlquote(middle, safe='/&=:;#?+*') elif middle.startswith('www.') or ('@' not in middle and \ middle and middle[0] in string.ascii_letters + string.digits and \ (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))): url = urlquote('http://%s' % middle, safe='/&=:;#?+*') elif '@' in middle and not ':' in middle and simple_email_re.match(middle): url = 'mailto:%s' % middle nofollow_attr = '' # Make link. if url: trimmed = trim_url(middle) ytregex = re.compile(r"^(http://)?(www\.)?(youtube\.com/watch\?v=)(?P[A-Za-z0-9\-=_]{11})") vimeoregex = re.compile(r"^(http://)?(www\.)?(vimeo\.com/)(?P\d+)") dmregex = re.compile(r"^(http://)?(www\.)?(dailymotion\.com/video/)(?P[A-Za-z0-9]+)") ytmatch = ytregex.match(middle) vimeomatch = vimeoregex.match(middle) dmmatch = dmregex.match(middle) if ytmatch: video_id = ytmatch.group('id') if vimeomatch: video_id = vimeomatch.group('id') if dmmatch: video_id = dmmatch.group('id') if autoescape and not safe_input: lead, trail = escape(lead), escape(trail) url, trimmed = escape(url), escape(trimmed) if any(s in middle for s in ['.jpg', '.JPG', '.gif', '.GIF', '.bmp', '.BMP', '.png', '.PNG', '.jpeg', '.JPEG', 'gstatic.com/images']): if has_stuff and mini: middle = '' else: if mini: middle = '' % (url, url) else: middle = '' % (url, url) has_stuff = True elif middle.startswith("http://www.youtube.com/watch?v=") or middle.startswith("www.youtube.com/watch?v="): if ytmatch: if has_stuff and mini: middle = '' else: middle = "" % (video_id + "?autohide=1&wmode=transparent") has_stuff = True elif middle.startswith("http://www.vimeo.com") or middle.startswith("www.vimeo.com") or middle.startswith("vimeo.com") or middle.startswith("http://vimeo.com"): if vimeomatch: if has_stuff and mini: middle = '' else: middle = "" % (video_id) has_stuff = True elif middle.startswith("http://www.dailymotion.com") or middle.startswith("www.dailymotion.com") or middle.startswith("dailymotion.com") or middle.startswith("http://dailymotion.com"): if dmmatch: if has_stuff and mini: middle = '' else: middle = "" % (video_id) has_stuff = True elif middle.endswith(".ogg") or middle.endswith(".mp3"): if has_stuff and mini: middle = '' else: middle = "" % (url) has_stuff = True else: if has_stuff and mini: middle = '' else: middle = "%s" % (url, nofollow_attr, trimmed) has_stuff = True words[i] = mark_safe('%s%s%s' % (lead, middle, trail)) else: if 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 mark_safe(u''.join(words)) ultralize.is_safe=True ultralize.needs_autoescape = True ultralize = stringfilter(ultralize) register.filter(ultralize) ultralize = allow_lazy(ultralize, unicode)