Browse Source

Add multiple Admonitions per chunk

pull/214/head
Sogari 3 years ago committed by Simon Christ
parent
commit
d8f065e156
  1. 312
      src/Literate.jl

312
src/Literate.jl

@ -994,223 +994,167 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
write(io, "$(flavor.use_cm ? "cm" : "md")\"\"\"\n") write(io, "$(flavor.use_cm ? "cm" : "md")\"\"\"\n")
str = chunkToMD(chunk) str = chunkToMD(chunk)
helperList = []
helperTestList = []
# Content before the Admonition # Content before the Admonition
################################################################ ################################################################
mdContent = str.content mdContent = str.content
admoIndex = 1 for item in mdContent
for (i, item) in enumerate(mdContent) if !isa(item, Markdown.Admonition)
if isa(item, Markdown.Admonition) write(io, string(Markdown.MD(item)), '\n')
admoIndex = i elseif isa(item, Markdown.Admonition)
end # The Admonition
end ########################################################
if admoIndex > 1
index = 1
while index < admoIndex
para = string(Markdown.MD(mdContent[index]))
write(io, para, '\n')
index += 1
end
end
# The Admonition
################################################################
admonition = filter(x -> x isa Markdown.Admonition, str.content) admonition = item
questionName = replace("$(admonition[1].title)" * "$(replace(string(gensym()), "#" => ""))", r"[^\d\w]+" => "") questionName = replace("$(admonition.title)" * "$(replace(string(gensym()), "#" => ""))", r"[^\d\w]+" => "")
questionCategory = admonition[1].category questionCategory = admonition.category
str = string(Markdown.MD(admonition[1]))
################################################################
################################################################ # Single-Choice Admonition
# Single-Choice Admonition ################################################################
################################################################
if questionCategory == "sc"
answers = []
questionDict = Dict("correct" => "")
answerList = filter(x -> isa(x, Markdown.List), admonition.content)
answerStr = string(Markdown.MD(answerList[end]))
#there might be a nicer way to do it by using radio pairs
for line in split(answerStr, "\n")
if startswith(lstrip(line), r"[1-9]\.")
answer = lstrip(line)
correct = occursin("<!---correct-->", string(answer)) || occursin("<!–-correct–>", string(answer))
if correct
answer = formatAnswer(answer)
questionDict["correct"] = escape_string(answer)
end
answer = formatAnswer(answer)
push!(answers, answer)
end
end
if questionCategory == "sc" restList = filter(x -> !isa(x, Markdown.List), admonition.content)
answers = [] if length(answerList) > 1
questionDict = Dict("correct" => "") push!(restList, answerList[begin:end-1])
end
answerList = filter(x -> isa(x, Markdown.List), admonition[1].content) radioBind = writeRadioBind(questionName, answers)
answerStr = string(Markdown.MD(answerList[end])) logicBind = writeSingleLogic(questionName, questionDict)
result = writeControlFlow(questionName, restList)
write(io, result, '\n')
# Pluto nb helper functions
############################################################
push!(helperList, radioBind)
push!(helperList, logicBind)
#there might be a nicer way to do it by using radio pairs elseif questionCategory == "mc"
for line in split(answerStr, "\n") ############################################################
if startswith(lstrip(line), r"[1-9]\.") # Multiple-Choice Admonition
answer = lstrip(line) ############################################################
correct = occursin("<!---correct-->", string(answer)) || occursin("<!–-correct–>", string(answer)) answers = []
if correct questionDict = Dict("correct" => String[])
answer = formatAnswer(answer)
questionDict["correct"] = escape_string(answer) answerList = filter(x -> isa(x, Markdown.List), admonition.content)
answerStr = string(Markdown.MD(answerList[end]))
for line in split(answerStr, "\n")
if startswith(lstrip(line), r"[1-9]\.")
answer = lstrip(line)
correct = occursin("<!---correct-->", string(answer)) || occursin("<!–-correct–>", string(answer))
if correct
answer = formatAnswer(answer)
push!(questionDict["correct"], escape_string(answer))
end
answer = formatAnswer(answer)
push!(answers, answer)
end
end end
answer = formatAnswer(answer)
push!(answers, answer)
end
end
restList = filter(x -> !isa(x, Markdown.List), admonition[1].content) restList = filter(x -> !isa(x, Markdown.List), admonition.content)
if length(answerList) > 1 if length(answerList) > 1
push!(restList, answerList[begin:end-1]) push!(restList, answerList[begin:end-1])
end end
radioBind = writeRadioBind(questionName, answers)
logicBind = writeSingleLogic(questionName, questionDict)
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 radioBind = writeMultiBind(questionName, answers)
############################################################ logicBind = writeMultiLogic(questionName, questionDict)
cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold) result = writeControlFlow(questionName, restList)
write(io, result, '\n')
write(io, radioBind, '\n') # Pluto nb helper functions
cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) ############################################################
push!(helperList, radioBind)
push!(helperList, logicBind)
write(io, logicBind, '\n') elseif questionCategory == "freecode"
cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) ############################################################
elseif questionCategory == "mc" # Freecode Admonition
############################################################ ############################################################
# Multiple-Choice Admonition
############################################################ tests = []
answers = []
questionDict = Dict("correct" => String[])
answerList = filter(x -> isa(x, Markdown.List), admonition[1].content) testList = filter(x -> isa(x, Markdown.List), admonition.content)
answerStr = string(Markdown.MD(answerList[end])) testStr = string(Markdown.MD(testList[end]))
for line in split(answerStr, "\n") for line in split(testStr, "\n")
if startswith(lstrip(line), r"[1-9]\.") if startswith(lstrip(line), r"[1-9]\.")
answer = lstrip(line) test = formatTest(lstrip(line))
push!(tests, test)
correct = occursin("<!---correct-->", string(answer)) || occursin("<!–-correct–>", string(answer)) end
if correct
answer = formatAnswer(answer)
push!(questionDict["correct"], escape_string(answer))
end end
answer = formatAnswer(answer)
push!(answers, answer)
end
end
restList = filter(x -> !isa(x, Markdown.List), admonition[1].content)
if length(answerList) > 1
push!(restList, answerList[begin:end-1])
end
radioBind = writeMultiBind(questionName, answers) restList = filter(x -> !isa(x, Markdown.List), admonition.content)
logicBind = writeMultiLogic(questionName, questionDict) if length(testList) > 1
push!(restList, testList[begin:end-1])
result = writeControlFlow(questionName, restList) end
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 radioBind = writeFreeBind(questionName, tests)
############################################################ logicBind = writeFreeLogic(questionName, tests)
cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold) result = writeControlFlow(questionName, restList)
write(io, result, '\n')
write(io, radioBind, '\n') # Pluto nb helper functions
cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) ############################################################
write(io, logicBind, '\n') push!(helperTestList, radioBind)
cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) push!(helperList, logicBind)
elseif questionCategory == "freecode"
############################################################
# Freecode Admonition
############################################################
tests = []
testList = filter(x -> isa(x, Markdown.List), admonition[1].content) else
testStr = string(Markdown.MD(testList[end])) ############################################################
# Normal Admonitions
############################################################
for line in split(testStr, "\n") write(io, string(Markdown.MD(item)), '\n')
if startswith(lstrip(line), r"[1-9]\.")
test = formatTest(lstrip(line))
push!(tests, test)
end end
end end
end
restList = filter(x -> !isa(x, Markdown.List), admonition[1].content) write(io, "\"\"\"\n")
if length(testList) > 1 cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold)
push!(restList, testList[begin:end-1])
end
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 for item in helperTestList
############################################################ write(io, item, '\n')
cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold) cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold)
end
write(io, radioBind, '\n') for item in helperList
cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold) write(io, item, '\n')
write(io, logicBind, '\n')
cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold) cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold)
else
############################################################
# Normal Admonitions
############################################################
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 end
else else
################################################################ ################################################################
# Chunk doesnt contain an Admonition # Chunk doesnt contain an Admonition

Loading…
Cancel
Save