-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_citations.py
More file actions
executable file
·80 lines (64 loc) · 2.24 KB
/
format_citations.py
File metadata and controls
executable file
·80 lines (64 loc) · 2.24 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
import argparse
import json
from datetime import datetime
from pathlib import Path
import pypandoc
import structlog
from jinja2 import Template
logger = structlog.get_logger()
markdown_template = open('template.md').read()
pandoc_template = '''
---
geometry: margin=1in
colored-links: true
linkcolor: blue
urlcolor: orange
...
'''
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', dest='input_file', default='resources.json', type=Path)
parser.add_argument('-o', dest='output_path', default='resources.md', type=Path)
args = parser.parse_args()
with open(args.input_file) as fp:
resources = json.load(fp)
# sort resources by citation count
resources = sorted([
resource for resource in resources if resource['citations_count'] > 0
], key=lambda r: r['citations_count'], reverse=True)
# compile a list of publications
dois = set()
publications = {}
for resource in resources:
for resource in resources:
for citation in resource['citations']:
doi = citation.get('doi')
if doi not in dois:
publication_year = citation.get('publication_year')
if publication_year in publications:
publications[publication_year].append(citation)
else:
publications[publication_year] = [citation]
dois.add(doi)
# sort publications in each year by date
for year in publications:
publications[year] = sorted(publications[year], key=lambda r: r['publication_date'], reverse=True)
# render the markdown template
rendered_template = Template(markdown_template).render(
resources=resources,
publications=publications,
today=datetime.today().strftime("%B %d, %Y")
)
if args.output_path.suffix == '.md':
args.output_path.write_text(rendered_template)
elif args.output_path.suffix == '.pdf':
pypandoc.convert_text(
pandoc_template + rendered_template,
'pdf',
format='markdown',
outputfile=args.output_path,
extra_args=['--pdf-engine=xelatex']
)
if __name__ == '__main__':
main()