Strip whitespace in generated HTML using pure Python code
html, jinja2, python, strip, whitespace
Solution
If you just want to get rid of excess whitespace, you can use:
>>> import re
>>> html_string = re.sub(r'\s\s+', ' ', html_string)
or:
>>> html_string = ' '.join(html_string.split())
If you want to do something more complicated than just stripping excess whitespace, you'll need to use more powerful tools (or more complex regexps).
Problem
I am using Jinja2 to generate HTML files which are typically very huge in size. I noticed that the generated HTML had a lot of whitespace. Is there a pure-Python tool that I can use to minimize this HTML? When I say "minimize", I mean remove unnecessary whitespace from the HTML (much like Google does -- look at the source for google.com, for instance) I don't want to rely on libraries/external-executables such as tidy for this. For further clarification, there is virtually no JavaScript code. Only HTML content.