ContributedMiddleware: StripWhitespaceMiddleware.py

File StripWhitespaceMiddleware.py, 506 bytes (added by dougvanhorn at the gmail dot com, 10 years ago)

Strips superfluous line breaks and whitespace from response content.

Line 
1"""
2Tightens up response content by removed superflous line breaks and whitespace.
3By Doug Van Horn
4"""
5
6import re
7class StripWhitespaceMiddleware:
8    """
9    Strips leading and trailing whitespace from response content.
10    """
11
12    def __init__(self):
13        self.whitespace = re.compile('\s*\n+\s*')
14
15    def process_response(self, request, response):
16        new_content = self.whitespace.sub('\n', response.content)
17        response.content = new_content
18        return response
19
Back to Top