Created
January 12, 2014 15:49
-
-
Save victorono/8386264 to your computer and use it in GitHub Desktop.
filter tweet django templatetag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import re | |
from django.utils.safestring import mark_safe | |
from django import template | |
register = template.Library() | |
@register.filter(name='twitter_tags') | |
def twitter_tags(value): | |
value = re.sub(r'((mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)', '<a href="\g<0>" rel="external">\g<0></a>', value) | |
value = re.sub(r'#(?P<hashtag>\w+)', '<a href="https://twitter.com/search?q=\g<hashtag>&src=typd&f=realtime" rel="external">#\g<hashtag></a>', value) | |
value = re.sub(r'@(?P<username>\w+)', '@<a href="https://twitter.com/\g<username>/" rel="external">\g<username></a>', value) | |
return mark_safe(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment