Skip to content

Instantly share code, notes, and snippets.

@victorono
Created January 12, 2014 15:49
Show Gist options
  • Save victorono/8386264 to your computer and use it in GitHub Desktop.
Save victorono/8386264 to your computer and use it in GitHub Desktop.
filter tweet django templatetag
#!/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