Merge pull request #415 from philgyford/symbols-in-html-for-tweet

Link $IBM-style symbols in tweets in html_for_tweet()
This commit is contained in:
Mike Helmick 2016-04-30 05:33:14 -04:00
commit 2faa84629b
4 changed files with 23 additions and 4 deletions

View file

@ -528,7 +528,7 @@ class Twython(EndpointsMixin, object):
@staticmethod
def html_for_tweet(tweet, use_display_url=True, use_expanded_url=False, expand_quoted_status=False):
"""Return HTML for a tweet (urls, mentions, hashtags replaced with links)
"""Return HTML for a tweet (urls, mentions, hashtags, symbols replaced with links)
:param tweet: Tweet object from received from Twitter API
:param use_display_url: Use display URL to represent link
@ -566,6 +566,15 @@ class Twython(EndpointsMixin, object):
text = re.sub(r'(?<!>)' + tweet['text'][start:end] + '(?!</a>)',
hashtag_html % {'hashtag': entity['text']}, text)
# Symbols
for entity in sorted(entities['symbols'],
key=lambda symbol: len(symbol['text']), reverse=True):
start, end = entity['indices'][0], entity['indices'][1]
symbol_html = '<a href="https://twitter.com/search?q=%%24%(symbol)s" class="twython-symbol">$%(symbol)s</a>'
text = re.sub(r'(?<!>)' + re.escape(tweet['text'][start:end]) + '(?!</a>)',
symbol_html % {'symbol': entity['text']}, text)
# Urls
for entity in entities['urls']:
start, end = entity['indices'][0], entity['indices'][1]