Created
February 24, 2011 04:58
-
-
Save dylan-evans/841785 to your computer and use it in GitHub Desktop.
Tweet processor, inserts link markup for users and hashtags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tweet processor, inserts link markup for users and hashtags | |
# I became frustrated with third party tools which do not link to users and hashtags, so i wrote this. | |
# Public Domain - please use this, i am happy to rewrite it in any other language. | |
# Dylan Evans <[email protected]> | |
import re | |
def html_hashtag(tag, html_class='hashtag'): | |
"HTML hashtag - create a search link" | |
return '<a class="%s" href="http://www.twitter.com/search/%s">%s</a>' \ | |
% (html_class, tag, tag) | |
# Probably should encode the hashtag with urlib.qoute, | |
# but it looks better like this | |
def html_user(user, html_class='twitterer'): | |
"HTML user name - return a link to the specified user" | |
return '<a class="%s" href="http://www.twitter.com/%s">%s</a>' \ | |
% (html_class, user, user) | |
def process(tweet, ht=html_hashtag, user=html_user): | |
"""Process a tweet into a marked up string. | |
Markup is provided by the functions passed in the ht and user | |
arguments, html defaults are provided. | |
""" | |
s = tweet.split() | |
s = re.split('(\s*)', tweet) | |
buf = "" | |
for item in s: | |
if item[0] == '#': | |
buf += ht(item) | |
elif item[0] == '@': | |
buf += user(item) | |
else: | |
buf += item | |
return buf | |
if __name__ == '__main__': | |
print process("I have a #hashtag for @you \n\t.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment