From 9fd314115a114b408f7f66d6ad173e24eeec6e18 Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Sun, 6 Nov 2022 15:12:51 +0100 Subject: [PATCH] thumbnail view: Hide common trails on media titles in collections --- server/app.py | 9 +++++ .../entertainment_decider/extras/__init__.py | 1 + .../entertainment_decider/extras/strings.py | 40 +++++++++++++++++++ server/templates/collection_element.htm | 1 + server/templates/collection_episodes.htm | 1 + server/templates/macros.htm | 5 ++- 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 server/entertainment_decider/extras/strings.py diff --git a/server/app.py b/server/app.py index 44e2853..583f54a 100644 --- a/server/app.py +++ b/server/app.py @@ -66,6 +66,7 @@ from entertainment_decider.extractors.media import ( media_extract_uri, media_update, ) +from entertainment_decider.extras import remove_common_trails T = TypeVar("T") @@ -489,10 +490,16 @@ def show_collection(collection_id: int) -> ResponseReturnValue: if orm.count(collection.media_links) <= SMALL_COLLECTION_MAX_COUNT else None ) + media_titles = ( + remove_common_trails([link.element.title for link in media_links]) + if media_links is not None + else None + ) return render_template( "collection_element.htm", collection=collection, media_links=media_links, + media_titles=media_titles, ) @@ -504,10 +511,12 @@ def show_collection_episodes(collection_id: int) -> ResponseReturnValue: media_links = MediaCollectionLink.sorted( MediaCollectionLink.select(lambda l: l.collection == collection) ) + media_titles = remove_common_trails([link.element.title for link in media_links]) return render_template( "collection_episodes.htm", collection=collection, media_links=media_links, + media_titles=media_titles, ) diff --git a/server/entertainment_decider/extras/__init__.py b/server/entertainment_decider/extras/__init__.py index 5da4ee9..d3dc01f 100644 --- a/server/entertainment_decider/extras/__init__.py +++ b/server/entertainment_decider/extras/__init__.py @@ -1 +1,2 @@ from .chain import Chain +from .strings import remove_common_trails diff --git a/server/entertainment_decider/extras/strings.py b/server/entertainment_decider/extras/strings.py new file mode 100644 index 0000000..e7cad83 --- /dev/null +++ b/server/entertainment_decider/extras/strings.py @@ -0,0 +1,40 @@ +import re +from typing import Sequence + + +def common_prefix(elements: Sequence[str]) -> str: + if len(elements) <= 1: + return "" + test = re.compile(r"^[^\w]$") + s1 = min(elements) + s2 = max(elements) + i = 0 + min_l = min(len(s1), len(s2)) + if min_l <= 0: + return "" + for i in range(min_l): + if s1[i] != s2[i]: + break + else: + i += 1 + while i > 0 and not test.match(s1[i - 1]): + i -= 1 + return s1[:i] + + +def common_suffix(elements: Sequence[str]) -> str: + return common_prefix([s[::-1] for s in elements])[::-1] + + +def remove_prefix(elements: Sequence[str]) -> Sequence[str]: + pre = len(common_prefix(elements)) + return [s[pre:] for s in elements] + + +def remove_suffix(elements: Sequence[str]) -> Sequence[str]: + suf = len(common_suffix(elements)) + return [s[::-1][suf:][::-1] for s in elements] + + +def remove_common_trails(elements: Sequence[str]) -> Sequence[str]: + return remove_prefix(remove_suffix(elements)) diff --git a/server/templates/collection_element.htm b/server/templates/collection_element.htm index 384321e..5b503a5 100644 --- a/server/templates/collection_element.htm +++ b/server/templates/collection_element.htm @@ -114,6 +114,7 @@ links=media_links, check_considered=True, link_collection=False, + titles=media_titles, ) }} {% endif %} diff --git a/server/templates/collection_episodes.htm b/server/templates/collection_episodes.htm index 0aa8f70..204c0be 100644 --- a/server/templates/collection_episodes.htm +++ b/server/templates/collection_episodes.htm @@ -16,6 +16,7 @@ links=media_links, check_considered=True, link_collection=False, + titles=media_titles, ) }} diff --git a/server/templates/macros.htm b/server/templates/macros.htm index 51dfae4..9db4663 100644 --- a/server/templates/macros.htm +++ b/server/templates/macros.htm @@ -384,6 +384,7 @@ check_considered=True, is_considered=True, link_collection=True, + title=None, ) %} {% set element = link.element if link else element %}
@@ -445,7 +446,7 @@
- {{ element.title }} + {{ title or element.title }} {%- endmacro %} @@ -455,6 +456,7 @@ links=None, check_considered=True, link_collection=True, + titles=None, ) %} {%- set l = elements or links -%} {%- set considered = (links|map(attribute="element") if links else elements)|map(attribute="id")|are_considered -%} @@ -467,6 +469,7 @@ check_considered=False, is_considered=considered[elem.id] if check_considered else True, link_collection=link_collection, + title=titles[loop.index0] if titles else None, ) }} {% endfor %}