1+ ( function ( $ ) {
2+
3+ $ . fn . tweet = function ( o ) {
4+ var s = {
5+ username : [ "seaofclouds" ] , // [string] required, unless you want to display our tweets. :) it can be an array, just do ["username1","username2","etc"]
6+ list : null , //[string] optional name of list belonging to username
7+ avatar_size : null , // [integer] height and width of avatar if displayed (48px max)
8+ count : 3 , // [integer] how many tweets to display?
9+ intro_text : null , // [string] do you want text BEFORE your your tweets?
10+ outro_text : null , // [string] do you want text AFTER your tweets?
11+ join_text : null , // [string] optional text in between date and tweet, try setting to "auto"
12+ auto_join_text_default : "i said," , // [string] auto text for non verb: "i said" bullocks
13+ auto_join_text_ed : "i" , // [string] auto text for past tense: "i" surfed
14+ auto_join_text_ing : "i am" , // [string] auto tense for present tense: "i was" surfing
15+ auto_join_text_reply : "i replied to" , // [string] auto tense for replies: "i replied to" @someone "with"
16+ auto_join_text_url : "i was looking at" , // [string] auto tense for urls: "i was looking at" http:...
17+ loading_text : null , // [string] optional loading text, displayed while tweets load
18+ query : null // [string] optional search query
19+ } ;
20+
21+ if ( o ) $ . extend ( s , o ) ;
22+
23+ $ . fn . extend ( {
24+ linkUrl : function ( ) {
25+ var returning = [ ] ;
26+ var regexp = / ( ( f t p | h t t p | h t t p s ) : \/ \/ ( \w + : { 0 , 1 } \w * @ ) ? ( \S + ) ( : [ 0 - 9 ] + ) ? ( \/ | \/ ( [ \w # ! : . ? + = & % @ ! \- \/ ] ) ) ? ) / gi;
27+ this . each ( function ( ) {
28+ returning . push ( this . replace ( regexp , "<a href=\"$1\">$1</a>" ) ) ;
29+ } ) ;
30+ return $ ( returning ) ;
31+ } ,
32+ linkUser : function ( ) {
33+ var returning = [ ] ;
34+ var regexp = / [ \@ ] + ( [ A - Z a - z 0 - 9 - _ ] + ) / gi;
35+ this . each ( function ( ) {
36+ returning . push ( this . replace ( regexp , "<a href=\"http://twitter.com/$1\">@$1</a>" ) ) ;
37+ } ) ;
38+ return $ ( returning ) ;
39+ } ,
40+ linkHash : function ( ) {
41+ var returning = [ ] ;
42+ var regexp = / [ \# ] + ( [ A - Z a - z 0 - 9 - _ ] + ) / gi;
43+ this . each ( function ( ) {
44+ returning . push ( this . replace ( regexp , ' <a href="http://search.twitter.com/search?q=&tag=$1&lang=all&from=' + s . username . join ( "%2BOR%2B" ) + '">#$1</a>' ) ) ;
45+ } ) ;
46+ return $ ( returning ) ;
47+ } ,
48+ capAwesome : function ( ) {
49+ var returning = [ ] ;
50+ this . each ( function ( ) {
51+ returning . push ( this . replace ( / \b ( a w e s o m e ) \b / gi, '<span class="awesome">$1</span>' ) ) ;
52+ } ) ;
53+ return $ ( returning ) ;
54+ } ,
55+ capEpic : function ( ) {
56+ var returning = [ ] ;
57+ this . each ( function ( ) {
58+ returning . push ( this . replace ( / \b ( e p i c ) \b / gi, '<span class="epic">$1</span>' ) ) ;
59+ } ) ;
60+ return $ ( returning ) ;
61+ } ,
62+ makeHeart : function ( ) {
63+ var returning = [ ] ;
64+ this . each ( function ( ) {
65+ returning . push ( this . replace ( / ( & l t ; ) + [ 3 ] / gi, "<tt class='heart'>♥</tt>" ) ) ;
66+ } ) ;
67+ return $ ( returning ) ;
68+ }
69+ } ) ;
70+
71+ function relative_time ( time_value ) {
72+ var parsed_date = Date . parse ( time_value ) ;
73+ var relative_to = ( arguments . length > 1 ) ? arguments [ 1 ] : new Date ( ) ;
74+ var delta = parseInt ( ( relative_to . getTime ( ) - parsed_date ) / 1000 ) ;
75+ var pluralize = function ( singular , n ) {
76+ return '' + n + ' ' + singular + ( n == 1 ? '' : 's' ) ;
77+ } ;
78+ if ( delta < 60 ) {
79+ return 'less than a minute ago' ;
80+ } else if ( delta < ( 45 * 60 ) ) {
81+ return 'about ' + pluralize ( "minute" , parseInt ( delta / 60 ) ) + ' ago' ;
82+ } else if ( delta < ( 24 * 60 * 60 ) ) {
83+ return 'about ' + pluralize ( "hour" , parseInt ( delta / 3600 ) ) + ' ago' ;
84+ } else {
85+ return 'about ' + pluralize ( "day" , parseInt ( delta / 86400 ) ) + ' ago' ;
86+ }
87+ }
88+
89+ function build_url ( ) {
90+ var proto = ( 'https:' == document . location . protocol ? 'https:' : 'http:' ) ;
91+ if ( s . list ) {
92+ return proto + "//api.twitter.com/1/" + s . username [ 0 ] + "/lists/" + s . list + "/statuses.json?per_page=" + s . count + "&callback=?" ;
93+ } else if ( s . query == null && s . username . length == 1 ) {
94+ return proto + '//twitter.com/status/user_timeline/' + s . username [ 0 ] + '.json?count=' + s . count + '&callback=?' ;
95+ } else {
96+ var query = ( s . query || 'from:' + s . username . join ( '%20OR%20from:' ) ) ;
97+ return proto + '//search.twitter.com/search.json?&q=' + query + '&rpp=' + s . count + '&callback=?' ;
98+ }
99+ }
100+
101+ return this . each ( function ( ) {
102+ var list = $ ( '<ul class="tweet_list">' ) . appendTo ( this ) ;
103+ var intro = '<p class="tweet_intro">' + s . intro_text + '</p>' ;
104+ var outro = '<p class="tweet_outro">' + s . outro_text + '</p>' ;
105+ var loading = $ ( '<p class="loading">' + s . loading_text + '</p>' ) ;
106+
107+ if ( typeof ( s . username ) == "string" ) {
108+ s . username = [ s . username ] ;
109+ }
110+
111+ if ( s . loading_text ) $ ( this ) . append ( loading ) ;
112+ $ . getJSON ( build_url ( ) , function ( data ) {
113+ if ( s . loading_text ) loading . remove ( ) ;
114+ if ( s . intro_text ) list . before ( intro ) ;
115+ $ . each ( ( data . results || data ) , function ( i , item ) {
116+ // auto join text based on verb tense and content
117+ if ( s . join_text == "auto" ) {
118+ if ( item . text . match ( / ^ ( @ ( [ A - Z a - z 0 - 9 - _ ] + ) ) .* / i) ) {
119+ var join_text = s . auto_join_text_reply ;
120+ } else if ( item . text . match ( / ( ^ \w + : \/ \/ [ A - Z a - z 0 - 9 - _ ] + \. [ A - Z a - z 0 - 9 - _ : % & \? \/ .= ] + ) .* / i) ) {
121+ var join_text = s . auto_join_text_url ;
122+ } else if ( item . text . match ( / ^ ( ( \w + e d ) | j u s t ) .* / im) ) {
123+ var join_text = s . auto_join_text_ed ;
124+ } else if ( item . text . match ( / ^ ( \w * i n g ) .* / i) ) {
125+ var join_text = s . auto_join_text_ing ;
126+ } else {
127+ var join_text = s . auto_join_text_default ;
128+ }
129+ } else {
130+ var join_text = s . join_text ;
131+ } ;
132+
133+ var from_user = item . from_user || item . user . screen_name ;
134+ var profile_image_url = item . profile_image_url || item . user . profile_image_url ;
135+ var join_template = '<span class="tweet_join"> ' + join_text + ' </span>' ;
136+ var join = ( ( s . join_text ) ? join_template : ' ' ) ;
137+ var avatar_template = '<a class="tweet_avatar" href="http://twitter.com/' + from_user + '"><img src="' + profile_image_url + '" height="' + s . avatar_size + '" width="' + s . avatar_size + '" alt="' + from_user + '\'s avatar" title="' + from_user + '\'s avatar" border="0"/></a>' ;
138+ var avatar = ( s . avatar_size ? avatar_template : '' ) ;
139+ var date = '<a href="http://twitter.com/' + from_user + '/statuses/' + item . id + '" title="view tweet on twitter">' + relative_time ( item . created_at ) + '</a>' ;
140+ var text = '<span class="tweet_text">' + $ ( [ item . text ] ) . linkUrl ( ) . linkUser ( ) . linkHash ( ) . makeHeart ( ) . capAwesome ( ) . capEpic ( ) [ 0 ] + '</span>' ;
141+
142+ // until we create a template option, arrange the items below to alter a tweet's display.
143+ list . append ( '<li>' + avatar + date + join + text + '</li>' ) ;
144+
145+ list . children ( 'li:first' ) . addClass ( 'tweet_first' ) ;
146+ list . children ( 'li:odd' ) . addClass ( 'tweet_even' ) ;
147+ list . children ( 'li:even' ) . addClass ( 'tweet_odd' ) ;
148+ } ) ;
149+ if ( s . outro_text ) list . after ( outro ) ;
150+ } ) ;
151+
152+ } ) ;
153+ } ;
154+ } ) ( jQuery ) ;
0 commit comments