Parser of berki style problems and generator of latex file
Samo Penic
2018-11-03 c9c8d5997209b1f00b25a6bc2b7fa2b3fb186640
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
from string import Template
import os
from . import Exceptions
 
 
class TemplateEngine:
    def __init__(self, path=None):
        if path is None:
            raise (Exceptions.TemplatePathError("No path to templates"))
 
        self.template_filenames = {
            "head": os.path.join(path, "headtemplate.tpl"),
            "paper": os.path.join(path, "newpaperwithNAMEtemplate.tpl"),
            "paper_sid": os.path.join(path, "newpaperwithSIDtemplate.tpl"),
            "problem_intro_image": os.path.join(path, "problemintrowithimage.tpl"),
            "problem_intro": os.path.join(path, "problemintrotemplate.tpl"),
            "subproblem": os.path.join(path, "subproblemtemplate.tpl"),
            "subproblem_end": os.path.join(path, "subproblemtemplateend.tpl"),
            "answers": os.path.join(path,"answerstemplate.tpl")
        }
 
        self.template = {}
        for key, value in self.template_filenames.items():
            self.template[key] = self.loadtemplate(value)
 
    def loadtemplate(self, filename):
        with open(filename) as fd:
            template = fd.read()
        return template
 
    def put_problem_into_template(self, problem_dict, problem_number=0):
        retstr = Template(self.template["problem_intro"]).substitute(
            {"problem_number": problem_number, "text": problem_dict["introduction"]}
        )
        if len(problem_dict["solutions"]) > 1:
            for sp, sol in zip(problem_dict["subproblems"], problem_dict["solutions"]):
                retstr += Template(self.template["subproblem"]).substitute(
                    {
                        "text": sp,
                        "ans1": sol["shuffled"][0][0].format_as_tex(
                            glyph=sol["glyph"], unit=sol["unit"]
                        ),
                        "ans2": sol["shuffled"][1][0].format_as_tex(
                            glyph=sol["glyph"], unit=sol["unit"]
                        ),
                        "ans3": sol["shuffled"][2][0].format_as_tex(
                            glyph=sol["glyph"], unit=sol["unit"]
                        ),
                        "ans4": sol["shuffled"][3][0].format_as_tex(
                            glyph=sol["glyph"], unit=sol["unit"]
                        ),
                    }
                )
            retstr += Template(self.template["subproblem_end"]).substitute()
        else:
            retstr += Template(self.template["answers"]).substitute(
                {
                    "ans1": problem_dict["solutions"][0]["shuffled"][0][
                        0
                    ].format_as_tex(
                        glyph=problem_dict["solutions"][0]["glyph"],
                        unit=problem_dict["solutions"][0]["unit"],
                    ),
                    "ans2": problem_dict["solutions"][0]["shuffled"][1][
                        0
                    ].format_as_tex(
                        glyph=problem_dict["solutions"][0]["glyph"],
                        unit=problem_dict["solutions"][0]["unit"],
                    ),
                    "ans3": problem_dict["solutions"][0]["shuffled"][2][
                        0
                    ].format_as_tex(
                        glyph=problem_dict["solutions"][0]["glyph"],
                        unit=problem_dict["solutions"][0]["unit"],
                    ),
                    "ans4": problem_dict["solutions"][0]["shuffled"][3][
                        0
                    ].format_as_tex(
                        glyph=problem_dict["solutions"][0]["glyph"],
                        unit=problem_dict["solutions"][0]["unit"],
                    ),
                }
            )
        return retstr
 
    def head(self, settings):
        return Template(self.template["head"]).substitute(settings)
 
    def tail(self):
        return """\end{document}"""
 
    def start_paper(self, student_id=None, student_name=None):
        if student_id is not None:
            return Template(self.template["paper"]).substitute(
                {"student_id": student_id, "student_name": student_name}
            )
        else:
            return Template(self.template["paper_sid"]).substitute()