ContributedMiddleware: StripWhitespaceMiddleware.py
| File StripWhitespaceMiddleware.py, 506 bytes (added by dougvanhorn at the gmail dot com, 2 years ago) |
|---|
| Line | |
|---|---|
| 1 | """ |
| 2 | Tightens up response content by removed superflous line breaks and whitespace. |
| 3 | By Doug Van Horn |
| 4 | """ |
| 5 | |
| 6 | import re |
| 7 | class 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 |
