From 7350e70413434338adaaee0c1d49e5b8d28fca09 Mon Sep 17 00:00:00 2001 From: Sogari Date: Sat, 25 Mar 2023 21:07:50 +0100 Subject: [PATCH] Add category discrimination (sc tag for single choice) --- src/Literate.jl | 95 ++++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/src/Literate.jl b/src/Literate.jl index 9d994fe..8ebc7d3 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -976,62 +976,75 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) admonition = filter(x -> x isa Markdown.Admonition, str.content) questionName = "$(admonition[1].title)" * "$(replace(string(gensym()), "#" => ""))" + questionCategory = admonition[1].category str = string(Markdown.MD(admonition[1])) - answers = [] - questionDict = Dict("correct" => "") - qBuf = IOBuffer() - - for line in split(str, "\n") - if startswith(lstrip(line), r"[1-9]\.") - answer = lstrip(line) - - correct = occursin("", string(answer)) || occursin("", string(answer)) - if correct + + if questionCategory == "sc" + answers = [] + questionDict = Dict("correct" => "") + qBuf = IOBuffer() + + for line in split(str, "\n") + if startswith(lstrip(line), r"[1-9]\.") + answer = lstrip(line) + + correct = occursin("", string(answer)) || occursin("", string(answer)) + if correct + answer = formatAnswer(answer) + questionDict["correct"] = escape_string(string(answer)) + end answer = formatAnswer(answer) - questionDict["correct"] = escape_string(string(answer)) - end - answer = formatAnswer(answer) - answer = string(answer) - push!(answers, answer) - else - if line != "" && !startswith(line, "!!!") - write(qBuf, lstrip(line), "\n") + answer = string(answer) + push!(answers, answer) + else + if line != "" && !startswith(line, "!!!") + write(qBuf, lstrip(line), "\n") + end end end - end - radioBind = writeBind(questionName, answers) - logicBind = writeLogic(questionName, questionDict) - - seek(qBuf, 0) - qStr = rstrip(read(qBuf, String)) + radioBind = writeBind(questionName, answers) + logicBind = writeLogic(questionName, questionDict) + + seek(qBuf, 0) + qStr = rstrip(read(qBuf, String)) + + result = writeControlFlow(questionName, qStr) + write(io, result, '\n') + + ################################################################ + # Content after the Admonition + ################################################################ + + if admoIndex < length(mdContent) + index = admoIndex + 1 + while index <= length(mdContent) + para = string(Markdown.MD(mdContent[index])) + write(io, para, '\n') + index += 1 + end + end + write(io, "\"\"\"\n") - result = writeControlFlow(questionName, qStr) - write(io, result, '\n') + # Pluto nb helper functions + cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold) - ################################################################ - # Content after the Admonition - ################################################################ + write(io, radioBind, '\n') + cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) - if admoIndex < length(mdContent) - index = admoIndex + 1 + write(io, logicBind, '\n') + cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) + else + index = admoIndex while index <= length(mdContent) para = string(Markdown.MD(mdContent[index])) write(io, para, '\n') index += 1 end + write(io, "\"\"\"\n") + cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold) end - write(io, "\"\"\"\n") - - # Pluto nb helper functions - cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold) - - write(io, radioBind, '\n') - cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) - - write(io, logicBind, '\n') - cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) else ################################################################ # If Chunk doesnt contain an Admonition