aboutsummaryrefslogtreecommitdiff
path: root/exsh_lexercise.lua
blob: b05d29e6d34f14b1ef1e09d62011f35b5b14b20d (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
exsh_texprintlines = function(s)
  for line in s:gmatch("[^\n]+") do
    tex.print(line)
  end
end


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,p,bp = ""
  local sols = ""
  if type(se) ~= "string" then
    local set = se
    local po = ""
    if set.pointoptions then
      po = po .. set.pointoptions
    end
    p,bp = set.points,set.bonuspoints 
    if  p and bp then
      pts = [[\points[]] .. po .. "]{" .. set.points .. "+" ..
        set.bonuspoints .. "}"
    elseif bp then
      pts = [[\points[bonus,]] .. po .. "]{" .. set.bonuspoints .. "}"
    elseif p then
      pts = [[\points[]] .. po .. "]{" .. 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 = "["
    if set.options then
      se = se .. set.options
    end
    se = se .. "]{" .. (set.task or "") .. "}"
  else
    se = "{" .. se .. "}"
  end
  return [[\subtask]]..se .. pts .. sols, p, bp
end

exercise = function(ex)
  local subex = ""
  local pts,bpts = 0,0
  if ex.subexercises then
    subex =  [[\begin{subtasks}]]
    for _,se in ipairs(ex.subexercises) do
      local s,p,bp = subexercise(se)
      subex = subex .. s
      pts = pts + (p or 0)
      bpts = bpts + (bp or 0)
    end
    subex = subex .. [[\end{subtasks}]]
  end
  if ex.points and ex.points == "sum" then
    if  pts > 0  and bpts > 0 then
      pts = pts .. "+" .. bpts
    elseif bpts > 0 then --and pts = 0
      pts = bpts
      ex.options = (ex.options or "") .. ",bonus"
    --else bpts = 0 thus pts is all points 
    end
  else
    pts = ex.points
  end

  local ece = [[\begin{exercise}[]]
  if ex.firstline then
    ece = ece .. [[firstline={]] .. ex.firstline .. [[},]]
  end
  if pts then
    ece = ece .. [[points={]] .. pts .. [[},]]
  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
  
  ece = ece .. subex

  ece = ece .. "\n" .. [[\end{exercise}]]
  return ece
end