From 2f9a386c0db884ffbea27f43bdcff5bfd1876ad1 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Thu, 1 Jan 2015 18:59:53 +0100 Subject: [PATCH] [enh] better result template handling --- searx/templates/courgette/results.html | 2 +- searx/templates/default/results.html | 2 +- searx/templates/oscar/results.html | 6 +++++- searx/utils.py | 17 +++++++++++++++-- searx/webapp.py | 13 ++++++++++++- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/searx/templates/courgette/results.html b/searx/templates/courgette/results.html index 62bef8c90..8b5135c02 100644 --- a/searx/templates/courgette/results.html +++ b/searx/templates/courgette/results.html @@ -43,7 +43,7 @@ {% for result in results %} {% if result['template'] %} - {% include 'courgette/result_templates/'+result['template'] %} + {% include get_result_template('courgette', result['template']) %} {% else %} {% include 'courgette/result_templates/default.html' %} {% endif %} diff --git a/searx/templates/default/results.html b/searx/templates/default/results.html index 199eb1d96..822960593 100644 --- a/searx/templates/default/results.html +++ b/searx/templates/default/results.html @@ -59,7 +59,7 @@ {% for result in results %} {% if result['template'] %} - {% include 'default/result_templates/'+result['template'] %} + {% include get_result_template('default', result['template']) %} {% else %} {% include 'default/result_templates/default.html' %} {% endif %} diff --git a/searx/templates/oscar/results.html b/searx/templates/oscar/results.html index 69f8c9237..380947ca0 100644 --- a/searx/templates/oscar/results.html +++ b/searx/templates/oscar/results.html @@ -17,7 +17,11 @@ {% for result in results %}
{% set index = loop.index %} - {% if result.template %}{% include 'oscar/result_templates/'+result['template'] %}{% else %}{% include 'oscar/result_templates/default.html' %}{% endif %} + {% if result.template %} + {% include get_result_template('oscar', result['template']) %} + {% else %} + {% include 'oscar/result_templates/default.html' %} + {% endif %}
{% endfor %} diff --git a/searx/utils.py b/searx/utils.py index 0594339d5..5bd1ced4d 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -161,10 +161,23 @@ def get_themes(root): def get_static_files(base_path): + base_path = os.path.join(base_path, 'static') static_files = set() - base_path_length = len(base_path+'/static') + 1 - for directory, _, files in os.walk(os.path.join(base_path, 'static')): + base_path_length = len(base_path) + 1 + for directory, _, files in os.walk(base_path): for filename in files: f = os.path.join(directory[base_path_length:], filename) static_files.add(f) return static_files + + +def get_result_templates(base_path): + base_path = os.path.join(base_path, 'templates') + result_templates = set() + base_path_length = len(base_path) + 1 + for directory, _, files in os.walk(base_path): + if directory.endswith('result_templates'): + for filename in files: + f = os.path.join(directory[base_path_length:], filename) + result_templates.add(f) + return result_templates diff --git a/searx/webapp.py b/searx/webapp.py index 2e3c2c119..fca42d48e 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -39,7 +39,7 @@ from searx.engines import ( ) from searx.utils import ( UnicodeWriter, highlight_content, html_to_text, get_themes, - get_static_files + get_static_files, get_result_templates ) from searx.version import VERSION_STRING from searx.languages import language_codes @@ -58,6 +58,8 @@ default_theme = settings['server'].get('default_theme', 'default') static_files = get_static_files(searx_dir) +result_templates = get_result_templates(searx_dir) + app = Flask( __name__, static_folder=static_path, @@ -125,6 +127,13 @@ def get_current_theme_name(override=None): return theme_name +def get_result_template(theme, template_name): + themed_path = theme + '/result_templates/' + template_name + if themed_path in result_templates: + return themed_path + return 'result_templates/' + template_name + + def url_for_theme(endpoint, override_theme=None, **values): if endpoint == 'static' and values.get('filename'): theme_name = get_current_theme_name(override=override_theme) @@ -180,6 +189,8 @@ def render(template_name, override_theme=None, **kwargs): # override url_for function in templates kwargs['url_for'] = url_for_theme + kwargs['get_result_template'] = get_result_template + kwargs['theme'] = get_current_theme_name(override=override_theme) kwargs['template_name'] = template_name