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 @@ -33,7 +33,7 @@ mutable struct CodeChunk <: Chunk
end
CodeChunk() = CodeChunk(String[], false)
function parse(content)
function parse(content; allow_continued = true)
lines = collect(eachline(IOBuffer(content)))
chunks = Chunk[]
@ -86,6 +86,30 @@ function parse(content) @@ -86,6 +86,30 @@ function parse(content)
last_code_chunk = i
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
end
@ -354,8 +378,10 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide @@ -354,8 +378,10 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide
nb["nbformat"] = JUPYTER_VERSION.major
nb["nbformat_minor"] = JUPYTER_VERSION.minor
# parse
chunks = parse(content; allow_continued = false)
## create the notebook cells
chunks = parse(content)
cells = []
for chunk in chunks
cell = Dict()

14
test/runtests.jl

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

Loading…
Cancel
Save