Skip to content

Instantly share code, notes, and snippets.

@xfenix
Created December 12, 2012 16:06
Show Gist options
  • Save xfenix/4269014 to your computer and use it in GitHub Desktop.
Save xfenix/4269014 to your computer and use it in GitHub Desktop.
Django output html minification
import re
from django.utils.html import strip_spaces_between_tags
from django.conf import settings
RE_MULTISPACE = re.compile(r"\s{2,}")
RE_NEWLINE = re.compile(r"\n")
class MinifyHTMLMiddleware(object):
def process_response(self, request, response):
if 'text/html' in response['Content-Type'] and settings.COMPRESS_HTML and not re.match('^/admin/.*$', request.path):
response.content = strip_spaces_between_tags(response.content.strip())
response.content = RE_MULTISPACE.sub(" ", response.content)
response.content = RE_NEWLINE.sub("", response.content)
return response
MIDDLEWARE_CLASSES = (
'app_name.output_minify.MinifyHTMLMiddleware',
)
COMPRESS_HTML = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment