aboutsummaryrefslogtreecommitdiff
path: root/lexercise.lua
blob: 22649d5df385eeab1e89bfe4caf7518fbc1a644b (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
solution = function(sol)
  if type(sol) ~= "string" then
    local solt = sol
    sol = "["
    if solt.idea then
      sol = sol .. "idea,"
    end
    if solt.name then
      sol = sol .. "strings/solution={"..solt.name.."},"
    end
    sol = sol .. "] " .. (solt.text or "") 
  end
  return [[\begin{solution}]]..sol..[[\end{solution}]]
end

subexercise = function(se)
  local pts = ""
  local sols = ""
  if type(se) ~= "string" then
    local set = se
    se = "["
    if set.points then
      pts = [[\points{]] .. set.points ..[[}]]
    end
    if set.solution then
      sols = sols .. solution(set.solution)
    end
    if set.altsolutions then
      for _,sol in ipairs(set.altsolutions) do
        sols = sols .. solution(sol)
      end
    end
    se = se .. "]{" .. (set.task or "") .. "}"
  end
  return [[\subtask]]..se .. pts .. sols
end

exercise = function(ex)
  local ece = [[\begin{exercise}[]]
  if ex.firstline then
    ece = ece .. [[firstline={]] .. ex.firstline .. [[},]]
  end
  if ex.points then
    ece = ece .. [[points={]] .. ex.points .. [[},]]
  end
  if ex.name then
    ece = ece .. [[name={]] .. ex.name .. [[},]]
  end
  if ex.options then
    ece = ece .. ex.options .. ","
  end
  ece = ece .. "]{}"
  if ex.task then
    ece = ece .. [[\begin{maintask}]]  .. ex.task 
      .. [[\end{maintask}]]
  end
  if ex.solution then
    ece = ece .. solution(ex.solution)
  end
  if ex.altsolutions then
    for _,sol in ipairs(ex.altsolutions) do
      ece = ece .. solution(sol)
    end
  end
  if ex.subexercises then
    ece = ece .. [[\begin{subtasks}]]
    for _,se in ipairs(ex.subexercises) do
      ece = ece .. subexercise(se)
    end
    ece = ece .. [[\end{subtasks}]]
  end
  ece = ece .. "\n" .. [[\end{exercise}]]
  print(ece)
  return ece
end