[mod] image results: add list of alternative formats (#6153)

* [mod] template images.html: reformatted for readability (no func change)

In preparation for upcoming changes, the template is being reformatted for
better readability; no functional changes are being made.

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>

* [mod] image results: add list of alternative formats

To test alternatives formats apply patch from below, query ``!flaticon bmw`` and
open the detail view for the image.

    diff --git a/searx/engines/flaticon.py b/searx/engines/flaticon.py
    index 06b6a8e25..d88388705 100644
    --- a/searx/engines/flaticon.py
    +++ b/searx/engines/flaticon.py
    @@ -8,7 +8,7 @@ from urllib.parse import urlencode

     import typing as t

    -from searx.result_types import EngineResults
    +from searx.result_types import EngineResults, ImageRef

     if t.TYPE_CHECKING:
         from searx.extended_types import SXNG_Response
    @@ -61,6 +61,14 @@ def response(resp: "SXNG_Response"):
                     thumbnail_src=_fix_url(result["png"]),
                     img_src=_fix_url(result["png512"]),
                     author=result["team_name"],
    +                formats=[
    +                    ImageRef(label="PNG 100x100", url="https://example.org/test.png", subtype="png"),
    +                    ImageRef(label="SVG", url="https://example.org/test.svg", subtype="svg+xml"),
    +                    ImageRef(url="https://example.org/test.jpg", subtype="jpeg"),
    +                    ImageRef(url="https://example.org/test.bmp", subtype="bmp"),
    +                    ImageRef(url="https://example.org/test.ico", subtype="x-icon"),
    +                    ImageRef(url="https://example.org/test.tif", subtype="tiff"),
    +                ],
                 )
             )

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>

---------

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser
2026-06-13 13:28:05 +02:00
committed by GitHub
parent b48205b384
commit e3bd7f5df1
5 changed files with 142 additions and 32 deletions
+3 -1
View File
@@ -24,6 +24,8 @@ __all__ = [
"Code",
"Paper",
"File",
"Image",
"ImageRef",
]
import typing as t
@@ -35,7 +37,7 @@ from .keyvalue import KeyValue
from .code import Code
from .paper import Paper
from .file import File
from .image import Image
from .image import Image, ImageRef
class ResultList(list[Result | LegacyResult], abc.ABC):
+66 -3
View File
@@ -7,14 +7,51 @@ template.
:members:
:show-inheritance:
.. autoclass:: ImageRef
:members:
"""
# pylint: disable=too-few-public-methods
__all__ = ["Image", "ImageRef"]
__all__ = ["Image"]
import types
import typing as t
from collections.abc import Callable
import msgspec
from ._base import MainResult, Result, log, LegacyResult
MimeSubType = t.Literal["png", "svg+xml", "jpeg", "bmp", "x-icon", "tiff"]
MIMESUB: dict[MimeSubType, str] = {
"png": "PNG",
"svg+xml": "SVG",
"jpeg": "JPG",
"bmp": "BMP",
"x-icon": "ICO",
"tiff": "TIF",
}
from ._base import MainResult
class ImageRef(msgspec.Struct, kw_only=True):
"""Reference to an (alternative) image format"""
url: str
"""URL of the image reference."""
subtype: MimeSubType
"""Subtype (mimetype) of the image format."""
label: str = ""
"""Label of the reference, default is build from the uppercase of
:py:obj:`Image.ImageRef.subtype`."""
mtype: t.Literal["image"] = "image"
def __post_init__(self):
if not self.label:
self.label = MIMESUB.get(self.subtype, self.subtype.upper())
@t.final
@@ -42,3 +79,29 @@ class Image(MainResult, kw_only=True):
filesize: str = ""
"""Size of bytes in :py:obj:`human readable <searx.humanize_bytes>` notation
(e.g. ``1MB`` for ``1024*1024`` Bytes filesize)."""
formats: list[ImageRef] = []
"""List of links to alternative image formats."""
def filter_urls(self, filter_func: "Callable[[Result | LegacyResult, str, str], str | bool ]"):
for _ref in self.formats[:]:
_name = f"Image.formats:{_ref.label}"
try:
_url = filter_func(self, _name, _ref.url)
except Exception as exc: # pylint: disable=broad-exception-caught
# pylint: disable=no-member
_tb: types.TracebackType = exc.__traceback__.tb_next.tb_next # type: ignore
_fn = _tb.tb_frame.f_code.co_filename
_lno = _tb.tb_lineno
log.error("filter_urls: [%s] ignore %s from callback %s:%s", _name, repr(exc), _fn, _lno)
continue
if isinstance(_url, str):
log.debug("filter_urls: [%s] URL %s -> %s", _name, _ref.url, _url)
_ref.url = _url
elif not _url:
log.debug("filter_urls: [%s] drop ref %s", _name, _ref)
self.formats.remove(_ref)
return super().filter_urls(filter_func)