shortcuts_1.py 669 B

1234567891011121314151617181920212223
  1. def truncate_html(html, *args, **kwargs):
  2. """Truncates HTML string.
  3. :param html: The HTML string or parsed element tree (with
  4. :func:`html5lib.parse`).
  5. :param kwargs: Similar with :class:`.filters.TruncationFilter`.
  6. :return: The truncated HTML string.
  7. """
  8. if hasattr(html, 'getchildren'):
  9. etree = html
  10. else:
  11. etree = html5lib.parse(html)
  12. walker = html5lib.getTreeWalker('etree')
  13. stream = walker(etree)
  14. stream = TruncationFilter(stream, *args, **kwargs)
  15. serializer = html5lib.serializer.HTMLSerializer()
  16. serialized = serializer.serialize(stream)
  17. return u''.join(serialized).strip()