From 8e9ad1ccc296c220d61f12926c94d98baa83e3ca Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Wed, 22 Dec 2021 15:51:26 +0100 Subject: [PATCH] [enh] introduce categories_as_tabs Previously all categories were displayed as search engine tabs. This commit changes that so that only the categories listed under categories_as_tabs in settings.yml are displayed. This lets us introduce more categories without cluttering up the UI. Categories not displayed as tabs can still be searched with !bangs. --- docs/admin/engines/configured_engines.rst | 12 ++++++++++-- docs/admin/engines/settings.rst | 20 +++++++++++++++++++ docs/conf.py | 7 ++----- searx/engines/__init__.py | 24 +++++++++++++++++++++++ searx/settings.yml | 18 +++++++++++------ searx/settings_defaults.py | 4 ++-- searx/templates/oscar/categories.html | 4 ++-- searx/templates/oscar/preferences.html | 4 ++-- searx/templates/simple/categories.html | 2 +- searx/templates/simple/preferences.html | 2 +- searx/webapp.py | 10 ++-------- tests/robot/settings_robot.yml | 4 ++++ 12 files changed, 82 insertions(+), 29 deletions(-) diff --git a/docs/admin/engines/configured_engines.rst b/docs/admin/engines/configured_engines.rst index 82a1d2375..c7b6a1f52 100644 --- a/docs/admin/engines/configured_engines.rst +++ b/docs/admin/engines/configured_engines.rst @@ -16,11 +16,18 @@ Explanation of the :ref:`general engine configuration` shown in the table SearXNG supports {{engines | length}} search engines (of which {{enabled_engine_count}} are enabled by default). - {% for category, engines in categories.items() %} + {% for category, engines in categories_as_tabs.items() %} {{category}} search engines --------------------------------------- + {% for group, engines in engines | group_engines_in_tab %} + + {% if loop.length > 1 %} + {{group}} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + {% endif %} + .. flat-table:: :header-rows: 2 :stub-columns: 1 @@ -39,7 +46,7 @@ Explanation of the :ref:`general engine configuration` shown in the table - Safe search - Time range - {% for mod in engines | sort_engines %} + {% for mod in engines %} * - `{{mod.name}} <{{mod.about and mod.about.website}}>`_ - ``!{{mod.shortcut}}`` @@ -65,3 +72,4 @@ Explanation of the :ref:`general engine configuration` shown in the table {% endfor %} {% endfor %} + {% endfor %} diff --git a/docs/admin/engines/settings.rst b/docs/admin/engines/settings.rst index b04de7cb3..cf561a5ab 100644 --- a/docs/admin/engines/settings.rst +++ b/docs/admin/engines/settings.rst @@ -219,6 +219,26 @@ Communication with search engines. ``max_redirects`` : 30 by default. Maximum redirect before it is an error. +``categories_as_tabs:`` +----------------------- + +A list of the categories that are displayed as tabs in the user interface. +Categories not listed here can still be searched with the :ref:`search-syntax`. + +.. code-block:: yaml + + categories_as_tabs: + - general + - images + - videos + - news + - map + - music + - it + - science + - files + - social media + .. _settings engine: Engine settings diff --git a/docs/conf.py b/docs/conf.py index 1b78ece60..da989e62e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -50,14 +50,11 @@ jinja_contexts = { }, 'enabled_engine_count': sum(not x.disabled for x in searx.engines.engines.values()), 'categories': searx.engines.categories, + 'categories_as_tabs': {c: searx.engines.categories[c] for c in searx.settings['categories_as_tabs']}, }, } jinja_filters = { - 'sort_engines': - lambda engines: sorted( - engines, - key=lambda engine: (engine.about.get('language', ''), engine.name) - ) + 'group_engines_in_tab': searx.engines.group_engines_in_tab, } # Let the Jinja template in configured_engines.rst access documented_modules diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py index 1fc01600a..8d49bae8a 100644 --- a/searx/engines/__init__.py +++ b/searx/engines/__init__.py @@ -13,6 +13,7 @@ usage:: import sys import copy +import itertools from os.path import realpath, dirname from babel.localedata import locale_identifiers @@ -260,3 +261,26 @@ def load_engines(engine_list): if engine: register_engine(engine) return engines + + +DEFAULT_GROUP_NAME = 'others' + + +def group_engines_in_tab(engines): # pylint: disable=redefined-outer-name + def engine_sort_key(engine): + return (engine.about.get('language', ''), engine.name) + + def group_sort_key(group): + return (group[0] == DEFAULT_GROUP_NAME, group[0].lower()) + + def get_group(eng): + non_tab_engines = [c for c in eng.categories if c not in settings['categories_as_tabs']] + return non_tab_engines[0] if len(non_tab_engines) > 0 else DEFAULT_GROUP_NAME + + return [ + (groupname, sorted(engines, key=engine_sort_key)) + for groupname, engines in sorted( + ((name, list(engines)) for name, engines in itertools.groupby(sorted(engines, key=get_group), get_group)), + key=group_sort_key, + ) + ] diff --git a/searx/settings.yml b/searx/settings.yml index 3227a5a55..18d047535 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -81,12 +81,6 @@ ui: simple_style: auto # Open result links in a new tab by default # results_on_new_tab: false - # categories_order : - # - general - # - files - # - map - # - it - # - science # Lock arbitrary settings on the preferences page. To find the ID of the user # setting you want to lock, check the ID of the form on the page "preferences". @@ -233,6 +227,18 @@ checker: result_container: - has_infobox +categories_as_tabs: + - general + - images + - videos + - news + - map + - music + - it + - science + - files + - social media + engines: - name: apk mirror engine: apkmirror diff --git a/searx/settings_defaults.py b/searx/settings_defaults.py index 9c4711bfc..d8e3ec693 100644 --- a/searx/settings_defaults.py +++ b/searx/settings_defaults.py @@ -20,7 +20,7 @@ OUTPUT_FORMATS = ['html', 'csv', 'json', 'rss'] LANGUAGE_CODES = ['all'] + list(l[0] for l in languages) OSCAR_STYLE = ('logicodev', 'logicodev-dark', 'pointhi') SIMPLE_STYLE = ('auto', 'light', 'dark') -CATEGORY_ORDER = [ +CATEGORIES_AS_TABS = [ 'general', 'images', 'videos', @@ -181,7 +181,6 @@ SCHEMA = { 'results_on_new_tab': SettingsValue(bool, False), 'advanced_search': SettingsValue(bool, False), 'query_in_title': SettingsValue(bool, False), - 'categories_order': SettingsValue(list, CATEGORY_ORDER), }, 'preferences': { 'lock': SettingsValue(list, []), @@ -212,6 +211,7 @@ SCHEMA = { 'checker': { 'off_when_debug': SettingsValue(bool, True), }, + 'categories_as_tabs': SettingsValue(list, CATEGORIES_AS_TABS), 'engines': SettingsValue(list, []), 'doi_resolvers': {}, } diff --git a/searx/templates/oscar/categories.html b/searx/templates/oscar/categories.html index a5c5f61c7..39d1e1d33 100644 --- a/searx/templates/oscar/categories.html +++ b/searx/templates/oscar/categories.html @@ -1,11 +1,11 @@
{%- if rtl -%} - {% for category in categories | reverse -%} + {% for category in categories_as_tabs | reverse -%} {{- '' -}} {%- endfor %} {%- else -%} - {% for category in categories -%} + {% for category in categories_as_tabs -%} {{- '' -}} {%- endfor %} diff --git a/searx/templates/oscar/preferences.html b/searx/templates/oscar/preferences.html index b76ee44a1..da88cd755 100644 --- a/searx/templates/oscar/preferences.html +++ b/searx/templates/oscar/preferences.html @@ -298,7 +298,7 @@
@@ -317,7 +317,7 @@

- {% for categ in all_categories %} + {% for categ in categories_as_tabs %}
diff --git a/searx/templates/simple/categories.html b/searx/templates/simple/categories.html index ae8b2ec09..71a211904 100644 --- a/searx/templates/simple/categories.html +++ b/searx/templates/simple/categories.html @@ -14,7 +14,7 @@
{{- '' -}}
{%- if display_tooltip %}
{{ _('Click on the magnifier to perform search') }}
{% endif -%} - {%- for category in categories -%} + {%- for category in categories_as_tabs -%}