from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter
@stringfilter
def twitterize(value, autoescape=None):
from django.utils.html import urlize
import re
# Link URLs
value = urlize(value, nofollow=False, autoescape=autoescape)
# Link twitter usernames prefixed with @
value = re.sub(r'(\s+|\A)@([a-zA-Z0-9\-_]*)\b',r'\1@\2',value)
# Link hash tags
value = re.sub(r'(\s+|\A)#([a-zA-Z0-9\-_]*)\b',r'\1#\2',value)
return mark_safe(value)
twitterize.is_safe=True
twitterize.needs_autoescape = True