Skip to content

Commit

Permalink
Add base category option to front matter
Browse files Browse the repository at this point in the history
Resolves #27
  • Loading branch information
mitchelloharawild committed Dec 13, 2023
1 parent a3d0334 commit e85b7a1
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions inst/header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local moodle_category = [[
</question>
]]

local base_category = nil
local cloze_pattern = "{%d+:"

function process_header (elem)
Expand All @@ -21,19 +22,32 @@ function process_header (elem)
return pandoc.Div({}, elem.attr)
end

function Pandoc(doc)

function moodlequiz_meta(meta)
if (meta.moodlequiz ~= nil and meta.moodlequiz.category ~= nil) then
base_category = pandoc.utils.stringify(meta.moodlequiz.category)
end
end

function header_questions(doc)
local hblocks = {}
local category_idx = 0
local in_question = false

-- Set base category if specified
if base_category ~= nil then
category_idx = category_idx + 1
table.insert(hblocks, pandoc.Div({}, pandoc.Attr('', {'question'}, {type = 'category', category = base_category})))
end

-- Re-organise headers into questions containing following elements
for _,el in pairs(doc.blocks) do
if (el.t ~= "Header" or el.level > 2 or el.classes[1] == "header") then
-- If a question div hasn't yet been opened, create one.
if (not in_question) then
in_question = true
category_idx = category_idx + 1
table.insert(hblocks, process_header(pandoc.Header(2, 'Unnamed question', pandoc.Attr())))
table.insert(hblocks, pandoc.Div({}, pandoc.Attr('', {'question'})))
end
hblocks[category_idx].content:insert(el)
elseif (el.t == "Header") then
Expand All @@ -46,7 +60,11 @@ function Pandoc(doc)
if (not in_question) then
el.attributes.type = 'category'
if (el.attributes.category == nil) then
el.attributes.category = el.identifier
if base_category ~= nil then
el.attributes.category = base_category .. "/" .. el.identifier
else
el.attributes.category = el.identifier
end
end
end

Expand Down Expand Up @@ -76,3 +94,12 @@ function Pandoc(doc)

return pandoc.Pandoc(hblocks, doc.meta)
end

return {
{
Meta = moodlequiz_meta
},
{
Pandoc = header_questions
}
}

0 comments on commit e85b7a1

Please sign in to comment.