added a function to format a tweet. tweet-widget
authorhamidouk
Mon, 21 Nov 2011 10:51:22 +0100
branchtweet-widget
changeset 285 b7aa28af2c10
parent 284 cbb17aa3361c
child 286 6252f951d11c
child 848 d88735bc8de5
added a function to format a tweet.
src/js/utils.js
src/js/widgets/tweetsWidget.js
unittests/tests/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, "<a href='http://twitter.com/$1'>@$1</a>");
+  var i2 = i1.replace(rHashtag, "<a href='http://twitter.com/search?q=%23$1'>#$1</a>");
+  
+  return i2;
+};
+
 /* for ie compatibility
 if (Object.prototype.__defineGetter__&&!Object.defineProperty) {
    Object.defineProperty=function(obj,prop,desc) {
--- 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("<img src='{{src}}' alt='avatar'></img>", 
                                        {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); 
 };
--- 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 = "<a href='http://twitter.com/handle'>@handle</a> <a href='http://twitter.com/search?q=%23hashtag'>#hashtag</a> ";
+    equal(IriSP.formatTweet(input), output, "the correct output is given");
+  });
 }
\ No newline at end of file