Browse Source

merge MDChunks into the surrounding CodeChunks if we dont allow continued blocks

pull/5/head
Fredrik Ekre 8 years ago
parent
commit
0d3fbfdefc
  1. 30
      src/Literate.jl
  2. 14
      test/runtests.jl

30
src/Literate.jl

@ -33,7 +33,7 @@ mutable struct CodeChunk <: Chunk
end end
CodeChunk() = CodeChunk(String[], false) CodeChunk() = CodeChunk(String[], false)
function parse(content) function parse(content; allow_continued = true)
lines = collect(eachline(IOBuffer(content))) lines = collect(eachline(IOBuffer(content)))
chunks = Chunk[] chunks = Chunk[]
@ -86,6 +86,30 @@ function parse(content)
last_code_chunk = i last_code_chunk = i
end end
# if we don't allow continued code blocks we need to merge MDChunks into the CodeChunks
if !allow_continued
merged_chunks = Chunk[]
continued = false
for chunk in chunks
if continued
@assert !isempty(merged_chunks)
if isa(chunk, CodeChunk)
append!(merged_chunks[end].lines, chunk.lines)
else # need to put back #'
for line in chunk.lines
push!(merged_chunks[end].lines, rstrip("#' " * line))
end
end
else
push!(merged_chunks, chunk)
end
if isa(chunk, CodeChunk)
continued = chunk.continued
end
end
chunks = merged_chunks
end
return chunks return chunks
end end
@ -354,8 +378,10 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide
nb["nbformat"] = JUPYTER_VERSION.major nb["nbformat"] = JUPYTER_VERSION.major
nb["nbformat_minor"] = JUPYTER_VERSION.minor nb["nbformat_minor"] = JUPYTER_VERSION.minor
# parse
chunks = parse(content; allow_continued = false)
## create the notebook cells ## create the notebook cells
chunks = parse(content)
cells = [] cells = []
for chunk in chunks for chunk in chunks
cell = Dict() cell = Dict()

14
test/runtests.jl

@ -413,18 +413,8 @@ end
""" """
"source": [ "source": [
"for i in 1:10\\n", "for i in 1:10\\n",
" print(i)" " print(i)\\n",
] "#' some markdown in a code block\\n",
""",
"""
"source": [
"some markdown in a code block"
]
""",
"""
"source": [
"end" "end"
] ]
""", """,

Loading…
Cancel
Save