from .berkitext import getNaloga
|
from aoi_gen import Problem, Template, BerkiParse
|
from django import template
|
from django.template.defaultfilters import stringfilter
|
from django.utils.safestring import mark_safe
|
|
register = template.Library()
|
|
|
@register.filter
|
@stringfilter
|
def getHTMLText(value):
|
try:
|
parser = BerkiParse.BerkiParse(value)
|
parser.parseSections()
|
problem_source = Problem.ProblemSource(parser=parser)
|
problem = Problem.Problem(source=problem_source)
|
except:
|
return mark_safe(
|
"<p class='problem_text'>Error parsing and generating problem</p>"
|
)
|
ret_text = "<p class='problem_text'>{}</p>".format(problem.problem["introduction"])
|
try:
|
if len(problem.problem["subproblems"]) > 0:
|
ret_text += "<ul>"
|
for sp, sol in zip(
|
problem.problem["subproblems"], problem.problem["solutions"]
|
):
|
ret_text += "<li><p class='problem_text'>{}</p>".format(sp)
|
for i, soltext in enumerate(sol["shuffled"]):
|
ret_text += "<div class='{}'>{}) {} </div>".format(
|
"ans_correct" if soltext[1] == "1" else "ans_wrong",
|
chr(ord("a") + i),
|
soltext[0].format_as_tex(glyph=sol["glyph"], unit=sol["unit"]),
|
)
|
ret_text += "</li>"
|
ret_text += "</ul>"
|
else:
|
soltext = problem.problem["solutions"][0]
|
for i, soltext in enumerate(soltext["shuffled"]):
|
ret_text += "<div class='{}'>{}) {}</div>".format(
|
"ans_correct" if soltext[1] == "1" else "ans_wrong",
|
chr(ord("a") + i),
|
soltext[0].format_as_tex(glyph=problem.problem["solutions"][0]["glyph"],
|
unit=problem.problem["solutions"][0]["unit"]),
|
)
|
except:
|
ret_text+="Error finding texts and results!"
|
return mark_safe(ret_text)
|
# pr=getNaloga(value)
|
# problem = "<p class='problem_text'>"+pr['text']+"</p>"
|
# for i,ca in enumerate(pr['correct']):
|
# problem +="<div class='ans_correct'>"+chr(ord('a')+i)+") "+ca+'</div>'
|
# for j,ca in enumerate(pr['wrong']):
|
# problem +='<div class="ans_wrong">'+chr(ord('a')+i+j+1)+") "+ca+'</div>'
|
# return mark_safe(problem)
|