def popover_ellipsis(source, length=0): ''' return the plain text representation of markdown encoded text. That is the texted without any html tags. If ``length`` is 0 then it will not be truncated.''' try: length = int(length) except ValueError: # invalid literal for int() return source # Fail silently. ellipsis = ('<a href rel="popover" data-content="{0}" ' 'data-trigger="hover" data-container="body">...</a>').format(escape(source)) truncated = Truncator(strip_tags(source)).chars(length) nb_words = len(truncated.split(' ')) html_truncated = Truncator(source).words(nb_words, html=True, truncate='{...}') return mark_safe(html_truncated.replace('{...}', ellipsis))