Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tonyseek/html5lib-truncation
Truncating HTML with html5lib filter
https://github.com/tonyseek/html5lib-truncation
html python text-processing
Last synced: 3 months ago
JSON representation
Truncating HTML with html5lib filter
- Host: GitHub
- URL: https://github.com/tonyseek/html5lib-truncation
- Owner: tonyseek
- License: mit
- Created: 2015-03-10T18:31:19.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-09-30T10:51:01.000Z (over 7 years ago)
- Last Synced: 2024-09-13T01:19:21.403Z (4 months ago)
- Topics: html, python, text-processing
- Language: Python
- Size: 9.77 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
|Build Status| |Coverage Status| |PyPI Version| |Wheel Status|
html5lib-truncation
===================``html5lib-truncation`` is a 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:
.. code-block:: python
>>> from html5lib_truncation import truncate_html
'
>>>
>>> html = u'
>>> truncate_html(html, 8)
u'A very'
>>> truncate_html(html, 8, break_words=True)
u'A very ve'
>>> truncate_html(html, 20, end='...')
u'A very very...'
>>> truncate_html(html, 20, end='...', break_words=True)
u'.. _html5lib: https://github.com/html5lib/html5lib-python
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:
.. code-block:: python
import html5lib
from html5lib_truncation import TruncationFilteretree = html5lib.parse(u'
')
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 ``
A very very lon...``.
Issues
------If you want to report bugs or other issues, please create issues on
`GitHub Issues `_.Contributes
-----------You can send a pull reueqst on
`GitHub `_... |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://img.shields.io/pypi/wheel/html5lib-truncation.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