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