Skip to content

Commit

Permalink
Add processing of h1 and h2 headers into categories and questions
Browse files Browse the repository at this point in the history
Resolves #26
  • Loading branch information
mitchelloharawild committed Dec 13, 2023
1 parent a3f1434 commit 3bb97a1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion R/quiz-xml.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ moodlequiz <- function(self_contained = TRUE,
output_file
}

filters <- vapply(
c("header.lua", "question.lua", "code.lua"),
system.file, character(1L),
package = "moodlequiz", mustWork = TRUE
)

# return format
out <- output_format(
knitr = knitr_options(),
pandoc = pandoc_options(to = "html", ext = ".xml", lua_filters = c(system.file("question.lua", package = "moodlequiz"), system.file("code.lua", package = "moodlequiz"))),
pandoc = pandoc_options(to = "html", ext = ".xml", lua_filters = filters),
pre_processor = pre_processor,
post_processor = post_processor,
base_format = bookdown::html_document2(
Expand Down
42 changes: 42 additions & 0 deletions inst/header.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local moodle_category = [[
<question type="category">
<category>
<text>$course$/%s</text>
</category>
</question>
]]

function process_header (elem)
if (elem.classes[1] == "header" or elem.level > 2) then return elem end
elem.classes = {'question'}
elem.attributes.name = pandoc.utils.stringify(elem.content[1])
if (elem.level == 1) then
elem.attributes.type = 'category'
elem.attributes.category = elem.identifier
end
if (elem.attributes.type == nil) then
elem.attributes.type = 'cloze'
end
return pandoc.Div({}, elem.attr)
end

function Pandoc(doc)
local hblocks = {}
local category_idx = 0
local in_category = false
for i,el in pairs(doc.blocks) do
if (el.t ~= "Header" or el.level > 2 or el.classes[1] == "header") then
if (not in_category) then
category_idx = category_idx + 1
table.insert(hblocks, el)
else
hblocks[category_idx].content:insert(el)
end
elseif (el.t == "Header") then
in_category = true
category_idx = category_idx + 1
table.insert(hblocks, process_header(el))
end
end
return pandoc.Pandoc(hblocks, doc.meta)
end

0 comments on commit 3bb97a1

Please sign in to comment.