def clean_html5lib(input):
from html5lib import treebuilders, treewalkers, serializer, sanitizer<EOL>p = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("<STR_LIT>"))<EOL>dom_tree = p.parseFragment(input)<EOL>walker = treewalkers.getTreeWalker("<STR_LIT>")<EOL>stream = walker(dom_tree)<EOL>s = serializer.htmlserializer.HTMLSerializer(omit_optional_tags=False)<EOL>return "<STR_LIT>".join(s.serialize(stream))<EOL>
Takes an HTML fragment and processes it using html5lib to ensure that the HTML is well-formed.

>>> clean_html5lib("<p>Foo<b>bar</b></p>")
u'<p>Foo<b>bar</b></p>'
>>> clean_html5lib("<p>Foo<b>bar</b><i>Ooops!</p>")
u'<p>Foo<b>bar</b><i>Ooops!</i></p>'
>>> clean_html5lib('<p>Foo<b>bar</b>& oops<a href="#foo&bar">This is a <>link</a></p>')
u'<p>Foo<b>bar</b>&amp; oops<a href=#foo&amp;bar>This is a &lt;&gt;link</a></p>'