From 489d8cfa415ec3c573973225d1586b8bafd8a649 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 9 Jun 2025 12:32:14 +0200 Subject: [PATCH] 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. --- papis_bbt_formatter/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/papis_bbt_formatter/__init__.py b/papis_bbt_formatter/__init__.py index d60ab97..c585d9e 100644 --- a/papis_bbt_formatter/__init__.py +++ b/papis_bbt_formatter/__init__.py @@ -67,18 +67,24 @@ class BBTFormatter(papis.format.Formatter): else "UNKNOWN" ) 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") 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 either the full 4-digit year or a shortened 2-digit 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): - return str(year) - return str(year)[-2:] + return date_str + return date_str[-2:] def get_title(self, title: str) -> str: """Returns cleaned and shortened title.