|
3 | 3 | import logging |
4 | 4 | import sys |
5 | 5 |
|
| 6 | +from datetime import datetime |
| 7 | + |
6 | 8 | from .utils import ( |
7 | 9 | extract_keywords, |
8 | 10 | format_authors_for_bibtex, |
@@ -119,7 +121,18 @@ def create_bibtex_from_cff() -> bool | None: |
119 | 121 | # Validate required fields |
120 | 122 | authors = pref.get("authors", []) |
121 | 123 | title = pref.get("title", "Untitled") |
122 | | - year = str(pref.get("year", "")) # Ensure year is a string for generate_citation_key |
| 124 | + # Try 'year' field first, fall back to extracting year from 'date-released' |
| 125 | + year = pref.get("year", "") |
| 126 | + if not year and "date-released" in pref: |
| 127 | + date_released = str(pref["date-released"]) |
| 128 | + year = date_released[:4] if len(date_released) >= 4 else "" |
| 129 | + if not year and "date-released" in citation_data: |
| 130 | + date_released = str(citation_data["date-released"]) |
| 131 | + year = date_released[:4] if len(date_released) >= 4 else "" |
| 132 | + if not year: |
| 133 | + year = str(datetime.now().year) |
| 134 | + logger.info("No year or date-released found, using current year: %s", year) |
| 135 | + year = str(year) # Ensure year is a string for generate_citation_key |
123 | 136 |
|
124 | 137 | if not authors: |
125 | 138 | logger.warning("No authors found in CITATION.cff") |
@@ -175,6 +188,18 @@ def create_bibtex_from_cff() -> bool | None: |
175 | 188 | if "version" in pref: |
176 | 189 | bibtex_lines.append(f" version = {{{pref['version']}}},") |
177 | 190 |
|
| 191 | + # Extract DOI from 'identifiers' list if not present as a top-level field |
| 192 | + if "doi" not in pref and "identifiers" in pref: |
| 193 | + for identifier in pref["identifiers"]: |
| 194 | + if isinstance(identifier, dict) and identifier.get("type") == "doi": |
| 195 | + bibtex_lines.append(f" doi = {{{identifier['value']}}},") |
| 196 | + break |
| 197 | + if "doi" not in pref and "identifiers" not in pref and "identifiers" in citation_data: |
| 198 | + for identifier in citation_data["identifiers"]: |
| 199 | + if isinstance(identifier, dict) and identifier.get("type") == "doi": |
| 200 | + bibtex_lines.append(f" doi = {{{identifier['value']}}},") |
| 201 | + break |
| 202 | + |
178 | 203 | # Define common fields for all entry types |
179 | 204 | simple_fields = [ |
180 | 205 | "doi", |
|
0 commit comments