Skip to content

Instantly share code, notes, and snippets.

@topliceanu
Created February 19, 2013 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save topliceanu/4986214 to your computer and use it in GitHub Desktop.
Save topliceanu/4986214 to your computer and use it in GitHub Desktop.
method to extract transform users and links in tweets into actual links
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