Added prettifying middleware (from djangosnippet 172)
- Legacy-Id: 103
This commit is contained in:
parent
d927568b25
commit
80829985a0
26
ietf/middleware.py
Normal file
26
ietf/middleware.py
Normal 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
|
|
@ -66,6 +66,7 @@ MIDDLEWARE_CLASSES = (
|
|||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.middleware.doc.XViewMiddleware',
|
||||
# 'ietf.middleware.PrettifyMiddleware',
|
||||
)
|
||||
|
||||
ROOT_URLCONF = 'ietf.urls'
|
||||
|
|
Loading…
Reference in a new issue