-
Notifications
You must be signed in to change notification settings - Fork 9
/
gca-tex
executable file
·82 lines (66 loc) · 3.01 KB
/
gca-tex
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from gca.core import Abstract
from gca.core import Conference
from mako.template import Template
import os
import sys
import argparse
import json
import codecs
from gca.tex import basic_tempate
from gca.tex import mk_tex_text
text_replacements = {
'body': {
'': '',
},
'input': {}
}
def text_replace_all(text, replacements):
if replacements is not None and text is not None:
for key, val in replacements.items():
text = text.replace(key, val)
return text
def sanitize_abstract(abstract, replacements=None):
text = text_replace_all(abstract.text, replacements['body'])
text = mk_tex_text(text, is_body=True)
abstract.text = text
return abstract
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='GCA Client')
parser.add_argument('-r', '--replacements', type=str)
parser.add_argument('--figures', type=str, default=None)
parser.add_argument('--conference', dest='conf', type=str, default=None)
parser.add_argument('--bare', dest='bare', action='store_true', default=False)
parser.add_argument('--singlepage', dest='single_page', action='store_true', default=False)
parser.add_argument('--showmeta', dest='show_meta', action='store_true', default=False)
parser.add_argument('file', type=str, default='-')
args = parser.parse_args()
figure_path = os.path.abspath(args.figures) if args.figures is not None else None
conf = None
if args.conf is not None:
fd = codecs.open(args.conf, 'r', encoding='utf-8')
conf = Conference.from_data(fd.read())
if args.replacements is not None:
sys.stderr.write('[I] Using replacements from file "%s"\n' % args.replacements)
with codecs.open(args.replacements, 'r', encoding='utf-8') as r_file:
r_data = json.loads(r_file.read(), encoding='utf-8')
for key in r_data.iterkeys():
text_replacements[key].update(r_data[key])
fd = codecs.open(args.file, 'r', encoding='utf-8') if args.file != '-' else sys.stdin
sys.stderr.write('[I] Loading abstract data\n')
txt = fd.read()
sys.stderr.write('[I] Pre-processing abstracts\n')
txt = text_replace_all(txt, text_replacements['input'])
data = json.loads(txt)
abstracts = [sanitize_abstract(Abstract(abstract, conference=conf), text_replacements) for abstract in data]
sys.stderr.write('[I] %d abstracts. Generating tex [figures: %s, conference: %s]\n' %
(len(abstracts), 'yes' if args.figures else 'NO', 'yes' if conf is not None else 'No'))
tex_template = Template(basic_tempate, output_encoding='utf-8')
rendered = tex_template.render(abstracts=abstracts, figures=figure_path,
bare=args.bare, single_page=args.single_page,
show_meta=args.show_meta)
sys.stderr.write('[I] Done\n')
sys.stdout.write(rendered.decode('utf-8'))