-
Notifications
You must be signed in to change notification settings - Fork 2
/
pybibtex-sort-bibfile.py
55 lines (43 loc) · 1.8 KB
/
pybibtex-sort-bibfile.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
# pybibtext -- Utility functions for
# parsing BibTeX files and creating citation reference
# lists.
#
# Author: Sebastian Raschka <mail@sebastianraschka.com>
# License: MIT
# Code Repository: https://github.com/rasbt/pybibtex
# pybibtex-sort-bibfile.py is a script that
# sorts bib file entries alphabetically
import argparse
from pybibtex import parse_bibfile_to_dict
from pybibtex import __version__
def write_new_text(file_path, text):
with open(file_path, 'w') as f:
f.write(text)
def main(file_path, out_path):
d = parse_bibfile_to_dict(file_path)
items_sorted = [(k, v) for k, v in d.items()]
items_sorted.sort()
sorted_file_contents = [i[1] for i in items_sorted]
sorted_file_contents = '\n\n'.join(['\n'.join(entry)
for entry in sorted_file_contents])
write_new_text(out_path, sorted_file_contents)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=('Script that sorts bib file entries alphabetically'),
epilog=('Example:\npython pybibtex-sort-bibfile.py'
' -i infile.bib -o oubfile.bib\n'),
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-i', '--input',
type=str,
required=True,
help='Input bibfiles file with bibtex entries')
parser.add_argument('-o', '--output',
type=str,
required=True,
help='Output path for the bibtex file '
'with sorted entries')
parser.add_argument('--version', action='version',
version='pybibtex v. %s' % __version__)
args = parser.parse_args()
main(file_path=args.input,
out_path=args.output)