diff --git a/Project.toml b/Project.toml index bfad104..192960f 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,7 @@ IOCapture = "b5f81e59-6552-4d32-b1f0-c071b021bf89" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] IOCapture = "0.2" diff --git a/src/Literate.jl b/src/Literate.jl index acc2984..2a9f163 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -845,6 +845,16 @@ function writeMultiBind(questionName, answers) return """$(questionName)Check = @bind $(questionName)Answer confirm(MultiCheckBox($(multi), orientation=:column));""" end +function writeFreeBind(questionName, tests) + return """$(questionName)Check = @bind $(questionName)Answer MultiCheckBox([ + $(join([""" + try + string($(test)) + catch + "to be added" + end => "$(test)" """ for test in tests], ",\n"))], orientation=:column, default=["Test Passed" for test in $tests]);""" +end + function writeSingleLogic(questionName, questionDict) logic = """ @@ -872,6 +882,15 @@ function writeMultiLogic(questionName, questionDict) return logic end +function writeFreeLogic(questionName, tests) + logic = + """ + function $(questionName)Test($(questionName)Answer) + return $(questionName)Answer == ["Test Passed" for test in $tests] + end;""" + return logic +end + function writeControlFlow(questionName, restList) controlFlow = """ \$( @@ -896,7 +915,7 @@ function chunkToMD(chunk) end function formatAnswer(answer) - answer = replace(answer, r"[1-9]\.?\s" => "") + answer = replace(answer, r"^[1-9]\.\s" => "") answer = replace(answer, "" => "") answer = replace(answer, "" => "") answer = replace(answer, "`" => "") @@ -904,6 +923,12 @@ function formatAnswer(answer) return string(answer) end +function formatTest(test) + test = replace(test, r"^[1-9]\.\s" => "") + test = rstrip(test) + return string(test) +end + function formatCells(io, ionb, cellCounter, uuids, folds, fold) content = String(take!(io)) uuid = uuid4(content, cellCounter) @@ -935,7 +960,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) ### A Pluto.jl notebook ### # v0.16.0 # ╔═╡ a0000000-0000-0000-0000-000000000000 - using $(flavor.use_cm ? "CommonMark, PlutoUI" : "Markdown") + using $(flavor.use_cm ? "CommonMark, PlutoUI, Test" : "Markdown") """) @@ -987,6 +1012,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) para = string(Markdown.MD(mdContent[index])) write(io, para, '\n') index += 1 + println(para) end end @@ -1120,6 +1146,58 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) write(io, logicBind, '\n') cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) + elseif questionCategory == "freecode" + ############################################################ + # Freecode Admonition + ############################################################ + + tests = [] + + testList = filter(x -> isa(x, Markdown.List), admonition[1].content) + testStr = string(Markdown.MD(testList)) + + for line in split(testStr, "\n") + if startswith(lstrip(line), r"[1-9]\.") + test = formatTest(lstrip(line)) + push!(tests, test) + end + end + + codeList = filter(x -> isa(x, Markdown.Code), admonition[1].content) + codeStr = string(Markdown.MD(codeList)) + + restList = filter(x -> !isa(x, Markdown.List), admonition[1].content) + + radioBind = writeFreeBind(questionName, tests) + logicBind = writeFreeLogic(questionName, tests) + + result = writeControlFlow(questionName, restList) + 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") + + # Pluto nb helper functions + ############################################################ + + cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold) + + write(io, radioBind, '\n') + cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold) + + write(io, logicBind, '\n') + cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) + else ############################################################ # Normal Admonitions