aboutsummaryrefslogtreecommitdiff
path: root/lexercise.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lexercise.lua')
-rw-r--r--lexercise.lua76
1 files changed, 76 insertions, 0 deletions
diff --git a/lexercise.lua b/lexercise.lua
new file mode 100644
index 0000000..22649d5
--- /dev/null
+++ b/lexercise.lua
@@ -0,0 +1,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
+