forked from alshedivat/al-folio
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
32c35d0
commit 406261b
Showing
4 changed files
with
67 additions
and
145 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,25 +1,24 @@ | ||
|
||
import bibtexparser | ||
from bibtexparser.bwriter import BibTexWriter | ||
|
||
|
||
def add_field_if_author_in_entry(bibtex_path, author_name, new_field, new_field_value): | ||
def add_field_if_author_in_entry(bibtex_path, pis): | ||
with open(bibtex_path) as bibtex_file: | ||
bib_database = bibtexparser.load(bibtex_file) | ||
|
||
for entry in bib_database.entries: | ||
if 'author' in entry and author_name in entry['author']: | ||
if new_field not in entry: | ||
entry[new_field] = new_field_value | ||
else: | ||
entry[new_field] = entry[new_field] + \ | ||
"\n x \n" + new_field_value | ||
if 'author' in entry: | ||
entry["abbr"] = ", ".join( | ||
[pi_label for pi, pi_label in pis.items() if pi in entry['author']]) | ||
|
||
writer = BibTexWriter() | ||
writer.contents = ['entries'] | ||
writer.indent = ' ' | ||
|
||
with open(bibtex_path, 'w') as bibtex_file: | ||
bibtexparser.dump(bib_database, bibtex_file) | ||
|
||
|
||
# Usage | ||
add_field_if_author_in_entry( | ||
'papers.bib', 'Nolan', 'abbr', 'NOLAN') | ||
add_field_if_author_in_entry( | ||
'papers.bib', 'Gulsen', 'abbr', 'SÜRMELI') | ||
'papers.bib', {"Matt": "NOLAN", "Gulsen": "SÜRMELI"}) |
Oops, something went wrong.