HTML cleaner?

I

Ivan Voras

Is there a HTML clean/tidy library or module written in pure python? I
found mxTidy, but it's a interface to command-line tool.

What I'm searching is something that will accept a list of allowed tags
and/or attributes and strip the rest from HTML string.
 
F

Fuzzyman

I *just* wrote something that does this. It uses the htmldata module -
you can find that using pypi. It only allows a specific set of html
tags and attempts to close tags not closed. :

from htmldata import tagextract, tagjoin
allowed_tags = ['br', 'b', 'strong', 'em', 'i', 'u', 'tt', 'a', 'big',
'small',
'h2', 'h3', 'h4', 'strike', 'sub', 'sup', 'samp', 's',
'code', 'ins',
'br/',
]
def htmlfilter(intext, allowed_tags=allowed_tags):
"""Given a text entry as input, check it only contains allowed html.

It returns the text with banned html removed.
Uses two functiosn from htmldata :
from htmldata import tagextract, tagjoin
allowed_tags is the list of tags that are allowed.
"""
html = tagextract(intext) #
out_html = []
skip = None
unclosed = []
for entry in html:
if isinstance(entry, basestring):
if skip is not None:
continue
out_html.append(entry)
else:
tag = entry[0]
if skip is not None:
if tag.startswith('/') and tag[1:] == skip:
skip = None
continue
otag = tag
if tag.startswith('/'):
otag = tag[1:]
if otag in allowed_tags:
if tag.startswith('/'):
if otag in unclosed:
unlclosed.remove(otag)
else: # bad html
continue
elif tag not in ['br', '/br', 'hr', '/hr', 'img',
'/img']: # XXXX hardwired - what if we need to add to this ?
unclosed.append(tag)
out_html.append(entry)
continue
if not tag.startswith('/'):
skip = tag
for tag in unclosed:
out_html.append(('/%s' % (tag,), {})) # close any unclosed
tags
return tagjoin(out_html)


###############

I've used it to allow a few html tags to appear in my guestbook
entries. It's not very sophisticated because complex tags like 'div'
and tables aren't allowed.

Best regards,

Fuzzy
http://www.voidspace.org.uk/python
 
M

M.-A. Lemburg

Ivan said:
Is there a HTML clean/tidy library or module written in pure python? I
found mxTidy, but it's a interface to command-line tool.

Not true: mxTidy integrates tidy as C lib. It's not an interface
to the command line tool.
What I'm searching is something that will accept a list of allowed tags
and/or attributes and strip the rest from HTML string.

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Apr 25 2005)________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::
 
L

Leif K-Brooks

Ivan said:
Is there a HTML clean/tidy library or module written in pure python? I
found mxTidy, but it's a interface to command-line tool.

What I'm searching is something that will accept a list of allowed tags
and/or attributes and strip the rest from HTML string.

Here's a module I wrote to do something along the lines of what you
want: <http://ecritters.biz/limithtml.py>. Unfortunately, it requires
the HTML to be relatively well-formed (e.g. it doesn't like things like
"<i><b>foo</i></b>"), so I feed the HTML into uTidyLib (another
interface to HTML Tidy) first. I'm not sure why you don't want to use
Tidy, but if you do change your mind, you should be able to use my
module alongside Tidy to limit the HTML elements and attributes which
will be accepted.
 
T

Terry Hancock

Is there a HTML clean/tidy library or module written in pure python? I
found mxTidy, but it's a interface to command-line tool.

What I'm searching is something that will accept a list of allowed tags
and/or attributes and strip the rest from HTML string.

I'm using stripogram for this. It uses a whitelist approach, where you
tell it what tags to accept. It also has a function for getting text only.

http://www.zope.org/Members/chrisw/StripOGram/

It's very useful in Zope, but is actually an independent pure-python
module (you don't need Zope to use it). Also very small.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top