1- #!/usr/bin/env python3
2- import sys
31import logging
2+ import sys
43from pathlib import Path
4+
55from .utils import (
6- load_yaml_file ,
7- get_file_path ,
6+ extract_keywords ,
87 format_authors_for_bibtex ,
98 generate_citation_key ,
9+ get_file_path ,
10+ load_yaml_file ,
1011)
1112
1213logging .basicConfig (level = logging .INFO , format = "%(levelname)s: %(message)s" )
@@ -112,9 +113,7 @@ def create_bibtex_from_cff():
112113 # Validate required fields
113114 authors = pref .get ("authors" , [])
114115 title = pref .get ("title" , "Untitled" )
115- year = str (
116- pref .get ("year" , "" )
117- ) # Ensure year is a string for generate_citation_key
116+ year = str (pref .get ("year" , "" )) # Ensure year is a string for generate_citation_key
118117
119118 if not authors :
120119 logging .warning ("No authors found in CITATION.cff" )
@@ -142,19 +141,13 @@ def create_bibtex_from_cff():
142141 if entry_type == "thesis" :
143142 # Check for thesis type information
144143 thesis_type = pref .get ("thesis-type" , "" ).lower ()
145- if (
146- thesis_type == "master"
147- or thesis_type == "masters"
148- or thesis_type == "master's"
149- ):
144+ if thesis_type == "master" or thesis_type == "masters" or thesis_type == "master's" :
150145 entry_type = "mastersthesis"
151146 else :
152147 # Default to phdthesis if type is not specified or is something else
153148 entry_type = "phdthesis"
154149
155- logging .info (
156- f"Converting CFF type '{ cff_type } ' to BibTeX entry type: { entry_type } "
157- )
150+ logging .info (f"Converting CFF type '{ cff_type } ' to BibTeX entry type: { entry_type } " )
158151
159152 # Use utility function to generate citation key
160153 try :
@@ -195,11 +188,7 @@ def create_bibtex_from_cff():
195188 # Map CFF journal-specific fields to BibTeX fields
196189 if "collection-title" in pref and "journal" not in pref :
197190 bibtex_lines .append (f" journal = {{{ pref ['collection-title' ]} }}," )
198- if (
199- "volume-title" in pref
200- and "journal" not in pref
201- and "collection-title" not in pref
202- ):
191+ if "volume-title" in pref and "journal" not in pref and "collection-title" not in pref :
203192 bibtex_lines .append (f" journal = {{{ pref ['volume-title' ]} }}," )
204193
205194 elif entry_type in ["inproceedings" , "proceedings" ]:
@@ -261,9 +250,7 @@ def create_bibtex_from_cff():
261250 bibtex_lines .append (f" booktitle = {{{ pref ['collection-title' ]} }}," )
262251
263252 # Map collection editor if available
264- if "collection-editors" in pref and isinstance (
265- pref ["collection-editors" ], list
266- ):
253+ if "collection-editors" in pref and isinstance (pref ["collection-editors" ], list ):
267254 try :
268255 editor_str = format_authors_for_bibtex (pref ["collection-editors" ])
269256 bibtex_lines .append (f" editor = {{{ editor_str } }}," )
@@ -280,18 +267,14 @@ def create_bibtex_from_cff():
280267
281268 # Add repository info to note field if available
282269 if "repository-code" in pref and "note" not in pref :
283- bibtex_lines .append (
284- f" note = {{Repository: { pref ['repository-code' ]} }},"
285- )
270+ bibtex_lines .append (f" note = {{Repository: { pref ['repository-code' ]} }}," )
286271
287272 # Add version info
288273 if "version" in pref :
289274 bibtex_lines .append (f" version = {{{ pref ['version' ]} }}," )
290275
291276 # Add software-specific details as howpublished if not present
292- if ("howpublished" not in pref ) and (
293- "repository-code" in pref or "url" in pref
294- ):
277+ if ("howpublished" not in pref ) and ("repository-code" in pref or "url" in pref ):
295278 repo = pref .get ("repository-code" , pref .get ("url" , "" ))
296279 bibtex_lines .append (f" howpublished = {{Available from: { repo } }}," )
297280
@@ -303,9 +286,7 @@ def create_bibtex_from_cff():
303286
304287 # Include last accessed date if available
305288 if "date-accessed" in pref :
306- bibtex_lines .append (
307- f" note = {{Accessed: { pref ['date-accessed' ]} }},"
308- )
289+ bibtex_lines .append (f" note = {{Accessed: { pref ['date-accessed' ]} }}," )
309290
310291 # Process all simple fields
311292 for field in simple_fields :
@@ -328,8 +309,9 @@ def create_bibtex_from_cff():
328309 # Handle keywords field
329310 if "keywords" in pref and pref ["keywords" ]:
330311 try :
331- if isinstance (pref ["keywords" ], list ):
332- keywords_str = ", " .join (pref ["keywords" ])
312+ keywords_list = extract_keywords (pref ["keywords" ])
313+ if keywords_list :
314+ keywords_str = ", " .join (keywords_list )
333315 bibtex_lines .append (f" keywords = {{{ keywords_str } }}," )
334316 except Exception as e :
335317 logging .warning (f"Error processing keywords field: { str (e )} " )
@@ -355,4 +337,4 @@ def create_bibtex_from_cff():
355337
356338if __name__ == "__main__" :
357339 success = create_bibtex_from_cff ()
358- sys .exit (0 if success else 1 )
340+ sys .exit (0 if success else 1 )
0 commit comments