Login

@reply links in Twitter messages

Author:
ryanberg
Posted:
July 2, 2008
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

This template filter is rewritten, courtesy of Eric Moritz.

It is meant to be used when displaying status messages from Twitter. A regular expression is used to replace all @username replies with a link to that user's Twitter page.

In use at http://ryanberg.net/blog/statuses/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Template tags file

from django.utils.safestring import mark_safe
import re

@register.filter(name='at_reply')
def twitter_at_reply(tweet):
    pattern = re.compile(r"(\A|\W)@(?P<user>\w+)(\Z|\W)")
    repl = (r'\1@<a href="http://twitter.com/\g<user>"'
    r' title="\g<user> on Twitter">\g<user></a>\3')
	
    return mark_safe(pattern.sub(repl, tweet))

# Template usage
{% load TAG_LIBRARY_NAME %}
{{ status.status|at_reply }}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

ericmoritz (on July 3, 2008):

Thanks for the props! It looks awesome in action.

#

Please login first before commenting.