Created
December 12, 2012 16:06
-
-
Save xfenix/4269014 to your computer and use it in GitHub Desktop.
Django output html minification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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