1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import re
class StripHtmlCommentsMiddleware:
"""
Strips all html comments from response content.
"""
def __init__(self):
self.htmlcomments = re.compile('\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>')
def process_response(self, request, response):
if ("text" in response['Content-Type']):
new_content = self.htmlcomments.sub('', response.content)
response.content = new_content
return response
else:
return response
|
More like this
- Strip trailing .html extensions from URLs so that existing bookmarks work for a legacy site ported to Django by daverowell 5 years ago
- Simple profile middleware by limodou 5 years, 2 months ago
- Updated version of StripWhitespaceMiddleware (v1.1) by sleepycal 12 months ago
- strip_tags like php one by homm 2 years ago
- Remove self links middleware by svetlyak 4 years, 1 month ago
Comments
Hi, nice code! Something to note: in case you want some comment to pass through (like the conditionals for IE to load extra css), you can make it adding an extra '-' in the comment tag like this:
Cheers, Pedro
#