From dd84814b68369e36d6be20bae707848a051a025a Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Sun, 12 Apr 2015 19:24:01 +0200 Subject: [PATCH] [enh] plugins: client side dependency support --- searx/plugins/__init__.py | 6 ++++++ searx/templates/oscar/base.html | 14 ++++++++++---- searx/webapp.py | 10 ++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/searx/plugins/__init__.py b/searx/plugins/__init__.py index 1cc232560..f3cf1bf2b 100644 --- a/searx/plugins/__init__.py +++ b/searx/plugins/__init__.py @@ -8,6 +8,9 @@ required_attrs = (('name', str), ('description', str), ('default_on', bool)) +optional_attrs = (('js_dependencies', tuple), + ('css_dependencies', tuple)) + class Plugin(): default_on = False @@ -30,6 +33,9 @@ class PluginStore(): if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type): logger.critical('missing attribute "{0}", cannot load plugin: {1}'.format(plugin_attr, plugin)) exit(3) + for plugin_attr, plugin_attr_type in optional_attrs: + if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type): + setattr(plugin, plugin_attr, plugin_attr_type()) plugin.id = plugin.name.replace(' ', '_') self.plugins.append(plugin) diff --git a/searx/templates/oscar/base.html b/searx/templates/oscar/base.html index c185f8774..88eedc994 100644 --- a/searx/templates/oscar/base.html +++ b/searx/templates/oscar/base.html @@ -9,17 +9,20 @@ {% block meta %}{% endblock %} {% block title %}{% endblock %}searx - + - + + {% for css in styles %} + + {% endfor %} - + {% block styles %} @@ -28,7 +31,7 @@ {% endblock %} - + {% endif %} + {% for script in scripts %} + + {% endfor %} diff --git a/searx/webapp.py b/searx/webapp.py index cbdff8962..89ab9b543 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -301,6 +301,16 @@ def render(template_name, override_theme=None, **kwargs): kwargs['cookies'] = request.cookies + kwargs['scripts'] = set() + for plugin in request.user_plugins: + for script in plugin.js_dependencies: + kwargs['scripts'].add(script) + + kwargs['styles'] = set() + for plugin in request.user_plugins: + for css in plugin.css_dependencies: + kwargs['styles'].add(css) + return render_template( '{}/{}'.format(kwargs['theme'], template_name), **kwargs)