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:
>>> 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.
Contributes
You can send a pull reueqst on GitHub.
Metadata
Release files for html5lib-truncation 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| html5lib-truncation-0.1.0.tar.gz | 4.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| html5lib_truncation-0.1.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 12.4 kB
Release files / html5lib-truncation-0.1.0.tar.gz
| Download URL | html5lib-truncation-0.1.0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f69d3b3e31d4e9caef138d4602ed8eb531eaafd94b6f9ee8b4932722cd3d0308
|
|
BLAKE2b-256 checksum How to use checksums |
cf0dd07cc96c60000dfa1afd446b0660e6f3f8ff59ef0d513dafc907f1d3ee60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / html5lib_truncation-0.1.0-py2.py3-none-any.whl
| Download URL | html5lib_truncation-0.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
2179c3d04a948aaf4ce8b4472b18fa3b2cb9f4956eb3810942be620faf36a9d3
|
|
BLAKE2b-256 checksum How to use checksums |
ca9685470bf06ca3a5fef024aefe29514516871c169e4c42e8e1c2ff81d48513
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for html5lib-truncation 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.