Package: htmllaundry
IntroductionThis 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 forz3c.formto provide fields that automatically
sanitize HTML on save.The implementation is based on theCleanerclass fromlxml.Cleanup routinesAll cleanup routines can be invoked through the singlesanitizefunction.
This functions takes an input string as input and will return a cleaned up
version of that string. Here is a simple example:>>> from htmllaundry 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:htmllaundry.cleaners.DocumentCleaner, which is the default cleaner. This
cleaner will allow most safe tags, while stripping out inline styles and
insecure markup.htmllaundry.cleaners.LineCleaneris 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.htmllaundry.cleaners.CommentCleaneronly allows a very limited set of
HTML elements, and is designed to be useful for user provided comments. It
will also force all external links to open in a new browser window.If you want to go all the way you can also usestrip_markupto strip
all markup from your input:>>> from htmllaundry import strip_markup
>>> strip_markup('Hello, <em>world</em>')
'Hello, world'z3c.form integrationIf you want to use thez3c.formintegration you should use thez3cformextra for this package:install_requires=[
     ....
     htmllaundry [z3cform]
     ...
     ],In addition you will need to load the ZCML. In yourconfigure.zcmladd
a line like this:<include package="htmllaundry" />You can then use theHtmlTextfield 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 usingHtmlTextwill not automatically give you a WYSYWIG
widget.Changelog2.2 (2020-01-28)Use @implementer and @adapter class decorators for Python3 compatibility [ale-rt]2.1 - May 10, 2016Do not remove empty<a>tags that could be used as anchors.When removing empty tags, allow to define additional tags that are considered OK to be empty2.0 - December 7, 2012When wrapping unwrapped text do not create separate wrappers for inline
elements.Use PEP8 naming for all functions. The old names for public methods
will continue to work for backwards compatibility.Add support for Python 3.1.10 - May 17, 2011Add option tosanitizeto specify a different wrap element or
skip wrapping completely.1.9 - April 27, 2011Add MANIFEST.in to faciliate releases not made from subversion.Fix all cleaners to strip javascript. This fixesissue 1.1.8 - November 30, 2010Remove link target enforcement from hardcoded code path fromsanitize.
This makes it possible to use the newlink_targetcleaner option.1.7 - November 30, 2010Make forcing of target attributes on externals linke configurable via a
newlink_targetoption in the cleaners. Only enable this option for
theCommentCleaner.1.6 - November 18, 2010Correct whitespace test for wrapping bare text as well.1.5 - November 18, 2010Correct whitespace checks to handle all unicode whitespace. This fixes problems
with xA0 (or &nbsp; in HTML-speak) being treated as text.1.4 - August 3, 2010Small code cleanup.Strip leading breaks.1.3 - July 30, 2010Strip all top level br elements. Breaks are fine in blocklevel elements,
but should not be used to add vertical spacing between block elements.1.2 - February 15, 2010Fix a typo in the documentation.Strip trailing breaks.1.1 - February 5, 2010Add a simple StripMarkup method.Add ZCML necessary for z3c.form integration.1.0 - February 5, 2010First release