-
Notifications
You must be signed in to change notification settings - Fork 4
/
j2tex.py
77 lines (65 loc) · 1.97 KB
/
j2tex.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
import os
import sys
import json
import jinja2
from jinja2 import Template
import getopt
def main(argv):
try:
# -i and -t requires a value,
opts, args = getopt.getopt(sys.argv[1:], "i:t:o:")
except getopt.GetoptError as err:
# print help information and exit:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
input_file = ""
template_file = ""
if "-i" not in [x[0] for x in opts]:
print("-i <filename> is required")
usage()
sys.exit(2)
if "-t" not in [x[0] for x in opts]:
print("-t <filename> is required")
usage()
sys.exit(2)
if "-o" not in [x[0] for x in opts]:
print("-o <filename> is required")
usage()
sys.exit(2)
for option, value in opts:
if option == "-i":
input_file = value
elif option == "-t":
template_file = value
elif option == "-o":
output_file = value
render(input_file, template_file, output_file)
def usage():
# TODO
print("help")
def render(input_file, template_file, output_file):
latex_jinja_env = jinja2.Environment(
block_start_string='\jb{',
block_end_string='}',
variable_start_string='\jv{',
variable_end_string='}',
comment_start_string='\#{',
comment_end_string='}',
line_statement_prefix='j%%',
line_comment_prefix='%#',
trim_blocks=True,
autoescape=False,
loader=jinja2.FileSystemLoader(os.path.abspath('.'))
)
template = latex_jinja_env.get_template(template_file)
with open(input_file, 'r') as file:
try:
data = json.loads(file.read())
except ValueError:
sys.exit("Not a valid JSON")
with open(output_file, 'w') as file:
file.write(template.render(data=data))
print("Tex successfully compiled")
if __name__ == "__main__":
main(sys.argv[1:])