shortcuts.py 768 B

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