Kozea/WeasyPrint — pull request #112 by SimonSapin (2013-07-22 13:31:59+00:00)
Title: Switch to html5lib to parse HTML. Fix #12. *Do not merge yet*

**Not ready for merging.**

Recent html5lib versions:

* Run on Python 3.x and 2.x with the same code base.
* Have a `namespaceHTMLElements` parameter that can be set to `False` to work around cssselect’s namespace issues described in #12.

… so nothing blocks WeasyPrint from using it anymore. This HTML parser is much closer to what web browsers do (Yay standards!), easier to upgrade (pure Python), and is generally less buggy. (See #65 for example.)

Rather than adding API to choose between parsers, this patch goes with simplicity and always uses html5lib. If a user really want libxml2’s parser for some reason, they can pass a parsed tree to `weasyprint.HTML(tree=…)`.

html5lib is much slower at parsing than libxml2, but the difference is dominated by the time taken by the rest of WeasyPrint. Quick unscientific testing shows a 2~5% slow-down on my machine, which seems acceptable.

Before this is merged, I’d like to get feedback from users on real documents:

* Is the performance acceptable?
* Does this break something? Is it easy to fix?

The recommended way to try this branch is to run this in a virtualenv:

pip uninstall WeasyPrint
pip install git+https://github.com/Kozea/WeasyPrint.git@html5lib

If you pull this branch in a long-lived repository, be advised that I may rebase it amend it as long as it is not merged into master.