Created
April 6, 2012 20:13
-
-
Save kara-ryli/2322623 to your computer and use it in GitHub Desktop.
Twitter entity processing made easy.
This file contains hidden or 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
/*global YUI*/ | |
/** | |
* Twitter entity processing made easy. | |
* @module twitter-processentities | |
*/ | |
YUI.add("twitter-processentities", function (Y) { | |
"use strict"; | |
var yArrayEach = Y.Array.each, | |
yLangSub = Y.Lang.sub, | |
startIndexSort = function (a, b) { | |
var aVal = a.indices[0], | |
bVal = b.indices[0]; | |
return aVal < bVal ? -1 : aVal > bVal ? 1 : 0; | |
}, | |
hash_tag_template = '<a class="hashtag" href="https://twitter.com/#!/search?q=%23{text}" target="_blank">#{text}</a>', | |
mention_template = '<a class="mention" href="https://twitter.com/#!/{screen_name}" target="_blank">{name}</a>', | |
url_template = '<a class="url" href="{expanded_url}" target="_blank">{display_url}</a>', | |
process = function (entity) { | |
var template = entity.text ? hash_tag_template : entity.url ? url_template : mention_template; | |
return yLangSub(template, entity); | |
}; | |
/** | |
* Process the entities within a tweet to render display text. | |
* | |
* Then entities object should contain three properties, all of which are arrays: | |
* <dl> | |
* <dt>hashtags</dt> | |
* <dd>An array of objects with a single key: text</dd> | |
* <dt>urls</dt> | |
* <dd>An array of objects with the keys <code>expanded_url</code> and <code>display_url</code></dd> | |
* <dt>mentions</dt> | |
* <dd>An array of objects with the keys <code>screen_name</code> and <code>name</code></dd> | |
* </dl> | |
* | |
* @method processEntities | |
* @namespace Twitter | |
* @static | |
* @param {String} text Text of the tweet | |
* @param {Object} entities Twitter entities. | |
* @return {String} Tweet HTML for display. | |
*/ | |
Y.namespace("Twitter").processEntities = function (text, entities) { | |
var all_entities, result_offset, result; | |
// combine all entities into a single array | |
all_entities = (entities.hashtags || []).concat(entities.urls || [], entities.user_mentions || [], entities.media || []); | |
// sort them by their start index | |
all_entities.sort(startIndexSort); | |
// start with the original text | |
result = text; | |
// store the difference in length between the original and the new string | |
result_offset = 0; | |
// loop through all entities | |
yArrayEach(all_entities, function (entity) { | |
// get the result text | |
var entity_result = process(entity), | |
// get the text we should replace | |
text_to_replace = result.substring(entity.indices[0] + result_offset, entity.indices[1] + result_offset); | |
// replace the entity text | |
result = result.replace(text_to_replace, entity_result); | |
// update the result_offset with the new difference in length | |
result_offset += entity_result.length - text_to_replace.length; | |
}); | |
// return the results | |
return result; | |
}; | |
}, "3.4.1"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you could simplify your code by sorting by descending end (rather than ascending start). You wouldn't need the result_offset anymore.