Version 4 (modified by Cal Leeming, 8 years ago) (diff)


Strip Whitespace Middleware

Tightens up response content by removed superflous line breaks and whitespace.
By Doug Van Horn

v1.1 - 31st May 2011
Cal Leeming [Simplicity Media Ltd]
Modified regex to strip leading/trailing white space from every line, not just those with blank \n.
TODO: Ensure whitespace isn't stripped from within <pre> or <code> or <textarea> tags.

import re

class StripWhitespaceMiddleware:
    Strips leading and trailing whitespace from response content.

    def __init__(self):
        self.whitespace = re.compile('^\s+|\s+$', re.MULTILINE)

    def process_response(self, request, response):
        if "text" in response['Content-Type']:
            new_content = self.whitespace.sub('\n', response.content)
            response.content = new_content
            return response
            return response

Back to Top