|
10 | 10 | """ |
11 | 11 |
|
12 | 12 | import warnings |
| 13 | +import re |
13 | 14 |
|
14 | 15 | import requests |
15 | 16 | from requests.auth import HTTPBasicAuth |
@@ -550,19 +551,22 @@ def html_for_tweet(tweet, use_display_url=True, use_expanded_url=False): |
550 | 551 | entities = tweet['entities'] |
551 | 552 |
|
552 | 553 | # Mentions |
553 | | - for entity in entities['user_mentions']: |
| 554 | + for entity in sorted(entities['user_mentions'], |
| 555 | + key=lambda mention: len(mention['screen_name']), reverse=True): |
554 | 556 | start, end = entity['indices'][0], entity['indices'][1] |
555 | 557 |
|
556 | 558 | mention_html = '<a href="https://twitter.com/%(screen_name)s" class="twython-mention">@%(screen_name)s</a>' |
557 | | - text = text.replace(tweet['text'][start:end], |
558 | | - mention_html % {'screen_name': entity['screen_name']}) |
| 559 | + text = re.sub(r'(?<!>)' + tweet['text'][start:end] + '(?!</a>)', |
| 560 | + mention_html % {'screen_name': entity['screen_name']}, text) |
559 | 561 |
|
560 | 562 | # Hashtags |
561 | | - for entity in entities['hashtags']: |
| 563 | + for entity in sorted(entities['hashtags'], |
| 564 | + key=lambda hashtag: len(hashtag['text']), reverse=True): |
562 | 565 | start, end = entity['indices'][0], entity['indices'][1] |
563 | 566 |
|
564 | 567 | hashtag_html = '<a href="https://twitter.com/search?q=%%23%(hashtag)s" class="twython-hashtag">#%(hashtag)s</a>' |
565 | | - text = text.replace(tweet['text'][start:end], hashtag_html % {'hashtag': entity['text']}) |
| 568 | + text = re.sub(r'(?<!>)' + tweet['text'][start:end] + '(?!</a>)', |
| 569 | + hashtag_html % {'hashtag': entity['text']}, text) |
566 | 570 |
|
567 | 571 | # Urls |
568 | 572 | for entity in entities['urls']: |
|
0 commit comments