Skip to content

Instantly share code, notes, and snippets.

@durple
Created April 27, 2014 06:12
Show Gist options
  • Select an option

  • Save durple/11338751 to your computer and use it in GitHub Desktop.

Select an option

Save durple/11338751 to your computer and use it in GitHub Desktop.
A better javascript parser for Twitter Entities
/*
* better-twitter-entities.js
* This function converts a tweet with "entity" metadata
* from plain text to linkified HTML.
*
* See the documentation here: http://dev.twitter.com/pages/tweet_entities
* Basically, add ?include_entities=true to your timeline call
* This is an extension of Wade Simmons' twitter-entities.js gist
* https://gist.github.com/wadey/442463
*
* Copyright 2014, Deep Kapadia
* Licensed under the MIT license
* http://wades.im/mons
*
* Requires jQuery
*/
function linkify_entities(tweet) {
if (!(tweet.entities)) {
return tweet.text
}
// This is very naive, should find a better way to parse this
var index_map = {}
// Fix retweets
offset = 0
if ('retweeted_status' in tweet) {
tweet.text = "RT @"+tweet.entities.user_mentions[0].screen_name+": "+tweet.retweeted_status.text;
offset = tweet.entities.user_mentions[0].indices[1]+2
urls = [];
for (var i=0; i < tweet.retweeted_status.entities.urls.length; i++) {
url = tweet.retweeted_status.entities.urls[i];
url.indices[0] = url.indices[0]+offset;
url.indices[1] = url.indices[1]+offset;
urls.push(url);
}
tweet.entities.urls = urls
hashtags = [];
for (var i=0; i < tweet.retweeted_status.entities.hashtags.length; i++) {
hashtag = tweet.retweeted_status.entities.hashtags[i];
hashtag.indices[0] = hashtag.indices[0]+offset;
hashtag.indices[1] = hashtag.indices[1]+offset;
hashtags.push(hashtag);
}
tweet.entities.hashtags = hashtags
user_mentions = [tweet.entities.user_mentions[0]]; // tricky!!
for (var i=0; i < tweet.retweeted_status.entities.user_mentions.length; i++) {
user_mention = tweet.retweeted_status.entities.user_mentions[i];
user_mention.indices[0] = user_mention.indices[0]+offset;
user_mention.indices[1] = user_mention.indices[1]+offset;
user_mentions.push(user_mention);
}
tweet.entities.user_mentions = user_mentions
}
$.each(tweet.entities.urls, function(i,entry) {
index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a target='_blank' href='"+entry.url+"'>"+entry.display_url+"</a>"}]
})
$.each(tweet.entities.hashtags, function(i,entry) {
index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a target='_blank' href='http://twitter.com/search?q="+escape("#"+entry.text)+"'>"+text+"</a>"}]
})
$.each(tweet.entities.user_mentions, function(i,entry) {
index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a target='_blank' title='"+entry.name+"' href='http://twitter.com/"+entry.screen_name+"'>"+text+"</a>"}]
})
var result = ""
var last_i = 0
var i = 0
// iterate through the string looking for matches in the index_map
for (i=0; i < tweet.text.length; ++i) {
var ind = index_map[i]
if (ind) {
var end = ind[0]
var func = ind[1]
if (i > last_i) {
result += tweet.text.substring(last_i, i)
}
result += func(tweet.text.substring(i, end))
i = end - 1
last_i = end
}
}
if (i > last_i) {
result += tweet.text.substring(last_i, i)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment