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
        (ex. google.com, github.com). Default: True
        :param use_expanded_url: Use expanded URL to represent link
        (e.g. http://google.com). Default False

        If use_expanded_url is True, it overrides use_display_url.
        If use_display_url and use_expanded_url is False, short url will
        be used (t.co/xxxxx)

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, symbols replaced with links)

        :param tweet: Tweet object from received from Twitter API
        :param use_display_url: Use display URL to represent link
        (ex. google.com, github.com). Default: True
        :param use_expanded_url: Use expanded URL to represent link
        (e.g. http://google.com). Default False

        If use_expanded_url is True, it overrides use_display_url.
        If use_display_url and use_expanded_url is False, short url will
        be used (t.co/xxxxx)

        """
        if 'retweeted_status' in tweet:
            tweet = tweet['retweeted_status']

        if 'extended_tweet' in tweet:
            tweet = tweet['extended_tweet']

        orig_tweet_text = tweet.get('full_text') or tweet['text']

        display_text_range = tweet.get('display_text_range') or [0, len(orig_tweet_text)]
        display_text_start, display_text_end = display_text_range[0], display_text_range[1]
        display_text = orig_tweet_text[display_text_start:display_text_end]
        prefix_text = orig_tweet_text[0:display_text_start]
        suffix_text = orig_tweet_text[display_text_end:len(orig_tweet_text)]

        if 'entities' in tweet:
            # We'll put all the bits of replacement HTML and their starts/ends
            # in this list:
            entities = []

            # Mentions
            if 'user_mentions' in tweet['entities']:
                for entity in tweet['entities']['user_mentions']:
                    temp = {}
                    temp['start'] = entity['indices'][0]
                    temp['end'] = entity['indices'][1]

                    mention_html = '<a href="https://twitter.com/%(screen_name)s" class="twython-mention">@%(screen_name)s</a>' % {'screen_name': entity['screen_name']}

                    if display_text_start <= temp['start'] <= display_text_end:
                        temp['replacement'] = mention_html
                        temp['start'] -= display_text_start
                        temp['end'] -= display_text_start
                        entities.append(temp)
                    else:
                        # Make the '@username' at the start, before
                        # display_text, into a link:
                        sub_expr = r'(?<!>)' + orig_tweet_text[temp['start']:temp['end']] + '(?!</a>)'
                        prefix_text = re.sub(sub_expr, mention_html, prefix_text)

            # Hashtags
            if 'hashtags' in tweet['entities']:
                for entity in tweet['entities']['hashtags']:
                    temp = {}
                    temp['start'] = entity['indices'][0] - display_text_start
                    temp['end'] = entity['indices'][1] - display_text_start

                    url_html = '<a href="https://twitter.com/search?q=%%23%(hashtag)s" class="twython-hashtag">#%(hashtag)s</a>' % {'hashtag': entity['text']}

                    temp['replacement'] = url_html
                    entities.append(temp)

            # Symbols
            if 'symbols' in tweet['entities']:
                for entity in tweet['entities']['symbols']:
                    temp = {}
                    temp['start'] = entity['indices'][0] - display_text_start
                    temp['end'] = entity['indices'][1] - display_text_start

                    url_html = '<a href="https://twitter.com/search?q=%%24%(symbol)s" class="twython-symbol">$%(symbol)s</a>' % {'symbol': entity['text']}

                    temp['replacement'] = url_html
                    entities.append(temp)

            # URLs
            if 'urls' in tweet['entities']:
                for entity in tweet['entities']['urls']:
                    temp = {}
                    temp['start'] = entity['indices'][0] - display_text_start
                    temp['end'] = entity['indices'][1] - display_text_start

                    if use_display_url and entity.get('display_url') and not use_expanded_url:
                        shown_url = entity['display_url']
                    elif use_expanded_url and entity.get('expanded_url'):
                        shown_url = entity['expanded_url']
                    else:
                        shown_url = entity['url']

                    url_html = '<a href="%s" class="twython-url">%s</a>' % (entity['url'], shown_url)

                    if display_text_start <= temp['start'] <= display_text_end:
                        temp['replacement'] = url_html
                        entities.append(temp)
                    else:
                        suffix_text = suffix_text.replace(orig_tweet_text[temp['start']:temp['end']], url_html)

            if 'media' in tweet['entities'] and len(tweet['entities']['media']) > 0:
                # We just link to the overall URL for the tweet's media,
                # rather than to each individual item.
                # So, we get the URL from the first media item:
                entity = tweet['entities']['media'][0]

                temp = {}
                temp['start'] = entity['indices'][0]
                temp['end'] = entity['indices'][1]

                if use_display_url and entity.get('display_url') and not use_expanded_url:
                    shown_url = entity['display_url']
                elif use_expanded_url and entity.get('expanded_url'):
                    shown_url = entity['expanded_url']
                else:
                    shown_url = entity['url']

                url_html = '<a href="%s" class="twython-media">%s</a>' % (entity['url'], shown_url)

                if display_text_start <= temp['start'] <= display_text_end:
                    temp['replacement'] = url_html
                    entities.append(temp)
                else:
                    suffix_text = suffix_text.replace(orig_tweet_text[temp['start']:temp['end']], url_html)

            # Now do all the replacements, starting from the end, so that the
            # start/end indices still work:
            for entity in sorted(entities, key=lambda e: e['start'], reverse=True):
                display_text = display_text[0:entity['start']] + entity['replacement'] + display_text[entity['end']:]

        quote_text = ''
        if expand_quoted_status and tweet.get('is_quote_status') and tweet.get('quoted_status'):
            quoted_status = tweet['quoted_status']
            quote_text += '<blockquote class="twython-quote">%(quote)s<cite><a href="%(quote_tweet_link)s">' \
                    '<span class="twython-quote-user-name">%(quote_user_name)s</span>' \
                    '<span class="twython-quote-user-screenname">@%(quote_user_screen_name)s</span></a>' \
                    '</cite></blockquote>' % \
                    {'quote': Twython.html_for_tweet(quoted_status, use_display_url, use_expanded_url, False),
                     'quote_tweet_link': 'https://twitter.com/%s/status/%s' %
                                         (quoted_status['user']['screen_name'], quoted_status['id_str']),
                     'quote_user_name': quoted_status['user']['name'],
                     'quote_user_screen_name': quoted_status['user']['screen_name']}

        return '%(prefix)s%(display)s%(suffix)s%(quote)s' % {
            'prefix': '<span class="twython-tweet-prefix">%s</span>' % prefix_text if prefix_text else '',
            'display': display_text,
            'suffix': '<span class="twython-tweet-suffix">%s</span>' % suffix_text if suffix_text else '',
            'quote': quote_text
        }