add new utility module for html processing
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3567 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
0bb7071829
commit
427d043f2c
3 changed files with 84 additions and 0 deletions
63
util/html.py
Normal file
63
util/html.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
#
|
||||
# Copyright (c) 2009 Helmut Merz helmutm@cy55.de
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#
|
||||
|
||||
"""
|
||||
Strip HTML tags and other HTML-related utilities.
|
||||
|
||||
$Id$
|
||||
"""
|
||||
|
||||
from cybertools.text.lib.BeautifulSoup import BeautifulSoup, Comment
|
||||
|
||||
#validTags = 'p i strong b u a h1 h2 h3 img pre br'.split()
|
||||
validTags = 'b br div em font h1 h2 h3 i p pre span strong table td tr u'.split()
|
||||
|
||||
#validAttrs = 'href src'.split()
|
||||
validAttrs = 'class style'.split()
|
||||
|
||||
validStyles = 'font-style font-weight'.split()
|
||||
|
||||
|
||||
def sanitize(value, validTags=validTags, validAttrs=validAttrs,
|
||||
validStyles=validStyles):
|
||||
soup = BeautifulSoup(value)
|
||||
for comment in soup.findAll(text=lambda text: isinstance(text, Comment)):
|
||||
comment.extract()
|
||||
for tag in soup.findAll(True):
|
||||
if tag.name not in validTags:
|
||||
tag.hidden = True
|
||||
attrs = []
|
||||
for attr, val in tag.attrs:
|
||||
if attr not in validAttrs:
|
||||
continue
|
||||
if attr == 'style':
|
||||
val = sanitizeStyle(val, validStyles)
|
||||
if val:
|
||||
attrs.append((attr, val))
|
||||
tag.attrs = attrs
|
||||
return soup.renderContents().decode('utf8')
|
||||
|
||||
|
||||
def sanitizeStyle(value, validStyles=validStyles):
|
||||
result = []
|
||||
for item in value.split(';'):
|
||||
if ':' in item:
|
||||
k, v = item.split(':')
|
||||
if k.strip() in validStyles:
|
||||
result.append(item.strip())
|
||||
return '; '.join(result)
|
20
util/html.txt
Normal file
20
util/html.txt
Normal file
|
@ -0,0 +1,20 @@
|
|||
==================
|
||||
Tweaking HTML text
|
||||
==================
|
||||
|
||||
$Id$
|
||||
|
||||
>>> from cybertools.util.html import sanitize
|
||||
|
||||
>>> input = """<html>
|
||||
... <p class="standard" style="font-size: 200%; font-weight: bold">
|
||||
... <a href="blubb"><b>Text</b></a>
|
||||
... </p>
|
||||
... </html>"""
|
||||
|
||||
>>> sanitize(input, validAttrs=['style'])
|
||||
u'\n<p style="font-weight: bold">\n<b>Text</b>\n</p>\n'
|
||||
|
||||
>>> sanitize(input, ['p', 'b'], ['class'])
|
||||
u'\n<p class="standard">\n<b>Text</b>\n</p>\n'
|
||||
|
|
@ -24,6 +24,7 @@ def test_suite():
|
|||
doctest.DocFileSuite('config.txt', optionflags=flags),
|
||||
doctest.DocFileSuite('defer.txt', optionflags=flags),
|
||||
doctest.DocFileSuite('format.txt', optionflags=flags),
|
||||
doctest.DocFileSuite('html.txt', optionflags=flags),
|
||||
doctest.DocFileSuite('multikey.txt', optionflags=flags),
|
||||
doctest.DocFileSuite('property.txt', optionflags=flags),
|
||||
doctest.DocFileSuite('json.txt', optionflags=flags),
|
||||
|
|
Loading…
Add table
Reference in a new issue