Metadata-Version: 2.0
Name: html5lib-truncation
Version: 0.1.0
Summary: Truncating HTML with html5lib filter
Home-page: https://github.com/tonyseek/html5lib-truncation
Author: Jiangge Zhang
Author-email: tonyseek@gmail.com
License: MIT
Platform: Any
Classifier: Development Status :: 3 - Alpha
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Dist: html5lib

html5lib-truncation
===================

``html5lib-truncation`` is an html5lib filter implementation, which can
truncate HTML to specific length in display, but never breaks HTML tags.

There is a shortcut function, the simplest way to use it::

    >>> from html5lib_truncation import truncate_html
    >>>
    >>> html = u'<p>A <a href="#">very very long link</a></p>'
    >>> truncate_html(html, 8)
    u'<p>A <a href=#>very</a>'
    >>> truncate_html(html, 8, break_words=True)
    u'<p>A <a href=#>very ve</a>'
    >>> truncate_html(html, 20, end='...')
    u'<p>A <a href=#>very very...</a>'
    >>> truncate_html(html, 20, end='...', break_words=True)
    u'<p>A <a href=#>very very lon...</a>'


Installation
------------

::

    pip install html5lib-truncation

Don't forget to put it into your ``requirements.txt`` or ``setup.py``.


API Overview
------------

The core API of html5lib-truncation is the filter::

    import html5lib
    from html5lib_truncation import TruncationFilter

    etree = html5lib.parse(u'<p>A <a href="#">very very long link</a></p>')
    walker = html5lib.getTreeWalker('etree')

    stream = walker(etree)
    stream = TruncationFilter(stream, 20, end='...', break_words=True)

    serializer = html5lib.serializer.HTMLSerializer()
    serialized = serializer.serialize(stream)

    print(u''.join(serialized).strip())

The output is ``<p>A <a href=#>very very lon...</a>``.


Issues
------

If you want to report bugs or other issues, please create issues on
`GitHub Issues <https://github.com/tonyseek/html5lib-truncation/issues>`_.


Contributes
-----------

You can send a pull reueqst on
`GitHub <https://github.com/tonyseek/html5lib-truncation/pulls>`_.

.. |Build Status| image:: https://img.shields.io/travis/tonyseek/html5lib-truncation.svg?style=flat
   :target: https://travis-ci.org/tonyseek/html5lib-truncation
   :alt: Build Status
.. |Coverage Status| image:: https://img.shields.io/coveralls/tonyseek/html5lib-truncation.svg?style=flat
   :target: https://coveralls.io/r/tonyseek/html5lib-truncation
   :alt: Coverage Status
.. |Wheel Status| image:: https://pypip.in/wheel/html5lib-truncation/badge.svg?style=flat
   :target: https://warehouse.python.org/project/html5lib-truncation
   :alt: Wheel Status
.. |PyPI Version| image:: https://img.shields.io/pypi/v/html5lib-truncation.svg?style=flat
   :target: https://pypi.python.org/pypi/html5lib-truncation
   :alt: PyPI Version

