aboutsummaryrefslogtreecommitdiff
path: root/render.py
blob: e563cec39b27eacf171f4331a0c0ee556527411d (plain)
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
#!/usr/bin/env python3
# Render a single document

import argparse
import os
import jinja2
import subprocess
import re

tex_template = """\\documentclass[12pt]{article}
\\usepackage{amsmath}
\\usepackage{amsfonts}
\\usepackage{mdframed}
\\usepackage{float}
\\usepackage{amsthm}
\\usepackage{booktabs}
\\usepackage{siunitx}
\\usepackage{enumitem}

\\usepackage{tikz}
\\usetikzlibrary{automata, positioning, arrows}

\\newtheorem{lemma}{Lemma}

\\theoremstyle{definition}
\\newtheorem{definition}{Definition}[section]
\\newtheorem{theorem}{Theorem}[section]
\\newtheorem{principle}{Principle}[section]

{% for thing in before %}
{{thing}}
{% endfor %}

\\setlength{\parindent}{0cm}
\\setlength{\parskip}{0.3em}

\\newenvironment{opg}
{
\itshape
}{}

\\begin{document}
    {% if title is not none %}
    \maketitle
    {% endif %}

    {{content}}
\\end{document}
"""

beforewhitelist = ["title", "date"]

parser = argparse.ArgumentParser()
parser.add_argument("file", help="The file to load")

args = parser.parse_args()

# Load the file
content = []
before = []
with open(args.file, "r") as f:
    for line in f:
        m = re.findall("\\\\([a-zA-Z]*)\{.*\}", line)
        if m and m[0] in beforewhitelist:
            before.append(line)
        else:
            content.append(line)

content = "".join(content)

# Write to output
tmpl = jinja2.Template(tex_template)
output = tmpl.render(before=before,content=content)
print(output)

# Create build folder
if not os.path.exists("render_build"):
    os.mkdir("render_build")

os.chdir("render_build")
with open("input.tex", "w") as f:
    f.write(output)

subprocess.call(["pdflatex", "-halt-on-error", "input.tex"])