Turn hashtags into links

This commit is contained in:
Anna 2010-05-28 19:44:09 -04:00
parent dce0fa160c
commit 0bccbc0b6b
2 changed files with 30 additions and 1 deletions

View File

@ -346,6 +346,7 @@ class Hrafn():
new_pane.connect('tweet-in-reply-to', self.on_reply_to) new_pane.connect('tweet-in-reply-to', self.on_reply_to)
new_pane.connect('tweet-conversation', self.on_conversation) new_pane.connect('tweet-conversation', self.on_conversation)
new_pane.connect('show-user', self.show_user_callback) new_pane.connect('show-user', self.show_user_callback)
new_pane.connect('show-hashtag', self.show_hashtag)
# Special logic for single tweet pane # Special logic for single tweet pane
if single_tweet is not None: if single_tweet is not None:
@ -419,6 +420,10 @@ class Hrafn():
self.show_user(data) self.show_user(data)
def show_hashtag(self, widget, data):
self.add_to_notebook('#' + data)
def on_at_button_clicked(self, widget, user_name): def on_at_button_clicked(self, widget, user_name):
self.add_to_notebook('@' + user_name) self.add_to_notebook('@' + user_name)

View File

@ -74,6 +74,7 @@ class TweetPane(gtk.ScrolledWindow):
self.tweets[i].connect('in-reply-to', self.on_tweet_reply_to) self.tweets[i].connect('in-reply-to', self.on_tweet_reply_to)
self.tweets[i].connect('conversation', self.on_tweet_conversation) self.tweets[i].connect('conversation', self.on_tweet_conversation)
self.tweets[i].connect('show-user', self.on_show_user) self.tweets[i].connect('show-user', self.on_show_user)
self.tweets[i].connect('show-hashtag', self.on_show_hashtag)
self.tweets[i].connect('tweet-read', self.set_tweets_read_callback) self.tweets[i].connect('tweet-read', self.set_tweets_read_callback)
viewport.add(tweet_box) viewport.add(tweet_box)
@ -209,6 +210,10 @@ class TweetPane(gtk.ScrolledWindow):
self.emit('show-user', data) self.emit('show-user', data)
def on_show_hashtag(self, widget, data):
self.emit('show-hashtag', data)
def get_is_user(self): def get_is_user(self):
return self.is_user return self.is_user
@ -239,6 +244,9 @@ gobject.signal_new("tweet-conversation", TweetPane,
gobject.signal_new("show-user", TweetPane, gobject.signal_new("show-user", TweetPane,
gobject.SIGNAL_RUN_LAST, gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)) gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))
gobject.signal_new("show-hashtag", TweetPane,
gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))
gobject.signal_new("follow-clicked", TweetPane, gobject.signal_new("follow-clicked", TweetPane,
gobject.SIGNAL_RUN_LAST, gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)) gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))
@ -462,8 +470,11 @@ class TweetBox(gtk.HBox):
new_text = re.sub(r'&([^;]*?)( |$)', r'&\1\2', new_text) new_text = re.sub(r'&([^;]*?)( |$)', r'&\1\2', new_text)
new_text = re.sub(r'"([^;]*?)( |$)', r'"\1\2', new_text) new_text = re.sub(r'"([^;]*?)( |$)', r'"\1\2', new_text)
if gtk.gtk_version[0] > 2 or (gtk.gtk_version[0] == 2 and gtk.gtk_version[1] >= 18): if gtk.gtk_version[0] > 2 or (gtk.gtk_version[0] == 2 and gtk.gtk_version[1] >= 18):
# Make URLs into links
new_text = re.sub(r"(http://.*?)([^0-9a-zA-Z.~/-_]|$)", r'<a href="\1">\1</a>\2', new_text) new_text = re.sub(r"(http://.*?)([^0-9a-zA-Z.~/-_]|$)", r'<a href="\1">\1</a>\2', new_text)
# Make @ or # refs into links, to be used internally
new_text = re.sub(r'@(.*?)([^0-9a-zA-Z_]|$)', self._make_user_link, new_text) new_text = re.sub(r'@(.*?)([^0-9a-zA-Z_]|$)', self._make_user_link, new_text)
new_text = re.sub(r'#(.*?)([^0-9a-zA-Z_-]|$)', self._make_hashtag_link, new_text)
self.text.set_markup(new_text) self.text.set_markup(new_text)
# If this is in reply to something, set appropriate label # If this is in reply to something, set appropriate label
@ -546,6 +557,9 @@ class TweetBox(gtk.HBox):
if re.match(r'@', uri): if re.match(r'@', uri):
self.emit('show-user', re.sub(r'@', '', uri)) self.emit('show-user', re.sub(r'@', '', uri))
return True return True
if re.match(r'#', uri):
self.emit('show-hashtag', re.sub(r'#', '', uri))
return True
def _make_user_link(self, matchobj): def _make_user_link(self, matchobj):
@ -555,6 +569,13 @@ class TweetBox(gtk.HBox):
else: else:
return '@<a href="@' + name + '">' + name + '</a>' + matchobj.group(2) return '@<a href="@' + name + '">' + name + '</a>' + matchobj.group(2)
def _make_hashtag_link(self, matchobj):
name = matchobj.group(1)
if name == '':
return '#' + matchobj.group(2)
else:
return '<a href="#' + name + '">#' + name + '</a>' + matchobj.group(2)
# end class TweetBox # end class TweetBox
# signals for TweetBox # signals for TweetBox
@ -576,6 +597,9 @@ gobject.signal_new("conversation", TweetBox,
gobject.signal_new("show-user", TweetBox, gobject.signal_new("show-user", TweetBox,
gobject.SIGNAL_RUN_LAST, gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)) gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))
gobject.signal_new("show-hashtag", TweetBox,
gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))