Skip to main content

Simple HTML cleanup utilities

Project description

Introduction

This package contains several handy python methods to cleanup HTML markup or perform other common changes. The cleanup is strict enough to be able to clean HTML pasted from MS Word or Apple Pages. This package also contains integration code for z3c.form to provide fields that automatically sanitize HTML on save.

The implementation is based on the Cleaner class from lxml.

Cleanup routines

All cleanup routines can be invoked through the single sanitize function. This functions takes an input string as input and will return a cleaned up version of that string. Here is a simple example:

>>> from htmllaundy import sanitize
>>> sanitize('Hello, <em>world</em>')
'<p>Hello, <em>world</em></p>'

The sanitize method takes an extra optional parameter with a lxml Cleaner instance, which can be used to use different filtering rules. htmllaundry includes three cleaners:

  • htmllaundy.cleaners.DocumentCleaner, which is the default cleaner. This cleaner will allow most safe tags, while stripping out inline styles and insecure markup.

  • htmllaundy.cleaners.LineCleaner is a more strict cleaner which only allows a few inline elements. This is useful in places where you only want to accept single-line input, for example in document titles.

  • htmllaundy.cleaners.CommentCleaner only allows a very limited set of HTML elements, and is designed to be useful for user provided comments.

z3c.form integration

If you want to use the z3c.form integration you should use the z3cform extra for this package:

install_requires=[
     ....
     htmllaundry [z3cform]
     ...
     ],

You can then use the HtmlText field type in your schemas. For example:

from zope.interface import Interface
from zope import schema
from htmllaundry.z3cform import HtmlText

class IDocument(Interface):
    title = schema.TextLine(
            title = _(u"Title"),
            required = True)

    description = HtmlText(
            title = _(u"Description"),
          required = True)

Please note that using HtmlText will not automatically give you a WYSYWIG widget.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

htmllaundry-1.0.tar.gz (5.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page