@register.filter def youtubize(value): """ Converts http:// links to youtube into youtube-embed statements, so that one can provide a simple link to a youtube video and this filter will embed it. Based on the Django urlize filter. """ text = value # Configuration for urlize() function LEADING_PUNCTUATION = ['(', '<', '<'] TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '>'] word_split_re = re.compile(r'(\s+)') punctuation_re = re.compile('^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % \ ('|'.join([re.escape(x) for x in LEADING_PUNCTUATION]), '|'.join([re.escape(x) for x in TRAILING_PUNCTUATION]))) youtube_re = re.compile ('http://www.youtube.com/watch.v=(?P<videoid>(.+))') words = word_split_re.split(text) for i, word in enumerate(words): match = punctuation_re.match(word) if match: lead, middle, trail = match.groups() if middle.startswith('http://www.youtube.com/watch') or middle.startswith('http://youtube.com/watch'): video_match = youtube_re.match(middle) if video_match: video_id = video_match.groups()[1] middle = '''<object width="425" height="350"> <param name="movie" value="http://www.youtube.com/v/%s"/> <param name="wmode" value="transparent"/> <embed src="http://www.youtube.com/v/%s" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"/> </object>''' % (video_id, video_id) if lead + middle + trail != word: words[i] = lead + middle + trail return ''.join(words)