Login

@reply links and hash tags in Twitter messages

Author:
dougal
Posted:
February 28, 2009
Language:
Python
Version:
1.0
Score:
3 (after 3 ratings)

Adapted from #848 to basically copy the reply tag and create it again as a hash tag filter.

Kudos to ryanberg, not me.

will be in use on my website soon (www.dougalmatthews.com) for a demo.

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

#

Please login first before commenting.