--- moodle-test.lua – writer to convert markdown questions into Moodle XML --- --- Copyright: © 2026 Florian Fischer --- License: GPL-3.0-or-later -- Collected questions local questions = {} local cur_question = nil local concat = function(inlines) local s = pandoc.write(pandoc.Pandoc(pandoc.Plain(inlines)), 'plain', {wrap_text='wrap-none'}) if s:sub(-1) == '\n' then s = s:sub(1, -2) end return s end local function find_answer_attributes(answer) local attributes = {} answer:walk({Span=function(s) if #s.classes > 0 then -- this is not the attribute span return end for k, v in pairs(s.attributes) do attributes[k] = v end end}) return attributes end local function detect_question_type(question) local ret = "" if question.header.classes:includes("shortanswer") then ret = "shortanswer" elseif question.header.classes:includes("multichoice") then ret = "multichoice" elseif question.header.classes:includes("cloze") then ret = "cloze" elseif question.header.classes:includes("ddwtos") then ret = "ddwtos" else pandoc.log.warn("No question type found") end return ret end local function write_answers(question, default_fraction, a) for _, answer in ipairs(question.answers) do local answer_attributes = find_answer_attributes(answer) local frac = answer_attributes.fraction or default_fraction a(' \n') a(' \n') if answer.feedback then a(' \n') end a(' \n') end end local function write_shortanwser(question, a) a(' 1.0000000\n') a(' 0\n') write_answers(question, 100, a) end local function write_multichoice(question, a) local fractions = 0 for _, answ in ipairs(question.answers) do local answer_attributes = find_answer_attributes(answ) if answer_attributes.fraction and tonumber(answer_attributes.fraction) > 0 then fractions = fractions + 1 end end a(' 1.0000000\n') a(' '..(fractions == 1 and "true" or "false")..'') a(' true') a(' none') a(' 0') a(' ') a(' Die Antwort ist richtig.

]]>
') a('
') a(' ') a(' Die Antwort ist teilweise richtig.

]]>
') a('
') a(' ') a(' Die Antwort ist falsch.

]]>
') a('
') a(' ') write_answers(question, 0, a) end local function write_cloze(question, a) end local function write_ddwtos(question, a) a(' 1.0000000\n') a(' 0\n') a(' \n') for _, answer in ipairs(question.answers) do local answer_attributes = find_answer_attributes(answer) local group = answer_attributes.fraction or 1 a(' \n') a(' '); a(pandoc.write(pandoc.Pandoc(answer), "plain")) a(' \n') a(' ' .. group..'\n') if answer.feedback then a(' \n') end a(' \n') end end local write_question_for = { cloze = write_cloze, ddwtos = write_ddwtos, multichoice = write_multichoice, shortanswer = write_shortanwser, } local function write_questions() local res = "" local a = function(s) res = res .. s end for _, question in ipairs(questions) do if not question then io.stderr:write("nil question\n") -- pandoc.log.warn("nil question") end local question_type = detect_question_type(question) -- Question type a('\n') -- Question name a(' '); a(concat(question.name)) a('\n') -- Question text a(' \n \n \n') a(' \n') a(' 0.3333333\n') a(' 0\n') a(' \n') -- questiontype specific tags local question_type_fn = write_question_for[question_type] question_type_fn(question, a) a('\n') end return res end local function Header(h) cur_question = { header=h, text={}} table.insert(questions, cur_question) cur_question.name = h.content end local function Image(i) local datauri = pandoc.mediabag.make_data_uri(pandoc.mediabag.fetch(i.src)) local ni = pandoc.Image(i.caption, datauri) ni.attributes = i.attributes return ni end local function Div(d) -- append question content table.insert(cur_question.text, pandoc.walk_block(d, {Image=Image})) -- embed all images end local function addAnswer(l) local fl = l:walk({Image=Image}) cur_question.answers = fl.content end local filter = { -- Traverse the AST depth-first to reach the content of a question after -- reaching its header traverse = 'topdown', Header = Header, -- Detect questions Div = Div, -- Collect question content BulletList = addAnswer, -- Collect question answers OrderedList = addAnswer, -- Collect question answers } Writer = function(doc, opts) local ctx = pandoc.template.meta_to_context(doc.meta, concat, concat) -- collect questions doc:walk(filter) local body = write_questions() ctx['body'] = body for k, v in pairs(opts.variables) do ctx[k] = v end if not opts.template then pandoc.log.warn("No quiz template specified") os.exit(1) end local complete = pandoc.template.apply(opts.template, ctx) return complete:render() end