Link $IBM-style symbols in tweets in html_for_tweet()

Fixes #412
This commit is contained in:
Phil Gyford 2016-04-22 14:22:32 +01:00
parent 885051acdc
commit a1640f4a17
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]