Added prettifying middleware (from djangosnippet 172)

- Legacy-Id: 103
This commit is contained in:
Henrik Levkowetz 2007-05-06 05:14:25 +00:00
parent d927568b25
commit 80829985a0
2 changed files with 27 additions and 0 deletions

26
ietf/middleware.py Normal file
View file

@ -0,0 +1,26 @@
# From http://www.djangosnippets.org/snippets/172/
# Uses python-utidylib, http://utidylib.berlios.de/,
# which uses HTML Tidy, http://tidy.sourceforge.net/
import tidy
options = dict(
output_xhtml=True,
# add_xml_decl=True,
# doctype='transitional',
indent=True,
tidy_mark=False,
# hide_comments=True,
wrap=100)
class PrettifyMiddleware(object):
"""Prettify middleware"""
def process_response(self, request, response):
if response.headers['Content-Type'].split(';', 1)[0] in ['text/html']:
content = response.content
content = str(tidy.parseString(content, **options))
response.content = content
return response

View file

@ -66,6 +66,7 @@ MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware', 'django.middleware.doc.XViewMiddleware',
# 'ietf.middleware.PrettifyMiddleware',
) )
ROOT_URLCONF = 'ietf.urls' ROOT_URLCONF = 'ietf.urls'