Browse Source

Add normal behavior when no Admonition, refactor ControlFlow

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

254
src/Literate.jl

@ -125,27 +125,6 @@ function parse(content; allow_continued = true)
return chunks return chunks
end end
text = """
# This is not inside the admonition
# !!! danger "Question"
# This is the question
#
# 1. `] install ForwardDiff`
# 2. `add ForwardDiff`
# 3. `] add ForwardDiff.jl`
# 4. `] add ForwardDiff` <!---correct-->
#
# This is not inside the admonition.
"""
text2 = """
# !!! danger "Question"
# This is the Question
# 1. Hello
# 2. World <!---correct-->
"""
parse(text2)
function replace_default(content, sym; function replace_default(content, sym;
config::Dict, config::Dict,
branch = "gh-pages", branch = "gh-pages",
@ -872,6 +851,19 @@ function writeLogic(questionName, questionDict)
return logic return logic
end end
function writeControlFlow(questionName, qStr, toWrite)
controlFlow = """
\$(
if $(questionName)Test($(questionName)Answer)
Markdown.MD(Markdown.Admonition("correct", "$(questionName)", [md"$(qStr)", md"$(toWrite)"]))
else
Markdown.MD(Markdown.Admonition("danger", "$(questionName)", [md"$(qStr)", md"$(toWrite)"]))
end
)
"""
return controlFlow
end
function create_notebook(flavor::PlutoFlavor, chunks, config) function create_notebook(flavor::PlutoFlavor, chunks, config)
ionb = IOBuffer() ionb = IOBuffer()
@ -910,148 +902,136 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
if length(chunk.lines) == 1 if length(chunk.lines) == 1
line = escape_string(chunk.lines[1].second, '"') line = escape_string(chunk.lines[1].second, '"')
write(io, "$(flavor.use_cm ? "cm" : "md")\"", line, "\"\n") write(io, "$(flavor.use_cm ? "cm" : "md")\"", line, "\"\n")
else elseif containsAdmonition(chunk)
write(io, "$(flavor.use_cm ? "cm" : "md")\"\"\"\n")
# for line in chunk.lines
# write(io, line.second, '\n') # Skip indent
# end
# write(io, "\"\"\"\n")
if containsAdmonition(chunk) buffer = IOBuffer()
write(io, "$(flavor.use_cm ? "cm" : "md")\"\"\"\n") for line in chunk.lines
write(buffer, line.first * line.second, '\n')
buffer = IOBuffer() end
for line in chunk.lines seek(buffer, 0)
write(buffer, line.first * line.second, '\n') str = Markdown.parse(read(buffer, String))
end
seek(buffer, 0) ############################################################
str = Markdown.parse(read(buffer, String)) #Content before the Admonition
############################################################
############################################################
#Content before the Admonition mdContent = str.content
############################################################ admoIndex = 1
for (i, item) in enumerate(mdContent)
mdContent = str.content if isa(item, Markdown.Admonition)
admoIndex = 1 admoIndex = i
for (i, item) in enumerate(mdContent)
if isa(item, Markdown.Admonition)
admoIndex = i
end
end end
end
if admoIndex > 1 if admoIndex > 1
index = 1 index = 1
while index < admoIndex while index < admoIndex
para = string(Markdown.MD(mdContent[index])) para = string(Markdown.MD(mdContent[index]))
write(io, para, '\n') write(io, para, '\n')
index += 1 index += 1
end
end end
end
############################################################ ############################################################
#The Admonition #The Admonition
############################################################ ############################################################
admonition = filter(x -> x isa Markdown.Admonition, str.content)
questionName = "$(admonition[1].title)" * "$(replace(string(gensym()), "#" => ""))"
str = string(Markdown.MD(admonition[1]))
answers = [] admonition = filter(x -> x isa Markdown.Admonition, str.content)
questionDict = Dict("correct" => "") questionName = "$(admonition[1].title)" * "$(replace(string(gensym()), "#" => ""))"
qBuf = IOBuffer() str = string(Markdown.MD(admonition[1]))
for line in split(str, "\n") answers = []
if startswith(lstrip(line), r"[1-9]\.") questionDict = Dict("correct" => "")
answer = lstrip(line) qBuf = IOBuffer()
correct = occursin("<!---correct-->", string(answer)) || occursin("<!–-correct–>", string(answer)) for line in split(str, "\n")
if correct if startswith(lstrip(line), r"[1-9]\.")
answer = replace(answer, r"[1-9]\.?\s" => "") answer = lstrip(line)
answer = replace(answer, "<!---correct-->" => "")
answer = replace(answer, "<!–-correct–>" => "")
answer = replace(answer, "`" => "")
answer = rstrip(answer)
questionDict["correct"] = escape_string(string(answer))
end
correct = occursin("<!---correct-->", string(answer)) || occursin("<!–-correct–>", string(answer))
if correct
answer = replace(answer, r"[1-9]\.?\s" => "") answer = replace(answer, r"[1-9]\.?\s" => "")
answer = replace(answer, "<!---correct-->" => "") answer = replace(answer, "<!---correct-->" => "")
answer = replace(answer, "<!–-correct–>" => "") answer = replace(answer, "<!–-correct–>" => "")
answer = replace(answer, "`" => "") answer = replace(answer, "`" => "")
answer = rstrip(answer) answer = rstrip(answer)
answer = string(answer) questionDict["correct"] = escape_string(string(answer))
push!(answers, answer)
else
if line != "" && !startswith(line, "!!!")
write(qBuf, lstrip(line), "\n") # why 2 \n
end
end end
end
radioBind = writeBind(questionName, answers) answer = replace(answer, r"[1-9]\.?\s" => "")
logicBind = writeLogic(questionName, questionDict) answer = replace(answer, "<!---correct-->" => "")
answer = replace(answer, "<!–-correct–>" => "")
answer = replace(answer, "`" => "")
answer = rstrip(answer)
answer = string(answer)
push!(answers, answer)
else
if line != "" && !startswith(line, "!!!")
write(qBuf, lstrip(line), "\n") # why 2 \n
end
end
end
# toWrite = " " * "\$(eval(md\"\$(" * "$name" * ")\"))" * "\n" # for interactivity in the notebook (else it isn't reactive) radioBind = writeBind(questionName, answers)
logicBind = writeLogic(questionName, questionDict)
name = "$(questionName)Check" # toWrite = " " * "\$(eval(md\"\$(" * "$name" * ")\"))" * "\n" # for interactivity in the notebook (else it isn't reactive)
toWrite = "\$(" * "$name" * ")"
seek(qBuf, 0) name = "$(questionName)Check"
qStr = read(qBuf, String) toWrite = "\$($name)"
qStr = rstrip(qStr)
result = """ seek(qBuf, 0)
\$( qStr = read(qBuf, String)
if $(questionName)Test($(questionName)Answer) qStr = rstrip(qStr)
Markdown.MD(Markdown.Admonition("correct", "$(questionName)", [md"$(qStr)", md"$(toWrite)"]))
else
Markdown.MD(Markdown.Admonition("danger", "$(questionName)", [md"$(qStr)", md"$(toWrite)"]))
end
)
"""
result = writeControlFlow(questionName, qStr, toWrite)
write(io, result, '\n') write(io, result, '\n')
if admoIndex < length(mdContent) if admoIndex < length(mdContent)
index = admoIndex + 1 index = admoIndex + 1
while index <= length(mdContent) while index <= length(mdContent)
para = string(Markdown.MD(mdContent[index])) para = string(Markdown.MD(mdContent[index]))
write(io, para, '\n') write(io, para, '\n')
index += 1 index += 1
end
end end
write(io, "\"\"\"\n")
write(io, '\n')
content = String(take!(io))
uuid = uuid4(content, cellCounter)
cellCounter += 1
push!(uuids, uuid)
push!(folds, fold)
print(ionb, "# ╔═╡ ", uuid, '\n')
write(ionb, content, '\n')
write(io, radioBind, '\n')
write(io, '\n')
content = String(take!(io))
uuid = uuid4(content, cellCounter)
cellCounter += 1
push!(singleChoiceUuids, uuid)
push!(singleChoiceFolds, fold)
push!(singleChoiceContent, content)
write(io, logicBind, '\n')
write(io, '\n')
content = String(take!(io))
uuid = uuid4(content, cellCounter)
cellCounter += 1
push!(singleChoiceUuids, uuid)
push!(singleChoiceFolds, fold)
push!(singleChoiceContent, content)
end end
write(io, "\"\"\"\n")
write(io, '\n')
content = String(take!(io))
uuid = uuid4(content, cellCounter)
cellCounter += 1
push!(uuids, uuid)
push!(folds, fold)
print(ionb, "# ╔═╡ ", uuid, '\n')
write(ionb, content, '\n')
write(io, radioBind, '\n')
write(io, '\n')
content = String(take!(io))
uuid = uuid4(content, cellCounter)
cellCounter += 1
push!(singleChoiceUuids, uuid)
push!(singleChoiceFolds, fold)
push!(singleChoiceContent, content)
write(io, logicBind, '\n')
write(io, '\n')
content = String(take!(io))
uuid = uuid4(content, cellCounter)
cellCounter += 1
push!(singleChoiceUuids, uuid)
push!(singleChoiceFolds, fold)
push!(singleChoiceContent, content)
else
write(io, "$(flavor.use_cm ? "cm" : "md")\"\"\"\n")
for line in chunk.lines
write(io, line.second, '\n') # Skip indent
end
write(io, "\"\"\"\n")
end end
content = String(take!(io)) content = String(take!(io))
else # isa(chunk, CodeChunk) else # isa(chunk, CodeChunk)

Loading…
Cancel
Save