@reply links and hash tags in Twitter messages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# OBSOLETED BY http://djangosnippets.org/snippets/1738/

from django.utils.safestring import mark_safe
from django import template

import re

register = template.Library()

@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))


@register.filter(name='hash_tag')
def twitter_hash_tag(tweet):
    pattern = re.compile(r"(\A|\W)#(?P<hashtag>[A-Za-z]+)(\Z|\W)")
    repl = (r'\1#<a href="http://search.twitter.com/search?q=\g<hashtag>"'
    r' title="#\g<hashtag> search Twitter">\g<hashtag></a>\3')
    
    return mark_safe(pattern.sub(repl, tweet))

More like this

  1. twitterize filter by thomasw 2 years, 9 months ago
  2. Twitterfy by dougal 2 years, 4 months ago
  3. @reply links in Twitter messages by ryanberg 3 years, 7 months ago
  4. twitter_status by zodman 3 years, 5 months ago
  5. Template tag: Last x twitter msgs of user by coulix 2 years, 7 months ago

Comments

dougal (on April 29, 2009):

This needs to be adapted for the case of more than one user name or hashtag in the tweet. not had time to do it yet...

#

(Forgotten your password?)