diff options
author | Bryan Newbold <bnewbold@archive.org> | 2021-01-14 23:29:27 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@archive.org> | 2021-01-14 23:29:27 -0800 |
commit | 9da8e36c9360ec63e161ac92f455b48614d8b636 (patch) | |
tree | 74c898b8444b5a2b14f581735c04847e9379911b | |
parent | 65406d42f00c5564499bfd54b5940f22ecc53ed0 (diff) | |
download | fatcat-scholar-9da8e36c9360ec63e161ac92f455b48614d8b636.tar.gz fatcat-scholar-9da8e36c9360ec63e161ac92f455b48614d8b636.zip |
crude bibtex and citation formatting, as a demo
-rw-r--r-- | fatcat_scholar/schema.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/fatcat_scholar/schema.py b/fatcat_scholar/schema.py index 4c6afe7..131cf74 100644 --- a/fatcat_scholar/schema.py +++ b/fatcat_scholar/schema.py @@ -104,6 +104,55 @@ class ScholarBiblio(BaseModel): contrib_names: List[str] affiliations: List[str] + def citation_str(self, style: str) -> Optional[str]: + """ + Tries to format this biblio metadata as a citation string. If it fails, + returns None. + + Urgently, will probably refactor to use a proper citeproc library. Will + need to do something different about author names at the same time. + """ + if style == "bibtex": + type_map = { + 'article-journal': 'article', + 'conference-paper': 'proceedings', + 'thesis': 'phdthesis', + 'book': 'book', + None: 'unpublished', + } + val = f"@{type_map.get(self.release_type, 'unpublished')}{{{self.release_ident},\n" + val += f" title = {{{self.title}}}\n" + for name in self.contrib_names: + val += f" author = {{{name}}}\n" + if self.pages: + val += f" pages = {{{self.pages}}}\n" + if self.volume: + val += f" volume = {{{self.volume}}}\n" + if self.issue: + val += f" number = {{{self.issue}}}\n" + if self.release_year: + val += f" year = {self.release_year}\n" + if self.container_name: + val += f" journal = {{{self.container_name}}}\n" + if self.publisher: + val += f" publisher = {{{self.publisher}}}\n" + val += "}" + return val + elif style == "default": + val = ", ".join(self.contrib_names) + if val: + val += ". " + val += f" \"self.title.\" " + if self.container_name: + val += self.container_name + if self.volume and self.issue: + val += f"{self.volume}.{self.issue} " + if self.release_year: + val += f" ({self.release_year})" + if self.pages: + val += f": {self.pages}" + return val + class ScholarFulltext(BaseModel): lang_code: Optional[str] |