From 3caf9ba2df7249292231b1554a4d4f1764b82f3d Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Fri, 27 Oct 2023 14:21:16 +0200 Subject: [PATCH] Allow extra fields with `GbibClean` (#80) --- GooseBib/bibtex.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/GooseBib/bibtex.py b/GooseBib/bibtex.py index 0782add..f8e7ca7 100644 --- a/GooseBib/bibtex.py +++ b/GooseBib/bibtex.py @@ -680,6 +680,7 @@ def clean( protect_math: bool = True, rm_unicode: bool = True, no_abbreviate: list[str] = [], + select_fields: bool = True, ) -> list[dict]: r""" Clean a BibTeX database. @@ -698,6 +699,7 @@ def clean( :param protect_math: Apply fix in :py:func:`GooseBib.reformat.protect_math`. :param rm_unicode: Apply fix in :py:func:`GooseBib.reformat.rm_unicode`. :param no_abbreviate: List of entries for which to skip author abbreviation. + :param select_fields: Apply :py:func:`selection` to the output. """ ignored_authors = [] @@ -768,7 +770,14 @@ def clean( warnings.warn(f"Protected authors found, please check:\n{ignored_authors}", Warning) # return selection of fields - return select(data, fields=selection(use_bibtexparser=True)) + if select_fields: + warnings.warn( + "``select_fields`` will be deprecated in next major release. " + "Please call ``selection`` directly." + ) + return select(data, fields=selection(use_bibtexparser=True)) + else: + return data @clean.register(bibtexparser.bibdatabase.BibDatabase) @@ -1043,6 +1052,13 @@ def _split_lines(self, text, width): help='Rename field (e.g. "arxivid" to "eprint").', ) + parser.add_argument( + "--add-field", + type=str, + action="append", + help='Add field. For all types ``"field"``, only for one type ``typename:field``.', + ) + parser.add_argument( "--no-title", action="store_true", @@ -1294,6 +1310,7 @@ def GbibClean(cli_args: list[str] = None): protect_math=not args.ignore_math, rm_unicode=not args.ignore_unicode, no_abbreviate=args.raw_author if args.raw_author else [], + select_fields=False, ) # reformat arXiv entries @@ -1325,14 +1342,29 @@ def GbibClean(cli_args: list[str] = None): data, m = clever_merge(data) merged = {**merged, **m} - # rename fields + # select and rename fields + + fields = selection(use_bibtexparser=True) + + if args.add_field: + for field in args.add_field: + if ":" in field: + typename, field = field.split(":") + fields[typename].append(field) + else: + for typename in fields: + fields[typename].append(field) if args.rename_field: for oldfield, newfield in args.rename_field: + for typename in fields: + fields[typename].append(newfield) for entry in data: if oldfield in entry: entry[newfield] = entry.pop(oldfield) + data = select(data, fields=fields) + # rename keys if args.rename: