Improve replacing of entities with links in html_for_tweet()

I've re-written the parts of `html_for_tweet()` so that it handles
all the replacements of URLs, mentions, symbols and hashtags better.

Mainly to fix #447 but it should be a little more robust generally.

Shamelessly cribbed from https://stackoverflow.com/a/25514650/250962

Passes all tests, but I haven't checked it beyond that.

Fixes #447
This commit is contained in:
Phil Gyford 2017-08-22 13:49:40 +01:00
parent 5a87fc7d84
commit b366ab55c3
3 changed files with 80 additions and 48 deletions

View file

@ -4,6 +4,7 @@ from twython import Twython, TwythonError, TwythonAuthError, TwythonRateLimitErr
from .config import (
test_tweet_object, test_tweet_html, test_tweet_symbols_object,
test_tweet_compat_object, test_tweet_extended_object, test_tweet_extended_html,
test_tweet_identical_urls,
unittest
)
@ -321,6 +322,12 @@ class TwythonAPITestCase(unittest.TestCase):
self.assertTrue('http://google.com' not in tweet_text)
self.assertTrue('google.com' not in tweet_text)
def test_html_for_tweet_identical_urls(self):
"""If the 'url's for different url entities are identical, they should link correctly."""
tweet_text = self.api.html_for_tweet(test_tweet_identical_urls)
self.assertEqual(tweet_text,
u'Use Cases, Trials and Making 5G a Reality <a href="https://t.co/W0uArTMk9N" class="twython-url">buff.ly/2sEhrgO</a> #5G #innovation via @5GWorldSeries <a href="https://t.co/W0uArTMk9N" class="twython-url">buff.ly/2sEhrgO</a>')
def test_html_for_tweet_symbols(self):
tweet_text = self.api.html_for_tweet(test_tweet_symbols_object)
# Should only link symbols listed in entities: