Created
February 19, 2013 14:07
-
-
Save topliceanu/4986214 to your computer and use it in GitHub Desktop.
method to extract transform users and links in tweets into actual links
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
parseTweet = (tweet) -> | |
### | |
This method parses a plain tweet text and outputs html | |
containing links for all @usernames, #hashtags and links in it. | |
@param {String} tweet | |
@param {String} - html output | |
### | |
links = [ | |
/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g | |
(url) -> | |
"<a href='#{url}'>#{url}</a>" | |
] | |
usernames = [ | |
/[@]+[A-Za-z0-9-_]+/g | |
(user) -> | |
user = user.substr 1 | |
"<a href='https://twitter.com/#{user}'>@#{user}</a>" | |
] | |
hashtags = [ | |
/[#]+[A-Za-z0-9-_]+/g | |
(hash) -> | |
hash = hash.substr 1 | |
"<a href='https://twitter.com/search?q=%23#{hash}'>##{hash}</a>" | |
] | |
unless _.isString tweet then return tweet | |
_.each [links, usernames, hashtags], ([regexp, callback]) -> | |
tweet = tweet.replace regexp, callback | |
tweet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment