aboutsummaryrefslogtreecommitdiff
path: root/lexercise.lua
diff options
context:
space:
mode:
authorFrank Fuhlbrück2022-01-22 16:29:41 +0100
committerFrank Fuhlbrück2022-01-22 16:29:41 +0100
commit6fe5baf09d0222b4e3546e29fd6897f857483755 (patch)
tree9b99bf6f54d80623ab27a479e5335ee09c27eab1 /lexercise.lua
parent11ff8b0b78e054742de03c54e79dfba475e5c75b (diff)
downloadexercisesheets-6fe5baf09d0222b4e3546e29fd6897f857483755.tar.gz
exercisesheets-6fe5baf09d0222b4e3546e29fd6897f857483755.tar.bz2
exercisesheets-6fe5baf09d0222b4e3546e29fd6897f857483755.zip
started Lua interface
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
+