Skip to content

Instantly share code, notes, and snippets.

@ssut
Created September 5, 2013 22:57
Show Gist options
  • Save ssut/6457376 to your computer and use it in GitHub Desktop.
Save ssut/6457376 to your computer and use it in GitHub Desktop.
Django html minify middleware.
#-*- coding: utf-8 -*-
import re
class MinifyMiddleware(object):
spaces = re.compile(r'^\s+', re.MULTILINE)
comments = re.compile(r'(\<!--\s*.*?((--\>)|$))', re.MULTILINE)
blank_attrs = re.compile(r'(\s\w+\=[\'|"]{2})', re.MULTILINE)
tag_types = re.compile(r'(\stype\=[\'|"]text\/\w+[\'|"])', re.MULTILINE)
def process_response(self, request, response):
if response['content-type'][:9] == 'text/html':
response.content = self.spaces.sub('', response.content.strip())
response.content = self.comments.sub('', response.content)
response.content = self.blank_attrs.sub('', response.content)
response.content = self.tag_types.sub('', response.content)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment