11

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.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
  • Do you *just* want to remove unnecessary whitespace or are you looking for something more? Your title suggests whitespace only, but from your question you seem to be looking for a more complete tool. – Mark Byers Jan 26 '10 at 19:51
  • @Mark: Just removing whitespace alone should be sufficient, I believe. (Is there anything more than that at all?) – Sridhar Ratnakumar Jan 26 '10 at 19:53

3 Answers3

9

You might also investigate Jinja's built-in whitespace control, which might alleviate some of the need for manually removing whitespace after your templates have been rendered.

Quoting the docs:

But you can also strip whitespace in templates by hand. If you put an minus sign (-) to the start or end of an block (for example a for tag), a comment or variable expression you can remove the whitespaces after or before that block:

{% for item in seq -%}
    {{ item }}
{%- endfor %}

This will yield all elements without whitespace between them. If seq was a list of numbers from 1 to 9 the output would be 123456789.

Will McCutchen
  • 13,047
  • 3
  • 44
  • 43
4

I found python slimmer library, perfect for what you need to do.

from slimmer import html_slimmer # or xhtml_slimmer, css_slimmer
html = html_slimmer(html)
Pykler
  • 14,565
  • 9
  • 41
  • 50
-1

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).

Edward Loper
  • 15,374
  • 7
  • 43
  • 52