-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,72 @@ | ||
"""Bibtex writer for commonmeta-py""" | ||
from bibtexparser.bwriter import BibTexWriter | ||
from bibtexparser.bibdatabase import BibDatabase | ||
from bibtexparser.customization import author | ||
|
||
from ..utils import pages_as_string | ||
from ..base_utils import compact | ||
from ..author_utils import authors_as_string | ||
from ..date_utils import get_month_from_date, get_iso8601_date | ||
from ..date_utils import get_month_from_date, get_iso8601_date, MONTH_SHORT_NAMES | ||
from ..doi_utils import doi_from_url | ||
from ..constants import CM_TO_BIB_TRANSLATIONS, Commonmeta | ||
|
||
|
||
def write_bibtex(metadata): | ||
def write_bibtex(metadata: Commonmeta) -> str: | ||
"""Write bibtex""" | ||
container = metadata.container or {} | ||
container = metadata.container if metadata.container else {} | ||
date_published = get_iso8601_date(metadata.date.get("published", None)) | ||
|
||
id_ = metadata.id | ||
type_ = CM_TO_BIB_TRANSLATIONS.get(metadata.type, "misc") | ||
abstract = metadata.descriptions[0].get("description", None) if metadata.descriptions else None | ||
author = authors_as_string(metadata.creators) | ||
license_ = str(metadata.license.get("url")) if metadata.license else None | ||
doi = doi_from_url(metadata.id) | ||
issn = container.get("identifier", None) if container.get("identifierType", None) == "ISSN" else None | ||
issue = container.get("issue", None) | ||
journal = container.get("title", None) if type_ not in ["inbook", "inproceedings"] else None | ||
booktitle = container.get("title", None) if type_ in ["inbook", "inproceedings"] else None | ||
language = metadata.language | ||
month = get_month_from_date(date_published) | ||
pages = pages_as_string(container) | ||
publisher = metadata.publisher.get("name", None) if type_ not in ["article", "phdthesis"] else None | ||
institution = metadata.publisher.get("name", None) if type_ == "phdthesis" else None | ||
title = metadata.titles[0].get("title", None) | ||
url = metadata.url | ||
year = date_published[:4] if date_published else None | ||
|
||
bib_database = BibDatabase() | ||
date_published = get_iso8601_date(metadata.date.get("published", None)) | ||
bib_database.entries = [ | ||
compact( | ||
{ | ||
"ID": metadata.id, | ||
"ID": id_, | ||
"ENTRYTYPE": type_, | ||
"abstract": metadata.descriptions[0].get("description", None) | ||
if metadata.descriptions | ||
else None, | ||
"author": authors_as_string(metadata.creators), | ||
"copyright": str(metadata.license.get("url")) | ||
if metadata.license | ||
else None, | ||
"doi": doi_from_url(metadata.id), | ||
"issn": container.get("identifier", None) | ||
if container.get("identifierType", None) == "ISSN" | ||
else None, | ||
"issue": container.get("issue", None), | ||
"journal": container.get("title", None) | ||
if type_ not in ["inbook", "inproceedings"] | ||
else None, | ||
"booktitle": container.get("title", None) | ||
if type_ in ["inbook", "inproceedings"] | ||
else None, | ||
"language": metadata.language, | ||
"month": get_month_from_date(date_published), | ||
"pages": pages_as_string(container), | ||
"publisher": metadata.publisher.get("name", None) | ||
if type_ not in ["article", "phdthesis"] | ||
else None, | ||
"institution": metadata.publisher.get("name", None) | ||
if type_ == "phdthesis" | ||
else None, | ||
"title": metadata.titles[0].get("title", None), | ||
"url": metadata.url, | ||
"abstract": abstract, | ||
"author": author, | ||
"copyright": license_, | ||
"doi": doi, | ||
"issn": issn, | ||
"issue": issue, | ||
"journal": journal, | ||
"booktitle": booktitle, | ||
"language": language, | ||
"month": month, | ||
"pages": pages, | ||
"publisher": publisher, | ||
"institution": institution, | ||
"title": title, | ||
"url": url, | ||
"urldate": date_published, | ||
"year": date_published[:4] if date_published else None, | ||
"year": year, | ||
} | ||
) | ||
] | ||
writer = BibTexWriter() | ||
writer.common_strings = True | ||
writer.indent = " " | ||
writer.common_strings = False | ||
bibtex_str = writer.write(bib_database) | ||
|
||
# Hack to remove curly braces around month names | ||
for month_name in MONTH_SHORT_NAMES: | ||
bibtex_str = bibtex_str.replace(f"{{{month_name}}}", month_name) | ||
return bibtex_str |