Skip to content

Commit 8082998

Browse files
committed
Added prettifying middleware (from djangosnippet 172)
- Legacy-Id: 103
1 parent d927568 commit 8082998

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

ietf/middleware.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# From http://www.djangosnippets.org/snippets/172/
3+
# Uses python-utidylib, http://utidylib.berlios.de/,
4+
# which uses HTML Tidy, http://tidy.sourceforge.net/
5+
6+
import tidy
7+
8+
options = dict(
9+
output_xhtml=True,
10+
# add_xml_decl=True,
11+
# doctype='transitional',
12+
indent=True,
13+
tidy_mark=False,
14+
# hide_comments=True,
15+
wrap=100)
16+
17+
18+
class PrettifyMiddleware(object):
19+
"""Prettify middleware"""
20+
21+
def process_response(self, request, response):
22+
if response.headers['Content-Type'].split(';', 1)[0] in ['text/html']:
23+
content = response.content
24+
content = str(tidy.parseString(content, **options))
25+
response.content = content
26+
return response

ietf/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
'django.contrib.sessions.middleware.SessionMiddleware',
6767
'django.contrib.auth.middleware.AuthenticationMiddleware',
6868
'django.middleware.doc.XViewMiddleware',
69+
# 'ietf.middleware.PrettifyMiddleware',
6970
)
7071

7172
ROOT_URLCONF = 'ietf.urls'

0 commit comments

Comments
 (0)