forked from mfouesneau/arxiv_on_deck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
100 lines (82 loc) · 3.95 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
A quick and dirty parser for ArXiv
===================================
Default running application
"""
from app import (ExportPDFLatexTemplate)
class DefaultTemplate(ExportPDFLatexTemplate):
template = open('./default.tpl', 'r').read()
compiler = r"TEXINPUTS='../deprecated_tex:' pdflatex"
compiler_options = r"-enable-write18 -shell-escape -interaction=nonstopmode"
def short_authors(self, document):
return document.short_authors
def figure_to_latex(self, figure):
fig = ""
for fname in figure.files:
fig += r" \includegraphics[width=\maxwidth, height=\maxheight,keepaspectratio]{"
fig += fname + r"}\\" + "\n"
if len(figure.files) > 1:
fig = fig.replace(r'\maxwidth', '{0:0.1f}'.format(0.9 * 1. / len(figure.files)) + r'\maxwidth')
caption = r""" \caption{Fig. """ + str(figure._number) + """: """ + figure.caption + r"""}"""
return fig, caption
def apply_to_document(self, document):
txt = self.template.replace('<MACROS>', document._macros)
if document._identifier is not None:
txt = txt.replace('<IDENTIFIER>',
r'\hl{{{0:s}}}'.format(document._identifier) or 'Abstract ')
else:
txt = txt.replace('<IDENTIFIER>', 'Abstract ')
txt = txt.replace('<TITLE>', document.title)
txt = txt.replace('<AUTHORS>', self.short_authors(document))
txt = txt.replace('<ABSTRACT>', document.abstract.replace(r'\n', ' '))
for where, figure in zip('ONE TWO THREE'.split(),
self.select_figures(document, N=3)):
fig, caption = self.figure_to_latex(figure)
if where == 'ONE':
special = fig.replace(r"[width=\maxwidth, height=\maxheight,keepaspectratio]", "")
txt = txt.replace('<FILE_FIGURE_ONE>', special)
fig = fig.replace(r'\\', '')
txt = txt.replace('<FIGURE_{0:s}>'.format(where), fig)
txt = txt.replace('<CAPTION_{0:s}>'.format(where), caption)
if '<CAPTION_TWO>' in txt:
txt = txt.replace('<FIGURE_TWO>', '')
txt = txt.replace('<CAPTION_TWO>', '')
if '<CAPTION_THREE>' in txt:
txt = txt.replace('<FIGURE_THREE>', '')
txt = txt.replace('<CAPTION_THREE>', '')
txt = txt.replace('<COMMENTS>', document.comment or '')
txt = txt.replace('<DATE>', document.date)
return txt
def main(template=None):
from app import (get_mitarbeiter, filter_papers, ArXivPaper,
highlight_papers, running_options, get_new_papers, shutil)
options = running_options()
identifier = options.get('identifier', None)
paper_request_test = (identifier not in (None, 'None', '', 'none'))
hl_authors = options.get('hl_authors', None)
hl_request_test = (hl_authors not in (None, 'None', '', 'none'))
if not hl_request_test:
mitarbeiter_list = options.get('mitarbeiter', './mitarbeiter.txt')
mitarbeiter = get_mitarbeiter(mitarbeiter_list)
else:
mitarbeiter = [author.strip() for author in hl_authors.split(',')]
if identifier in (None, '', 'None'):
papers = get_new_papers(skip_replacements=True)
keep = filter_papers(papers, mitarbeiter)
else:
papers = [ArXivPaper(identifier=identifier.split(':')[-1])]
keep = highlight_papers(papers, mitarbeiter)
for paper in keep:
print(paper)
try:
paper.get_abstract()
s = paper.retrieve_document_source('./tmp')
s.compile(template=template)
_identifier = paper.identifier.split(':')[-1]
name = s.outputname.replace('.tex', '.pdf').split('/')[-1]
shutil.move('./tmp/' + name, _identifier + '.pdf')
print("PDF postage:", _identifier + '.pdf' )
except Exception as error:
print(error, '\n')
if __name__ == "__main__":
main(template=DefaultTemplate())