Skip to content

Commit

Permalink
Add find_languages function
Browse files Browse the repository at this point in the history
  • Loading branch information
twodoorcoupe committed Jan 25, 2024
1 parent c490589 commit 8baf3af
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions plugins/enhanced_titles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,8 @@ def _swapprefix(self, metadata, field):
Otherwise, if none of the languages are available, it just copies the title.
Otherwise, it uses exclusively the languages that are also available.
"""
languages = (metadata["language"], metadata["_releaselanguage"])
languages = set(language for language in languages if language)
if not languages:
prefixes = lang_functions.find_prefixes(languages)
return func_swapprefix(None, metadata[field], *prefixes)
languages = languages.intersection(_articles_langs)
if not languages:
languages = lang_functions.find_languages(metadata)
if languages and "" in languages:
return metadata[field]
prefixes = lang_functions.find_prefixes(languages)
return func_swapprefix(None, metadata[field], *prefixes)
Expand Down Expand Up @@ -287,6 +282,21 @@ def _title_case(self, text, lower_case_words):
else:
return ""

def find_languages(self, metadata):
"""Finds the languages from the metadata.
Returns None if no language information is found.
Returns a set with only an empty string if the languages found are not available.
"""
languages = (metadata["language"], metadata["_releaselanguage"])
languages = set(language for language in languages if language)
if not languages:
return None
languages = languages.intersection(_articles_langs)
if not languages:
return {""}
return languages

def find_prefixes(self, languages):
"""Returns the list of prefixes for the given languages.
Expand Down Expand Up @@ -342,6 +352,8 @@ def swapprefix_lang(self, parser, text, *languages):
>>> lf.swapprefix_lang(None, "the")
'the'
"""
if parser and not languages:
languages = self.find_languages(parser.context)
prefixes = self.find_prefixes(languages)
return func_swapprefix(parser, text, *prefixes)

Expand Down Expand Up @@ -370,6 +382,8 @@ def delprefix_lang(self, parser, text, *languages):
>>> lf.delprefix_lang(None, "the")
'the'
"""
if parser and not languages:
languages = self.find_languages(parser.context)
prefixes = self.find_prefixes(languages)
return func_delprefix(parser, text, *prefixes)

Expand Down Expand Up @@ -407,6 +421,8 @@ def title_lang(self, parser, text, *languages):
"""
if text.upper() == text and config.setting[KEEP_ALLCAPS]:
return text
if parser and not languages:
languages = self.find_languages(parser.context)
minor_words = self.find_minor_words(languages)
return self._title_case(text, minor_words)

Expand Down

0 comments on commit 8baf3af

Please sign in to comment.