feat: Handle year and date fields both

Can now not just handle an exact year field (with only the year
contained), but also the biblatex supported 'date' field from which it
will extract any 4-digit number as the year provided.
This commit is contained in:
Marty Oehme 2025-06-09 12:32:14 +02:00
parent 12fd3271fd
commit 489d8cfa41
Signed by: Marty
GPG key ID: 4E535BC19C61886E

View file

@ -67,18 +67,24 @@ class BBTFormatter(papis.format.Formatter):
else "UNKNOWN" else "UNKNOWN"
) )
author = re.sub("[^a-z]+", "", author_unfmt.lower()) author = re.sub("[^a-z]+", "", author_unfmt.lower())
year = self.get_year(int(doc["year"]) if "year" in doc else 0000) year = self.get_year(
doc["year"] if "year" in doc else doc["date"] if "date" in doc else ""
)
title = self.get_title(doc["title"] if "title" in doc else "NO TITLE") title = self.get_title(doc["title"] if "title" in doc else "NO TITLE")
return f"{author}{year}{title}" return f"{author}{year}{title}"
def get_year(self, year: int) -> str: def get_year(self, date: str) -> str:
"""Returns year string according to set year display options. """Returns year string according to set year display options.
Returns either the full 4-digit year or a shortened 2-digit Returns either the full 4-digit year or a shortened 2-digit
version depending on the plugin year options.""" version depending on the plugin year options."""
date_match = re.search(r"\d{4}", date)
if not date_match:
return "0000"
date_str = date_match[0]
if papis.config.getboolean("full-year", OPTIONS_SECTION): if papis.config.getboolean("full-year", OPTIONS_SECTION):
return str(year) return date_str
return str(year)[-2:] return date_str[-2:]
def get_title(self, title: str) -> str: def get_title(self, title: str) -> str:
"""Returns cleaned and shortened title. """Returns cleaned and shortened title.