From d7e0fffb124f8c06a67d39e3ae2b78336580f79e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Schneider?= Date: Thu, 11 Dec 2014 14:42:43 +0100 Subject: [PATCH] add option 'scripts' to mapper which allows to load javascripts in header Closes #6184 --- mandaye/dispatcher.py | 9 +++++++++ mandaye/filters/default.py | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/mandaye/dispatcher.py b/mandaye/dispatcher.py index ed59ac8..3adb812 100644 --- a/mandaye/dispatcher.py +++ b/mandaye/dispatcher.py @@ -6,6 +6,7 @@ from urlparse import urlparse from importlib import import_module from mandaye import config +from mandaye.filters.default import MandayeFilter from mandaye.http import HTTPRequest, HTTPResponse from mandaye.log import logger from mandaye.mappers import default @@ -66,6 +67,14 @@ class Dispatcher(object): """ if not mapper.has_key('method') or \ mapper['method'] == self.env['REQUEST_METHOD']: + if mapper.has_key('scripts'): + req_mapping["on_response"].append( + { + 'filter': MandayeFilter.add_js_header, + 'values': {'scripts': mapper['scripts']}, + 'content-types': ['text/html'] + } + ) for hookname in req_mapping: if mapper.has_key(hookname): if isinstance(req_mapping[hookname], list): diff --git a/mandaye/filters/default.py b/mandaye/filters/default.py index 3cfd74a..5be4cb8 100644 --- a/mandaye/filters/default.py +++ b/mandaye/filters/default.py @@ -58,6 +58,28 @@ class MandayeFilter(object): '%s://%s' % (env["mandaye.scheme"], env["HTTP_HOST"])) return response + @staticmethod + def add_js_header(env, values, request, response): + if response.msg: + if not "mjQuery = jQuery.noConflict" in response.msg: + response.msg = re.sub( + r'', + r""" + + """ % (config.static_url), + response.msg, 1, re.IGNORECASE) + scripts_html = "" + for script in values["scripts"]: + scripts_html += '' % \ + (config.static_url, script) + response.msg = re.sub( + '', + '%s' % scripts_html, + response.msg, 1, re.IGNORECASE) + return response + @staticmethod def addtoolbar(env, values, request, response): if config.mandaye_offline_toolbar or \