twitterize filter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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<a href="http://twitter.com/\2">@\2</a>',value)
	# Link hash tags
	value = re.sub(r'(\s+|\A)#([a-zA-Z0-9\-_]*)\b',r'\1<a href="http://search.twitter.com/search?q=%23\2">#\2</a>',value)
	return mark_safe(value)
twitterize.is_safe=True
twitterize.needs_autoescape = True

More like this

  1. @reply links and hash tags in Twitter messages by dougal 5 years, 1 month ago
  2. Twitterfy by dougal 4 years, 7 months ago
  3. Template filter to turn Twitter names into links by mrben_ 3 years, 9 months ago
  4. Twitter template tags and filters by moxypark 3 years, 8 months ago
  5. twitter_status by zodman 5 years, 7 months ago

Comments

dfrankow (on December 23, 2010):

This will let through arbitrary HTML, hence is open to attacks.

#

dfrankow (on December 23, 2010):

Oops, not sure about that..

#

(Forgotten your password?)