-
Notifications
You must be signed in to change notification settings - Fork 5
/
makepdf.py
58 lines (38 loc) · 1.32 KB
/
makepdf.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
#!/usr/bin/env python3
# Assignment compiler version 1
import sys, os.path
import subprocess
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="Markdown file name")
parser.add_argument("--template", help="Template Latex File", default="template.tex")
args = parser.parse_args()
filename = args.filename
if not os.path.isfile(filename):
print("Error: file does not exist!")
sys.exit(1)
json_str = open(filename).read()
template_str = open(args.template).read()
#####################################################
import parse_json
latex_src = parse_json.parse(json_str, template_str)
#####################################################
open("temp.tex","w").write(latex_src)
cmd = ["xelatex","-interaction","batchmode","temp.tex"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, err = process.communicate()
out = out.decode()
exit_code = process.wait()
if exit_code != 0:
print(err)
print("Error: xelatex failed")
sys.exit(1)
print("Success: pdf generated.")
subprocess.call(["rm","temp.aux"])
subprocess.call(["rm","temp.tex"])
subprocess.call(["rm","temp.log"])
subprocess.call(["rm","temp.out"])
subprocess.call(["rm",filename])
new_pdf = filename.replace("json","pdf")
subprocess.call(["mv","temp.pdf",new_pdf])
#subprocess.call(["xdg-open",new_pdf])