README.rst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. |Build Status| |Coverage Status| |PyPI Version| |Wheel Status|
  2. html5lib-truncation
  3. ===================
  4. ``html5lib-truncation`` is a html5lib_ filter implementation, which can
  5. truncate HTML to specific length in display, but never breaks HTML tags.
  6. There is a shortcut function, the simplest way to use it:
  7. .. code-block:: python
  8. >>> from html5lib_truncation import truncate_html
  9. >>>
  10. >>> html = u'<p>A <a href="#">very very long link</a></p>'
  11. >>> truncate_html(html, 8)
  12. u'<p>A <a href=#>very</a>'
  13. >>> truncate_html(html, 8, break_words=True)
  14. u'<p>A <a href=#>very ve</a>'
  15. >>> truncate_html(html, 20, end='...')
  16. u'<p>A <a href=#>very very...</a>'
  17. >>> truncate_html(html, 20, end='...', break_words=True)
  18. u'<p>A <a href=#>very very lon...</a>'
  19. .. _html5lib: https://github.com/html5lib/html5lib-python
  20. Installation
  21. ------------
  22. ::
  23. pip install html5lib-truncation
  24. Don't forget to put it into your ``requirements.txt`` or ``setup.py``.
  25. API Overview
  26. ------------
  27. The core API of html5lib-truncation is the filter:
  28. .. code-block:: python
  29. import html5lib
  30. from html5lib_truncation import TruncationFilter
  31. etree = html5lib.parse(u'<p>A <a href="#">very very long link</a></p>')
  32. walker = html5lib.getTreeWalker('etree')
  33. stream = walker(etree)
  34. stream = TruncationFilter(stream, 20, end='...', break_words=True)
  35. serializer = html5lib.serializer.HTMLSerializer()
  36. serialized = serializer.serialize(stream)
  37. print(u''.join(serialized).strip())
  38. The output is ``<p>A <a href=#>very very lon...</a>``.
  39. Issues
  40. ------
  41. If you want to report bugs or other issues, please create issues on
  42. `GitHub Issues <https://github.com/tonyseek/html5lib-truncation/issues>`_.
  43. Contributes
  44. -----------
  45. You can send a pull reueqst on
  46. `GitHub <https://github.com/tonyseek/html5lib-truncation/pulls>`_.
  47. .. |Build Status| image:: https://img.shields.io/travis/tonyseek/html5lib-truncation.svg?style=flat
  48. :target: https://travis-ci.org/tonyseek/html5lib-truncation
  49. :alt: Build Status
  50. .. |Coverage Status| image:: https://img.shields.io/coveralls/tonyseek/html5lib-truncation.svg?style=flat
  51. :target: https://coveralls.io/r/tonyseek/html5lib-truncation
  52. :alt: Coverage Status
  53. .. |Wheel Status| image:: https://img.shields.io/pypi/wheel/html5lib-truncation.svg?style=flat
  54. :target: https://warehouse.python.org/project/html5lib-truncation
  55. :alt: Wheel Status
  56. .. |PyPI Version| image:: https://img.shields.io/pypi/v/html5lib-truncation.svg?style=flat
  57. :target: https://pypi.python.org/pypi/html5lib-truncation
  58. :alt: PyPI Version