This commit is contained in:
MatsuneMikuroi committed 2026-10-16 09:05:43 +02:00
1 parent 8bbcf8ecd9
commit 45dd65b48e
116 files changed
+20457 -1164

No files matched your search

+76 -21
View File
@@ -54,23 +54,29 @@ local LABELS = {
right = "Correct", wrong = "Not quite",
score = "{n} of {m} questions correct",
["true"] = "true", ["false"] = "false",
hint = "Tick every statement you think is true.", page = "p." },
hint = "Tick every statement you think is true.", page = "p.",
qpoints = "{n}/{m} points", totalpoints = "{n} of {m} points",
avail = "{m} points" },
fr = { label = "Quiz", question = "Question", answers = "Réponses du quiz",
answer = "Réponses", check = "Vérifier", reset = "Réinitialiser",
right = "Correct", wrong = "Pas tout à fait",
score = "{n} question(s) correcte(s) sur {m}",
["true"] = "vrai", ["false"] = "faux",
hint = "Cochez chaque énoncé que vous pensez vrai.", page = "p." },
hint = "Cochez chaque énoncé que vous pensez vrai.", page = "p.",
qpoints = "{n}/{m} points", totalpoints = "{n} points sur {m}",
avail = "{m} points" },
de = { label = "Quiz", question = "Frage", answers = "Quiz-Lösungen",
answer = "Lösungen", check = "Prüfen", reset = "Zurücksetzen",
right = "Richtig", wrong = "Nicht ganz",
score = "{n} von {m} Fragen richtig",
["true"] = "wahr", ["false"] = "falsch",
hint = "Kreuzen Sie jede Aussage an, die Sie für wahr halten.", page = "S." },
hint = "Kreuzen Sie jede Aussage an, die Sie für wahr halten.", page = "S.",
qpoints = "{n}/{m} Punkte", totalpoints = "{n} von {m} Punkten",
avail = "{m} Punkte" },
}
local labels = {}
local opts = { pdf_answers = "end" }
local opts = { pdf_answers = "end", points = 3, penalty = 1 }
local TICKED, EMPTY = "\u{2612}", "\u{2610}" -- ☒ ☐, what pandoc's task_lists writes
@@ -119,6 +125,14 @@ local function read_meta(meta)
if meta.quiz["pdf-answers"] ~= nil then
opts.pdf_answers = str(meta.quiz["pdf-answers"])
end
-- numeric, so they go through tonumber and never through the label loop
-- above: a label is a string, and arithmetic on a string fails silently.
if meta.quiz.points ~= nil then
opts.points = tonumber(str(meta.quiz.points)) or opts.points
end
if meta.quiz.penalty ~= nil then
opts.penalty = tonumber(str(meta.quiz.penalty)) or opts.penalty
end
end
if quarto ~= nil and quarto.doc ~= nil then
@@ -181,45 +195,68 @@ local function split_why(blocks)
return body, why
end
-- one question -> { statement = Blocks, options = { {ok, body, why} }, why = Blocks }
local function parse_question(div)
-- one question -> { statement, options = { {ok, body, why} }, why, points, penalty }
-- `dflt` is what the quiz div (or the book metadata) set; a question may override
-- either number with its own points= / penalty= attribute.
local function parse_question(div, dflt)
local statement, options, qwhy = pandoc.Blocks({}), {}, pandoc.Blocks({})
for _, b in ipairs(div.content) do
if is_why(b) then
qwhy:extend(b.content)
elseif b.t == "BulletList" then
local consumed = true
local found = {}
-- Every item has to be a task item for the list to be the options. A
-- list that is PARTLY ticked is almost always a typo rather than an
-- ordinary bullet list in the statement, and silently swallowing it
-- costs the author a whole question, so say so.
local found, plain = {}, 0
for _, item in ipairs(b.content) do
local mark, blocks = task_marker(item)
if mark == nil then consumed = false; break end
local body, why = split_why(blocks)
found[#found + 1] = { ok = (mark == TICKED), body = body, why = why }
if mark == nil then
plain = plain + 1
else
local body, why = split_why(blocks)
found[#found + 1] = { ok = (mark == TICKED), body = body, why = why }
end
end
if consumed and #found > 0 then
if plain == 0 and #found > 0 then
for _, o in ipairs(found) do options[#options + 1] = o end
else
if #found > 0 then
io.stderr:write("[quiz] a bullet list mixes " .. #found
.. " task item(s) with " .. plain .. " plain one(s), so none of it"
.. " became options -- write every option as `- [x]` or `- [ ]`\n")
end
statement:insert(b) -- an ordinary bullet list belongs to the statement
end
else
statement:insert(b)
end
end
return { statement = statement, options = options, why = qwhy }
local a = div.attributes or {}
return {
statement = statement, options = options, why = qwhy,
points = tonumber(a.points) or dflt.points,
penalty = tonumber(a.penalty) or dflt.penalty,
}
end
local function parse_quiz(el)
local intro, questions = pandoc.Blocks({}), {}
local a = el.attributes or {}
local dflt = {
points = tonumber(a.points) or opts.points,
penalty = tonumber(a.penalty) or opts.penalty,
}
for _, b in ipairs(el.content) do
if is_question(b) then
questions[#questions + 1] = parse_question(b)
questions[#questions + 1] = parse_question(b, dflt)
else
intro:insert(b)
end
end
-- a quiz written without ::: {.question} is one question
if #questions == 0 then
local single = parse_question(el)
local single = parse_question(el, dflt)
if #single.options > 0 then
return pandoc.Blocks({}), { single }
end
@@ -248,11 +285,16 @@ local function raw(fmt, text) return pandoc.RawBlock(fmt, text) end
------------------------------------------------------------------ html
local function html_question(q, i, total)
local out = pandoc.Blocks({ raw("html", '<div class="quiz-q">') })
if total > 1 then
out:insert(raw("html", '<p class="quiz-q-head"><strong>'
.. esc_html(labels.question .. " " .. i) .. "</strong></p>"))
end
local out = pandoc.Blocks({ raw("html",
'<div class="quiz-q" data-points="' .. tostring(q.points)
.. '" data-penalty="' .. tostring(q.penalty) .. '">') })
-- The head is emitted even for a one-question quiz: it is where the score
-- lands after Check, so it cannot be conditional on there being a number.
out:insert(raw("html", '<p class="quiz-q-head">'
.. (total > 1
and ("<strong>" .. esc_html(labels.question .. " " .. i) .. "</strong>")
or "")
.. '<span class="quiz-q-score" hidden></span></p>'))
out:extend(q.statement)
out:insert(raw("html", '<ul class="quiz-opts">'))
@@ -286,6 +328,8 @@ local function html_quiz(num, title, intro, questions)
local out = pandoc.Blocks({ raw("html",
'<div class="quiz" id="quiz-' .. esc_html(num) .. '"'
.. ' data-quiz-score="' .. esc_html(labels.score) .. '"'
.. ' data-quiz-points="' .. esc_html(labels.totalpoints) .. '"'
.. ' data-quiz-qpoints="' .. esc_html(labels.qpoints) .. '"'
.. ' data-quiz-right="' .. esc_html(labels.right) .. '"'
.. ' data-quiz-wrong="' .. esc_html(labels.wrong) .. '">') })
@@ -319,8 +363,19 @@ end
local function latex_question(q, i, total)
local out = pandoc.Blocks({})
-- The points a question is worth are printed in the statement, because a
-- reader working on paper has no Check button to tell them afterwards.
local avail = fill(labels.avail, 0, q.points)
if total > 1 then
out:insert(pandoc.Para({ pandoc.Strong({ pandoc.Str(labels.question .. " " .. i) }) }))
out:insert(pandoc.Para({
pandoc.Strong({ pandoc.Str(labels.question .. " " .. i) }),
enspace(),
pandoc.RawInline("latex", "{\\small\\itshape (" .. esc_tex(avail) .. ")}"),
}))
else
out:insert(pandoc.Para({
pandoc.RawInline("latex", "{\\small\\itshape (" .. esc_tex(avail) .. ")}"),
}))
end
out:extend(q.statement)