# HG changeset patch # User neko259 # Date 2013-12-17 13:47:46 # Node ID eb01481cbe021a321ba116283a7c5b93725c15e9 # Parent 898ed369a8b2313694a570dcc8827d719bdd7330 Added a middleware to remove spaces between tags in HTML code diff --git a/boards/middlewares.py b/boards/middlewares.py --- a/boards/middlewares.py +++ b/boards/middlewares.py @@ -1,11 +1,19 @@ from django.shortcuts import redirect from boards import views, utils from boards.models import Ban +from django.utils.html import strip_spaces_between_tags +from django.conf import settings + +RESPONSE_CONTENT_TYPE = 'Content-Type' + +TYPE_HTML = 'text/html' class BanMiddleware: - """This is run before showing the thread. Banned users don't need to see - anything""" + """ + This is run before showing the thread. Banned users don't need to see + anything + """ def process_view(self, request, view_func, view_args, view_kwargs): @@ -16,4 +24,17 @@ class BanMiddleware: if bans.exists(): ban = bans[0] if not ban.can_read: - return redirect(views.you_are_banned) \ No newline at end of file + return redirect(views.you_are_banned) + + +class MinifyHTMLMiddleware(object): + def process_response(self, request, response): + try: + compress_html = settings.COMPRESS_HTML + except AttributeError: + compress_html = False + + if TYPE_HTML in response[RESPONSE_CONTENT_TYPE] and compress_html: + response.content = strip_spaces_between_tags( + response.content.strip()) + return response \ No newline at end of file diff --git a/neboard/settings.py b/neboard/settings.py --- a/neboard/settings.py +++ b/neboard/settings.py @@ -114,6 +114,7 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'boards.middlewares.BanMiddleware', + 'boards.middlewares.MinifyHTMLMiddleware', ) ROOT_URLCONF = 'neboard.urls' @@ -220,6 +221,7 @@ ENABLE_CAPTCHA = False CAPTCHA_DEFAULT_SAFE_TIME = 30 # seconds POSTING_DELAY = 20 # seconds +COMPRESS_HTML = True def custom_show_toolbar(request): return DEBUG