From 0a6a9f3a59720b5c1254cadd65de7c15e65ea935 Mon Sep 17 00:00:00 2001 From: Arnab Chakraborty Date: Tue, 5 Nov 2024 14:22:08 +0530 Subject: [PATCH] Add custom function for title case to handle apostrophe --- picard/track.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/picard/track.py b/picard/track.py index fbaae45601..fccd4e5e70 100644 --- a/picard/track.py +++ b/picard/track.py @@ -321,6 +321,29 @@ def _customize_metadata(self): @staticmethod def _genres_to_metadata(genres, limit=None, minusage=0, filters='', join_with=None): + def titlize(text): + """Converts text to title case using custom rules. + + Capitalizes the first character of each word while converting remaining + characters to lowercase. Handles contractions properly by keeping the + apostrophe intact. + + Args: + text (str): The input string to be converted to title case. + + Returns: + str: The input text converted to title case format. + + Examples: + >>> titlize("children's music") + 'Children's Music' + >>> titlize("blues") + "Blues" + """ + return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", + lambda m: m.group(0)[0].upper() + m.group(0)[1:].lower(), + text) + if limit is not None and limit < 1: return [] @@ -335,7 +358,7 @@ def _genres_to_metadata(genres, limit=None, minusage=0, filters='', join_with=No # Find most common genres most_common_genres = genres.most_common(limit) - genres_list = [name.title() for name, _count in most_common_genres] + genres_list = [titlize(name) for name, _count in most_common_genres] genres_list.sort() # And generate the genre metadata tag