-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyaml-to-bibtex.py
executable file
·67 lines (56 loc) · 2.45 KB
/
yaml-to-bibtex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/env python3
# ReScience yaml to bibtex converter
# Released under the BSD two-clauses licence
def generate_bibtex(filename, article):
content = (
"@Article {{{_.bibtex_label}:{_.date_published.year},\n"
" author = {{{_.authors_full}}},\n"
" title = {{{{{_.title}}}}},\n"
" journal = {{{_.journal_name}}},\n"
" year = {{{_.date_published.year}}},\n"
" month = {{{_.date_published.month}}},\n"
" volume = {{{_.journal_volume}}},\n"
" number = {{{_.journal_issue}}},\n"
" pages = {{{{#{_.article_number}}}}},\n"
" doi = {{{_.article_doi}}},\n"
" url = {{{_.article_url}}},\n"
" code_url = {{{_.code.url}}},\n"
" code_doi = {{{_.code.doi}}},\n"
" code_swh = {{{_.code.swh}}},\n"
" data_url = {{{_.data.url}}},\n"
" data_doi = {{{_.data.doi}}},\n"
" review_url = {{{_.review.url}}},\n"
" type = {{{_.type}}},\n"
" language = {{{_.language}}},\n"
" domain = {{{_.domain}}},\n"
" keywords = {{{_.keywords}}}\n"
"}}".format(filename=filename, _=article))
return content
# -----------------------------------------------------------------------------
if __name__ == '__main__':
import argparse
from article import Article
parser = argparse.ArgumentParser(description='YAML to bibtex converter.')
parser.add_argument('--input', '-i', dest='filename_in', action='store',
default="metadata.yaml", help='input YAML file')
parser.add_argument('--output', "-o", dest='filename_out', action='store',
help='output bibtex file')
args = parser.parse_args()
filename_in = args.filename_in
filename_out = args.filename_out
with open(filename_in, "r") as file:
article = Article(file.read())
article.authors_full = ""
n = len(article.authors)
if n==1:
article.authors_full = article.authors[0].fullname
else:
for i in range(n-1):
article.authors_full += article.authors[i].fullname + " and "
article.authors_full += article.authors[-1].fullname
content = generate_bibtex(filename_in, article)
if filename_out is not None:
with open(filename_out, "w") as file:
file.write(content)
else:
print(content)