Skip to content

Commit 3032fc6

Browse files
author
Fabrice Benhamouda
committed
Remove empty fields & linting
This commit, combined with the associated commit in https://github.com/cryptobib/lib allows to remove empty fields from the generated bibfiles.
1 parent 47a4ba4 commit 3032fc6

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

mybibtex/generator.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def format_author(author):
189189

190190
return fields
191191

192-
def bibtex_write_entry(out, db, key, entry, expand_crossrefs=False, expand_values=False):
192+
def bibtex_write_entry(out, db, key, entry, expand_crossrefs=False, expand_values=False, remove_empty_fields=False):
193193
""" Write a bibtex entry in out """
194194

195195
def key_sort(key):
@@ -210,6 +210,12 @@ def format_type(type_):
210210
out.write("@{0}{{{1},\n".format(format_type(entry.type), str(key)))
211211

212212
for k in sorted(iter(fields.keys()), key=key_sort):
213+
# remove empty fields after expansion
214+
if remove_empty_fields:
215+
v_expanded = fields[k].to_bib(expand = True)
216+
if v_expanded == '""':
217+
continue
218+
213219
v = fields[k].to_bib(expand = expand_values)
214220

215221
# v_ascii only contains ascii characters
@@ -232,10 +238,15 @@ def bibtex_write_entries(out, db, entries, *args, **kwargs):
232238

233239

234240
def bibtex_gen(out, db, entry_filter=FilterPaper(), entry_sort=SortConfYearPage(), expand_crossrefs=False, include_crossrefs=False, *args, **kwargs):
235-
""" Options:
241+
"""
242+
Generate bibtex file
243+
244+
Options:
236245
@arg expand_crossrefs: expand crossrefs inside entries instead of keeping the crossref field if True,
237246
@arg include_crossrefs: include crossrefs in the output if True and expand_crossrefs=False,
238247
@arg expand_values: expand values (using macros) if True
248+
@arg remove_empty_fields: remove empty fields if True, empty fields are ones that are either empty or expand to an empty value
249+
(in case expand_values=False and multiple macros values may be used using, e.g., multiple "abbrev*.bib" files, be extra careful)
239250
"""
240251
entries = dict(entry_filter.filter(db.entries))
241252
bibtex_write_entries(

0 commit comments

Comments
 (0)