Skip to content

Instantly share code, notes, and snippets.

@jed
Created April 21, 2011 10:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jed/934159 to your computer and use it in GitHub Desktop.
Save jed/934159 to your computer and use it in GitHub Desktop.
turning a tweet into html
function htmlify( tweet ) {
return tweet.replace(
/[@]+([A-Za-z0-9-_]+)|[#]+([A-Za-z0-9-_]+)|(\b(?:https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
function( match, user, tag, url ) {
return 0,
user ? "@" + user.link( "http://twitter.com/" + user ) :
tag ? "#" + tag.link( "http://search.twitter.com/search?q=%23" + tag ) :
url ? url.split( "//" )[ 1 ].link( url ) : ""
})
}
@madrobby
Copy link

madrobby commented Aug 6, 2011

fwiw, I found this in an old project of mine:

   // goes into String.prototype
   unescapeHTML: function() {
      return this.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
    },

   autolink: function(){
      var URL = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
      return this.gsub(URL, function(part){
        return '<a href="'+part[0].unescapeHTML()+'" target="_new">'+part[0]+'</a>';
      });
    }

@madrobby
Copy link

madrobby commented Aug 6, 2011

(oh, this assumes a gsub implementation like in Prototype)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment