Browse Source

Add multiple Admonitions per chunk

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

128
src/Literate.jl

@ -994,35 +994,23 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -994,35 +994,23 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
write(io, "$(flavor.use_cm ? "cm" : "md")\"\"\"\n")
str = chunkToMD(chunk)
helperList = []
helperTestList = []
# Content before the Admonition
################################################################
mdContent = str.content
admoIndex = 1
for (i, item) in enumerate(mdContent)
if isa(item, Markdown.Admonition)
admoIndex = i
end
end
if admoIndex > 1
index = 1
while index < admoIndex
para = string(Markdown.MD(mdContent[index]))
write(io, para, '\n')
index += 1
end
end
for item in mdContent
if !isa(item, Markdown.Admonition)
write(io, string(Markdown.MD(item)), '\n')
elseif isa(item, Markdown.Admonition)
# The Admonition
################################################################
########################################################
admonition = filter(x -> x isa Markdown.Admonition, str.content)
questionName = replace("$(admonition[1].title)" * "$(replace(string(gensym()), "#" => ""))", r"[^\d\w]+" => "")
questionCategory = admonition[1].category
str = string(Markdown.MD(admonition[1]))
admonition = item
questionName = replace("$(admonition.title)" * "$(replace(string(gensym()), "#" => ""))", r"[^\d\w]+" => "")
questionCategory = admonition.category
################################################################
# Single-Choice Admonition
@ -1032,7 +1020,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -1032,7 +1020,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
answers = []
questionDict = Dict("correct" => "")
answerList = filter(x -> isa(x, Markdown.List), admonition[1].content)
answerList = filter(x -> isa(x, Markdown.List), admonition.content)
answerStr = string(Markdown.MD(answerList[end]))
@ -1051,7 +1039,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -1051,7 +1039,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
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
push!(restList, answerList[begin:end-1])
end
@ -1062,29 +1050,12 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -1062,29 +1050,12 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
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)
push!(helperList, radioBind)
push!(helperList, logicBind)
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)
elseif questionCategory == "mc"
############################################################
# Multiple-Choice Admonition
@ -1093,7 +1064,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -1093,7 +1064,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
answers = []
questionDict = Dict("correct" => String[])
answerList = filter(x -> isa(x, Markdown.List), admonition[1].content)
answerList = filter(x -> isa(x, Markdown.List), admonition.content)
answerStr = string(Markdown.MD(answerList[end]))
for line in split(answerStr, "\n")
@ -1110,7 +1081,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -1110,7 +1081,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
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
push!(restList, answerList[begin:end-1])
end
@ -1121,29 +1092,11 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -1121,29 +1092,11 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
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 = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold)
write(io, logicBind, '\n')
cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold)
push!(helperList, radioBind)
push!(helperList, logicBind)
elseif questionCategory == "freecode"
############################################################
@ -1152,7 +1105,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -1152,7 +1105,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
tests = []
testList = filter(x -> isa(x, Markdown.List), admonition[1].content)
testList = filter(x -> isa(x, Markdown.List), admonition.content)
testStr = string(Markdown.MD(testList[end]))
for line in split(testStr, "\n")
@ -1162,7 +1115,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -1162,7 +1115,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
end
end
restList = filter(x -> !isa(x, Markdown.List), admonition[1].content)
restList = filter(x -> !isa(x, Markdown.List), admonition.content)
if length(testList) > 1
push!(restList, testList[begin:end-1])
end
@ -1173,44 +1126,35 @@ function create_notebook(flavor::PlutoFlavor, chunks, config) @@ -1173,44 +1126,35 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
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)
push!(helperTestList, radioBind)
push!(helperList, logicBind)
else
############################################################
# Normal Admonitions
############################################################
index = admoIndex
while index <= length(mdContent)
para = string(Markdown.MD(mdContent[index]))
write(io, para, '\n')
index += 1
write(io, string(Markdown.MD(item)), '\n')
end
end
end
write(io, "\"\"\"\n")
cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold)
for item in helperTestList
write(io, item, '\n')
cellCounter = formatCells(io, ionb, cellCounter, uuids, folds, fold)
end
for item in helperList
write(io, item, '\n')
cellCounter = formatCellsEnd(io, ionb, cellCounter, singleChoiceContent, singleChoiceUuids, singleChoiceFolds, fold)
end
else
################################################################
# Chunk doesnt contain an Admonition

Loading…
Cancel
Save