Created
January 6, 2023 14:50
-
-
Save mariogeiger/68f24bbc4470e1b234b014137d58511a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from pybtex.database.input import bibtex | |
import argparse | |
def main(): | |
args = argparse.ArgumentParser() | |
args.add_argument("path") | |
args = args.parse_args() | |
parser = bibtex.Parser() | |
bib_data = parser.parse_file(args.path) | |
def get_author_last_name(entry): | |
authors = entry.persons.get("author", None) | |
if authors is None or len(authors) == 0: | |
return "" | |
first_author = authors[0] | |
return " ".join(first_author.last_names) | |
entries = sorted( | |
bib_data.entries.values(), | |
key=lambda x: (x.type, get_author_last_name(x), x.fields.get("year", "")), | |
) | |
for entry in entries: | |
fields_to_remove = ["abstract", "keywords"] | |
if entry.type != "misc": | |
fields_to_remove += ["url"] | |
for field in fields_to_remove: | |
if field in entry.fields: | |
del entry.fields[field] | |
output_str = "" | |
for entry in entries: | |
output_str += entry.to_string("bibtex") | |
print(output_str) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment