piwigo: Let getStatus return the current user.
[mediagoblin.git] / mediagoblin / plugins / trim_whitespace / __init__.py
CommitLineData
99454686
SS
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16from __future__ import unicode_literals
17import logging
18import re
19
20from mediagoblin import meddleware
21
22_log = logging.getLogger(__name__)
23
24class TrimWhiteSpaceMeddleware(meddleware.BaseMeddleware):
25 _setup_plugin_called = 0
26 RE_MULTI_WHITESPACE = re.compile(b'(\s)\s+', re.M)
27
28 def process_response(self, request, response):
29 """Perform very naive html tidying by removing multiple whitespaces"""
30 # werkzeug.BaseResponse has no content_type attr, this comes via
31 # werkzeug.wrappers.CommonRequestDescriptorsMixin (part of
32 # wrappers.Response)
33 if getattr(response ,'content_type', None) != 'text/html':
34 return
35
36 # This is a tad more complex than needed to be able to handle
37 # response.data and response.body, depending on whether we have
38 # a werkzeug Resonse or a webob one. Let's kill webob soon!
39 if hasattr(response, 'body') and not hasattr(response, 'data'):
40 # Old-style webob Response object.
41 # TODO: Remove this once we transition away from webob
42 resp_attr = 'body'
43 else:
44 resp_attr = 'data'
45 # Don't flatten iterator to list when we fudge the response body
46 # (see werkzeug.Response documentation)
47 response.implicit_sequence_conversion = False
48
49 # Set the tidied text. Very naive tidying for now, just strip all
50 # subsequent whitespaces (this preserves most newlines)
51 setattr(response, resp_attr, re.sub(
52 TrimWhiteSpaceMeddleware.RE_MULTI_WHITESPACE, br'\1',
53 getattr(response, resp_attr)))
54
55 @classmethod
56 def setup_plugin(cls):
57 """Set up this meddleware as a plugin during 'setup' hook"""
58 global _log
59 if cls._setup_plugin_called:
60 _log.info('Trim whitespace plugin was already set up.')
61 return
62
63 _log.debug('Trim whitespace plugin set up.')
64 cls._setup_plugin_called += 1
65
66 # Append ourselves to the list of enabled Meddlewares
67 meddleware.ENABLED_MEDDLEWARE.append(
68 '{0}:{1}'.format(cls.__module__, cls.__name__))
69
70
71hooks = {
72 'setup': TrimWhiteSpaceMeddleware.setup_plugin
73 }