SearXNG is a free internet metasearch engine which aggregates results from various search services and databases. Users are neither tracked nor profiled.
Python bingbravedegoogleduckduckgogooglemetasearchprivacypythonqwantsearchsearch-enginesearxsearxngstartpageyahoo
[mod] typification of SearXNG: add new result type KeyValue
Markus Heiseraf5dbdf768d5
diff --git a/client/simple/src/less/definitions.less b/client/simple/src/less/definitions.lessindex 7dd5782aa..395a02cde 100644--- a/client/simple/src/less/definitions.less+++ b/client/simple/src/less/definitions.less@@ -57,6 +57,10 @@/// Answer Colors--color-answer-font: #444; // same as --color-base-font--color-answer-background: #fff;+ // colors of the KeyValue result class+ --color-result-keyvalue-col-table: #fdfbff;+ --color-result-keyvalue-odd: #fdfbff;+ --color-result-keyvalue-even: #fff;/// Results Colors--color-result-background: #fff;--color-result-border: #ddd;@@ -180,6 +184,10 @@/// Answer Colors--color-answer-font: #bbb; // same as --color-base-font--color-answer-background: #26292f;+ // colors of the KeyValue result class+ --color-result-keyvalue-col-table: #1e1e22;+ --color-result-keyvalue-odd: #1e1e22;+ --color-result-keyvalue-even: #26292f;/// Results Colors--color-result-background: #26292f;--color-result-border: #333;diff --git a/client/simple/src/less/result_types/keyvalue.less b/client/simple/src/less/result_types/keyvalue.lessnew file mode 100644index 000000000..1e2f80442--- /dev/null+++ b/client/simple/src/less/result_types/keyvalue.less@@ -0,0 +1,35 @@+/*+ Layout of the KeyValue result class+*/+#main_results .result-keyvalue {+ caption {+ padding: 0.8rem 0.5rem;+ font-style: italic;+ caption-side: bottom;+ background-color: var(--color-result-keyvalue-table);+ }++ .col-key {+ width: 25%;+ }++ table {+ word-break: break-word;+ table-layout: fixed;+ width: 100%;+ background-color: var(--color-result-keyvalue-table);+ }++ tr.odd {+ background-color: var(--color-result-keyvalue-odd);+ }++ tr.even {+ background-color: var(--color-result-keyvalue-even);+ }++ th,+ td {+ padding: 0.3rem 0.5rem;+ }+}diff --git a/client/simple/src/less/style.less b/client/simple/src/less/style.lessindex 93e8cb7ed..0454f121e 100644--- a/client/simple/src/less/style.less+++ b/client/simple/src/less/style.less@@ -1164,3 +1164,6 @@ summary.title {pre code {white-space: pre-wrap;}++// import layouts of the Result types+@import "result_types/keyvalue.less";diff --git a/docs/dev/result_types/main/keyvalue.rst b/docs/dev/result_types/main/keyvalue.rstnew file mode 100644index 000000000..2848976df--- /dev/null+++ b/docs/dev/result_types/main/keyvalue.rst@@ -0,0 +1,7 @@+.. _result_types.keyvalue:++=================+Key-Value Results+=================++.. automodule:: searx.result_types.keyvaluediff --git a/docs/dev/result_types/main_result.rst b/docs/dev/result_types/main_result.rstindex 0c9ca619e..a76ed5e88 100644--- a/docs/dev/result_types/main_result.rst+++ b/docs/dev/result_types/main_result.rst@@ -14,6 +14,7 @@ following types have been implemented so far ..:maxdepth: 2main/mainresult+ main/keyvalueThe :ref:`LegacyResult <LegacyResult>` is used internally for the results thathave not yet been typed. The templates can be used as orientation until thediff --git a/searx/result_types/__init__.py b/searx/result_types/__init__.pyindex 9af16b9b5..e9a115ec3 100644--- a/searx/result_types/__init__.py+++ b/searx/result_types/__init__.py@@ -13,7 +13,7 @@from __future__ import annotations-__all__ = ["Result", "MainResult", "EngineResults", "AnswerSet", "Answer", "Translations"]+__all__ = ["Result", "MainResult", "KeyValue", "EngineResults", "AnswerSet", "Answer", "Translations"]import abc@@ -21,6 +21,7 @@ from searx import enginelibfrom ._base import Result, MainResult, LegacyResultfrom .answer import AnswerSet, Answer, Translations+from .keyvalue import KeyValueclass ResultList(list, abc.ABC):@@ -30,6 +31,7 @@ class ResultList(list, abc.ABC):"""The collection of result types (which have already been implemented)."""Answer = Answer+ KeyValue = KeyValueMainResult = MainResultResult = ResultTranslations = Translationsdiff --git a/searx/result_types/keyvalue.py b/searx/result_types/keyvalue.pynew file mode 100644index 000000000..12efccc4b--- /dev/null+++ b/searx/result_types/keyvalue.py@@ -0,0 +1,49 @@+# SPDX-License-Identifier: AGPL-3.0-or-later+"""+Typification of the *keyvalue* results. Results of this type are rendered in+the :origin:`keyvalue.html <searx/templates/simple/result_templates/keyvalue.html>`+template.++----++.. autoclass:: KeyValue+ :members:+ :show-inheritance:++"""+# pylint: disable=too-few-public-methods++from __future__ import annotations++__all__ = ["KeyValue"]++import typing+from collections import OrderedDict++from ._base import MainResult+++class KeyValue(MainResult, kw_only=True):+ """Simple table view which maps *key* names (first col) to *values*+ (second col)."""++ template: str = "keyvalue.html"++ kvmap: dict[str, typing.Any] | OrderedDict[str, typing.Any]+ """Dictionary with keys and values. To sort keys, use :py:obj:`OrderedDict`."""++ caption: str = ""+ """Optional caption for this result."""++ key_title: str = ""+ """Optional title for the *key column*."""++ value_title: str = ""+ """Optional title for the *value column*."""++ def __hash__(self) -> int:+ """The KeyValues objects are checked for object identity, even if all+ fields of two results have the same values, they are different from each+ other.+ """+ return id(self)diff --git a/searx/templates/simple/result_templates/keyvalue.html b/searx/templates/simple/result_templates/keyvalue.htmlnew file mode 100644index 000000000..0495b46c6--- /dev/null+++ b/searx/templates/simple/result_templates/keyvalue.html@@ -0,0 +1,21 @@+<article class="result result-keyvalue {% if result.category -%}category-{{ result.category }}{%- endif -%}">+ <table>+ {%- if result.caption %}<caption>{{ result.caption }}</caption>{%- endif -%}+ {%- if result.key_title or result.value_title %}+ <thead>+ <tr>+ <th class="col-key" scope="col" >{{result.key_title}}</th>+ <th class="col-value" scope="col" >{{result.value_title}}</th>+ </tr>+ </thead>+ {%- endif -%}+ {%- for key, value in result.kvmap.items() -%}+ <tr class="{{ loop.cycle('odd', 'even') }}">+ <th class="col-key" scope="row">{{ key }}</th>{{- '' -}}+ <td class="col-value">{{ value }}</td>{{- '' -}}+ </tr>+ {%- endfor -%}+ </table>{{- '' -}}+ <div class="engines">{% for engine in result.engines %}<span>{{ engine }}</span>{% endfor %}</div>{{- '' -}}+ <div class="break"></div>{{- '' -}}+</article>