From f2e83c37e9e4e4fdd1de9d63e9d7692859bbcb75 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Tue, 9 Aug 2022 22:21:45 +0200 Subject: [PATCH] WebHost: use title-typical sorting for game titles (#883) --- Utils.py | 11 +++++++++++ WebHost.py | 2 +- WebHostLib/__init__.py | 2 ++ WebHostLib/templates/supportedGames.html | 3 ++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Utils.py b/Utils.py index 8f00a910..82d13b3f 100644 --- a/Utils.py +++ b/Utils.py @@ -603,3 +603,14 @@ def messagebox(title: str, text: str, error: bool = False) -> None: root.withdraw() showerror(title, text) if error else showinfo(title, text) root.update() + + +def title_sorted(data: typing.Sequence, key=None, ignore: typing.Set = frozenset(("a", "the"))): + """Sorts a sequence of text ignoring typical articles like "a" or "the" in the beginning.""" + def sorter(element: str) -> str: + parts = element.split(maxsplit=1) + if parts[0].lower() in ignore: + return parts[1] + else: + return element + return sorted(data, key=lambda i: sorter(key(i)) if key else sorter(i)) diff --git a/WebHost.py b/WebHost.py index 5d3c4459..09f8d823 100644 --- a/WebHost.py +++ b/WebHost.py @@ -85,7 +85,7 @@ def create_ordered_tutorials_file() -> typing.List[typing.Dict[str, typing.Any]] for games in data: if 'Archipelago' in games['gameTitle']: generic_data = data.pop(data.index(games)) - sorted_data = [generic_data] + sorted(data, key=lambda entry: entry["gameTitle"].lower()) + sorted_data = [generic_data] + Utils.title_sorted(data, key=lambda entry: entry["gameTitle"].lower()) json.dump(sorted_data, json_target, indent=2, ensure_ascii=False) return sorted_data diff --git a/WebHostLib/__init__.py b/WebHostLib/__init__.py index b2d243c9..b7bf4e38 100644 --- a/WebHostLib/__init__.py +++ b/WebHostLib/__init__.py @@ -9,6 +9,7 @@ from flask_caching import Cache from flask_compress import Compress from werkzeug.routing import BaseConverter +from Utils import title_sorted from .models import * UPLOAD_FOLDER = os.path.relpath('uploads') @@ -65,6 +66,7 @@ class B64UUIDConverter(BaseConverter): # short UUID app.url_map.converters["suuid"] = B64UUIDConverter app.jinja_env.filters['suuid'] = lambda value: base64.urlsafe_b64encode(value.bytes).rstrip(b'=').decode('ascii') +app.jinja_env.filters["title_sorted"] = title_sorted def register(): diff --git a/WebHostLib/templates/supportedGames.html b/WebHostLib/templates/supportedGames.html index 125551fb..fe81463a 100644 --- a/WebHostLib/templates/supportedGames.html +++ b/WebHostLib/templates/supportedGames.html @@ -10,7 +10,8 @@ {% include 'header/oceanHeader.html' %}

Currently Supported Games

- {% for game_name, world in worlds.items() | sort(attribute=0) %} + {% for game_name in worlds | title_sorted %} + {% set world = worlds[game_name] %}

{{ game_name }}

{{ world.__doc__ | default("No description provided.", true) }}