# HG changeset patch # User hamidouk # Date 1321869082 -3600 # Node ID b7aa28af2c1033f62ae7e88bd0eb1dccc027eb29 # Parent cbb17aa3361c09de6d1319f103d3d07f9b28eea5 added a function to format a tweet. diff -r cbb17aa3361c -r b7aa28af2c10 src/js/utils.js --- a/src/js/utils.js Mon Nov 21 10:17:54 2011 +0100 +++ b/src/js/utils.js Mon Nov 21 10:51:22 2011 +0100 @@ -47,7 +47,18 @@ var seconds = parseFloat(Math.abs(secs % 60).toFixed(0)); return {"hours" : hours, "minutes" : minutes, "seconds" : seconds}; -} +}; + +/* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */ +IriSP.formatTweet = function(tweet) { + var rNickname = /@(\w+)/; // matches a @handle + var rHashtag = /#(\w+)/; // matches a hashtag + var i1 = tweet.replace(rNickname, "@$1"); + var i2 = i1.replace(rHashtag, "#$1"); + + return i2; +}; + /* for ie compatibility if (Object.prototype.__defineGetter__&&!Object.defineProperty) { Object.defineProperty=function(obj,prop,desc) { diff -r cbb17aa3361c -r b7aa28af2c10 src/js/widgets/tweetsWidget.js --- a/src/js/widgets/tweetsWidget.js Mon Nov 21 10:17:54 2011 +0100 +++ b/src/js/widgets/tweetsWidget.js Mon Nov 21 10:51:22 2011 +0100 @@ -13,7 +13,7 @@ IriSP.TweetsWidget.prototype.drawTweet = function(annotation) { - var title = annotation.content.title; + var title = IriSP.formatTweet(annotation.content.title); var img = annotation.content.img.src; if (typeof(img) === "undefined" || img === "" || img === "None") { img = IriSP.widgetsDefaults.TweetsWidget.default_profile_picture; @@ -22,7 +22,7 @@ var imageMarkup = Mustache.to_html("avatar", {src : img}); - this.selector.find(".Ldt-tweetContents").text(title); + this.selector.find(".Ldt-tweetContents").html(title); this.selector.find(".Ldt-tweetAvatar").html(imageMarkup); this.selector.show("blind", 250); }; diff -r cbb17aa3361c -r b7aa28af2c10 unittests/tests/utils.js --- a/unittests/tests/utils.js Mon Nov 21 10:17:54 2011 +0100 +++ b/unittests/tests/utils.js Mon Nov 21 10:51:22 2011 +0100 @@ -37,4 +37,10 @@ t = -t; deepEqual(IriSP.secondsToTime(t), {"hours" : h, "minutes" : m, "seconds" : s}, "the function is immune to negative numbers."); }); + + test("test function to format a tweet", function() { + var input = "@handle #hashtag "; + var output = "@handle #hashtag "; + equal(IriSP.formatTweet(input), output, "the correct output is given"); + }); } \ No newline at end of file