Mercurial > public > sg101
view gpp/core/html.py @ 370:e9a066db3f54
Adding some try/except around some key points in the code to protect against some IntegrityErrors. We log when these happens but otherwise keep going. Tickets: #160 and #169.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 06 Mar 2011 00:40:50 +0000 |
parents | b3b11edf91d8 |
children |
line wrap: on
line source
import html5lib from html5lib import sanitizer, treebuilders, treewalkers, serializer def sanitizer_factory(*args, **kwargs): san = sanitizer.HTMLSanitizer(*args, **kwargs) # This isn't available yet # san.strip_tokens = True return san def clean_html(buf): """Cleans HTML of dangerous tags and content.""" buf = buf.strip() if not buf: return buf p = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("dom"), tokenizer=sanitizer_factory) dom_tree = p.parseFragment(buf) walker = treewalkers.getTreeWalker("dom") stream = walker(dom_tree) s = serializer.htmlserializer.HTMLSerializer( omit_optional_tags=False, quote_attr_values=True) return s.render(stream) # vim: ts=4 sw=4