Skip to content

Instantly share code, notes, and snippets.

@bryanjswift
Forked from wadey/twitter-entities.js
Created August 19, 2012 02:31
Show Gist options
  • Select an option

  • Save bryanjswift/3391111 to your computer and use it in GitHub Desktop.

Select an option

Save bryanjswift/3391111 to your computer and use it in GitHub Desktop.

Revisions

  1. bryanjswift revised this gist Oct 19, 2012. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions twitter-entities.js
    Original file line number Diff line number Diff line change
    @@ -14,20 +14,20 @@
    */
    define(['underscore'], function(_) {

    function escapeHTML(text) {
    return _.escape(text);
    }

    function linkify_entities(tweet) {
    if (!(tweet.entities)) { return escapeHTML(tweet.text); }
    if (!(tweet.entities)) { return _.escape(tweet.text); }

    // This is very naive, should find a better way to parse this
    var index_map = {};

    _.each(tweet.entities.urls, function(entry, i) {
    index_map[entry.indices[0]] = [
    entry.indices[1],
    function(text) { return "<a href='"+escapeHTML(entry.url)+"'>"+escapeHTML(text)+"</a>"; }
    function(text) {
    var url = _.escape(entry.url);
    var t = _.escape(text);
    return "<a href='" + url + "'>" + t + "</a>";
    }
    ];
    });

    @@ -36,7 +36,7 @@ define(['underscore'], function(_) {
    entry.indices[1],
    function(text) {
    var et = escape("#"+entry.text);
    return "<a href='http://twitter.com/search?q=" + et + "'>" + escapeHTML(text) + "</a>";
    return "<a href='http://twitter.com/search?q=" + et + "'>" + _.escape(text) + "</a>";
    }
    ];
    });
    @@ -45,9 +45,9 @@ define(['underscore'], function(_) {
    index_map[entry.indices[0]] = [
    entry.indices[1],
    function(text) {
    var name = escapeHTML(entry.name);
    var sn = escapeHTML(entry.screen_name);
    var t = escapeHTML(text);
    var name = _.escape(entry.name);
    var sn = _.escape(entry.screen_name);
    var t = _.escape(text);
    return "<a title='" + name + "' href='http://twitter.com/" + sn + "'>" + t + "</a>";
    }
    ];
    @@ -64,7 +64,7 @@ define(['underscore'], function(_) {
    var end = ind[0];
    var func = ind[1];
    if (i > last_i) {
    result += escapeHTML(tweet.text.substring(last_i, i));
    result += _.escape(tweet.text.substring(last_i, i));
    }
    result += func(tweet.text.substring(i, end));
    i = end - 1;
    @@ -73,12 +73,12 @@ define(['underscore'], function(_) {
    }

    if (i > last_i) {
    result += escapeHTML(tweet.text.substring(last_i, i));
    result += _.escape(tweet.text.substring(last_i, i));
    }

    return result;
    }

    return linkify_entities;

    });
    });
  2. bryanjswift revised this gist Oct 19, 2012. 1 changed file with 36 additions and 19 deletions.
    55 changes: 36 additions & 19 deletions twitter-entities.js
    Original file line number Diff line number Diff line change
    @@ -19,44 +19,61 @@ define(['underscore'], function(_) {
    }

    function linkify_entities(tweet) {
    if (!(tweet.entities)) { return escapeHTML(tweet.text) }
    if (!(tweet.entities)) { return escapeHTML(tweet.text); }

    // This is very naive, should find a better way to parse this
    var index_map = {}
    var index_map = {};

    _.each(tweet.entities.urls, function(entry, i) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a href='"+escapeHTML(entry.url)+"'>"+escapeHTML(text)+"</a>"}]
    })
    index_map[entry.indices[0]] = [
    entry.indices[1],
    function(text) { return "<a href='"+escapeHTML(entry.url)+"'>"+escapeHTML(text)+"</a>"; }
    ];
    });

    _.each(tweet.entities.hashtags, function(entry, i) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a href='http://twitter.com/search?q="+escape("#"+entry.text)+"'>"+escapeHTML(text)+"</a>"}]
    })
    index_map[entry.indices[0]] = [
    entry.indices[1],
    function(text) {
    var et = escape("#"+entry.text);
    return "<a href='http://twitter.com/search?q=" + et + "'>" + escapeHTML(text) + "</a>";
    }
    ];
    });

    _.each(tweet.entities.user_mentions, function(entry, i) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a title='"+escapeHTML(entry.name)+"' href='http://twitter.com/"+escapeHTML(entry.screen_name)+"'>"+escapeHTML(text)+"</a>"}]
    })
    index_map[entry.indices[0]] = [
    entry.indices[1],
    function(text) {
    var name = escapeHTML(entry.name);
    var sn = escapeHTML(entry.screen_name);
    var t = escapeHTML(text);
    return "<a title='" + name + "' href='http://twitter.com/" + sn + "'>" + t + "</a>";
    }
    ];
    });

    var result = ""
    var last_i = 0
    var i = 0
    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]
    var ind = index_map[i];
    if (ind) {
    var end = ind[0]
    var func = ind[1]
    var end = ind[0];
    var func = ind[1];
    if (i > last_i) {
    result += escapeHTML(tweet.text.substring(last_i, i))
    result += escapeHTML(tweet.text.substring(last_i, i));
    }
    result += func(tweet.text.substring(i, end))
    i = end - 1
    last_i = end
    result += func(tweet.text.substring(i, end));
    i = end - 1;
    last_i = end;
    }
    }

    if (i > last_i) {
    result += escapeHTML(tweet.text.substring(last_i, i))
    result += escapeHTML(tweet.text.substring(last_i, i));
    }

    return result;
  3. bryanjswift revised this gist Aug 19, 2012. 1 changed file with 30 additions and 27 deletions.
    57 changes: 30 additions & 27 deletions twitter-entities.js
    Original file line number Diff line number Diff line change
    @@ -10,31 +10,30 @@
    * Licensed under the MIT license
    * http://wades.im/mons
    *
    * Requires jQuery
    * Modified by Bryan J Swift to define as AMD module and use underscore.js
    */
    define(['underscore'], function(_) {

    function escapeHTML(text) {
    return $('<div/>').text(text).html()
    }
    function escapeHTML(text) {
    return _.escape(text);
    }

    function linkify_entities(tweet) {
    if (!(tweet.entities)) {
    return escapeHTML(tweet.text)
    }
    function linkify_entities(tweet) {
    if (!(tweet.entities)) { return escapeHTML(tweet.text) }

    // This is very naive, should find a better way to parse this
    var index_map = {}

    $.each(tweet.entities.urls, function(i,entry) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a href='"+escapeHTML(entry.url)+"'>"+escapeHTML(text)+"</a>"}]
    _.each(tweet.entities.urls, function(entry, i) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a href='"+escapeHTML(entry.url)+"'>"+escapeHTML(text)+"</a>"}]
    })

    $.each(tweet.entities.hashtags, function(i,entry) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a href='http://twitter.com/search?q="+escape("#"+entry.text)+"'>"+escapeHTML(text)+"</a>"}]
    _.each(tweet.entities.hashtags, function(entry, i) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a href='http://twitter.com/search?q="+escape("#"+entry.text)+"'>"+escapeHTML(text)+"</a>"}]
    })

    $.each(tweet.entities.user_mentions, function(i,entry) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a title='"+escapeHTML(entry.name)+"' href='http://twitter.com/"+escapeHTML(entry.screen_name)+"'>"+escapeHTML(text)+"</a>"}]
    _.each(tweet.entities.user_mentions, function(entry, i) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a title='"+escapeHTML(entry.name)+"' href='http://twitter.com/"+escapeHTML(entry.screen_name)+"'>"+escapeHTML(text)+"</a>"}]
    })

    var result = ""
    @@ -43,22 +42,26 @@ function linkify_entities(tweet) {

    // 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 += escapeHTML(tweet.text.substring(last_i, i))
    }
    result += func(tweet.text.substring(i, end))
    i = end - 1
    last_i = end
    var ind = index_map[i]
    if (ind) {
    var end = ind[0]
    var func = ind[1]
    if (i > last_i) {
    result += escapeHTML(tweet.text.substring(last_i, i))
    }
    result += func(tweet.text.substring(i, end))
    i = end - 1
    last_i = end
    }
    }

    if (i > last_i) {
    result += escapeHTML(tweet.text.substring(last_i, i))
    result += escapeHTML(tweet.text.substring(last_i, i))
    }

    return result
    }
    return result;
    }

    return linkify_entities;

    });
  4. @wadey wadey revised this gist Jun 17, 2010. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions twitter-entities.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    /*
    * 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
    *
    * Copyright 2010, Wade Simmons
    * Licensed under the MIT license
  5. @invalid-email-address Anonymous created this gist Jun 17, 2010.
    59 changes: 59 additions & 0 deletions twitter-entities.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    /*
    * twitter-entities.js
    *
    * Copyright 2010, Wade Simmons
    * Licensed under the MIT license
    * http://wades.im/mons
    *
    * Requires jQuery
    */

    function escapeHTML(text) {
    return $('<div/>').text(text).html()
    }

    function linkify_entities(tweet) {
    if (!(tweet.entities)) {
    return escapeHTML(tweet.text)
    }

    // This is very naive, should find a better way to parse this
    var index_map = {}

    $.each(tweet.entities.urls, function(i,entry) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a href='"+escapeHTML(entry.url)+"'>"+escapeHTML(text)+"</a>"}]
    })

    $.each(tweet.entities.hashtags, function(i,entry) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a href='http://twitter.com/search?q="+escape("#"+entry.text)+"'>"+escapeHTML(text)+"</a>"}]
    })

    $.each(tweet.entities.user_mentions, function(i,entry) {
    index_map[entry.indices[0]] = [entry.indices[1], function(text) {return "<a title='"+escapeHTML(entry.name)+"' href='http://twitter.com/"+escapeHTML(entry.screen_name)+"'>"+escapeHTML(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 += escapeHTML(tweet.text.substring(last_i, i))
    }
    result += func(tweet.text.substring(i, end))
    i = end - 1
    last_i = end
    }
    }

    if (i > last_i) {
    result += escapeHTML(tweet.text.substring(last_i, i))
    }

    return result
    }