Browse Source

Refactor string formatting helper functions

pull/214/head
Sogari 3 years ago committed by Simon Christ
parent
commit
2530f9a7b8
  1. 38
      src/Literate.jl

38
src/Literate.jl

@ -864,6 +864,24 @@ function writeControlFlow(questionName, qStr, toWrite)
return controlFlow return controlFlow
end end
function chunkToMD(chunk)
buffer = IOBuffer()
for line in chunk.lines
write(buffer, line.first * line.second, '\n')
end
seek(buffer, 0)
str = Markdown.parse(read(buffer, String))
return str
end
function formatAnswer(answer)
answer = replace(answer, r"[1-9]\.?\s" => "")
answer = replace(answer, "<!---correct-->" => "")
answer = replace(answer, "<!–-correct–>" => "")
answer = replace(answer, "`" => "")
answer = rstrip(answer)
return answer
end
function create_notebook(flavor::PlutoFlavor, chunks, config) function create_notebook(flavor::PlutoFlavor, chunks, config)
ionb = IOBuffer() ionb = IOBuffer()
@ -905,12 +923,7 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
elseif containsAdmonition(chunk) elseif containsAdmonition(chunk)
write(io, "$(flavor.use_cm ? "cm" : "md")\"\"\"\n") write(io, "$(flavor.use_cm ? "cm" : "md")\"\"\"\n")
buffer = IOBuffer() str = chunkToMD(chunk)
for line in chunk.lines
write(buffer, line.first * line.second, '\n')
end
seek(buffer, 0)
str = Markdown.parse(read(buffer, String))
############################################################ ############################################################
#Content before the Admonition #Content before the Admonition
@ -951,19 +964,10 @@ function create_notebook(flavor::PlutoFlavor, chunks, config)
correct = occursin("<!---correct-->", string(answer)) || occursin("<!–-correct–>", string(answer)) correct = occursin("<!---correct-->", string(answer)) || occursin("<!–-correct–>", string(answer))
if correct if correct
answer = replace(answer, r"[1-9]\.?\s" => "") answer = formatAnswer(answer)
answer = replace(answer, "<!---correct-->" => "")
answer = replace(answer, "<!–-correct–>" => "")
answer = replace(answer, "`" => "")
answer = rstrip(answer)
questionDict["correct"] = escape_string(string(answer)) questionDict["correct"] = escape_string(string(answer))
end end
answer = formatAnswer(answer)
answer = replace(answer, r"[1-9]\.?\s" => "")
answer = replace(answer, "<!---correct-->" => "")
answer = replace(answer, "<!–-correct–>" => "")
answer = replace(answer, "`" => "")
answer = rstrip(answer)
answer = string(answer) answer = string(answer)
push!(answers, answer) push!(answers, answer)
else else

Loading…
Cancel
Save