From 3bb97a15902813e05eed153a6c96ffb7f1a64347 Mon Sep 17 00:00:00 2001 From: mitchelloharawild Date: Wed, 13 Dec 2023 14:32:31 +1100 Subject: [PATCH] Add processing of h1 and h2 headers into categories and questions Resolves #26 --- R/quiz-xml.R | 8 +++++++- inst/header.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 inst/header.lua diff --git a/R/quiz-xml.R b/R/quiz-xml.R index 0974cab..0e2fcf7 100644 --- a/R/quiz-xml.R +++ b/R/quiz-xml.R @@ -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( diff --git a/inst/header.lua b/inst/header.lua new file mode 100644 index 0000000..aab1dbc --- /dev/null +++ b/inst/header.lua @@ -0,0 +1,42 @@ +local moodle_category = [[ + + + $course$/%s + + +]] + +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