Browse Source

Rework a bit and add tests.

pull/150/head
Fredrik Ekre 5 years ago
parent
commit
3efeb3cabc
  1. 16
      src/Literate.jl
  2. 5
      test/runtests.jl

16
src/Literate.jl

@ -458,7 +458,7 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
write_code = !(all(l -> endswith(l, "#hide"), chunk.lines) && !(config["documenter"]::Bool)) write_code = !(all(l -> endswith(l, "#hide"), chunk.lines) && !(config["documenter"]::Bool))
write_code && write(iomd, seekstart(iocode)) write_code && write(iomd, seekstart(iocode))
if config["execute"]::Bool if config["execute"]::Bool
execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir; inputfile=inputfile) execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir; inputfile=config["literate_inputfile"])
end end
end end
write(iomd, '\n') # add a newline between each chunk write(iomd, '\n') # add a newline between each chunk
@ -472,7 +472,7 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
return outputfile return outputfile
end end
function execute_markdown!(io::IO, sb::Module, block::String, outputdir; inputfile::String="") function execute_markdown!(io::IO, sb::Module, block::String, outputdir; inputfile::String="<unknown>")
# TODO: Deal with explicit display(...) calls # TODO: Deal with explicit display(...) calls
r, str, _ = execute_block(sb, block; inputfile=inputfile) r, str, _ = execute_block(sb, block; inputfile=inputfile)
plain_fence = "\n```\n" => "\n```" # issue #101: consecutive codefenced blocks need newline plain_fence = "\n```\n" => "\n```" # issue #101: consecutive codefenced blocks need newline
@ -537,14 +537,14 @@ function notebook(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:nb) preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:nb)
# create the notebook # create the notebook
nb = jupyter_notebook(chunks, config; inputfile=inputfile) nb = jupyter_notebook(chunks, config)
# write to file # write to file
outputfile = write_result(nb, config; print = (io, c)->JSON.print(io, c, 1)) outputfile = write_result(nb, config; print = (io, c)->JSON.print(io, c, 1))
return outputfile return outputfile
end end
function jupyter_notebook(chunks, config; inputfile::String="") function jupyter_notebook(chunks, config)
nb = Dict() nb = Dict()
nb["nbformat"] = JUPYTER_VERSION.major nb["nbformat"] = JUPYTER_VERSION.major
nb["nbformat_minor"] = JUPYTER_VERSION.minor nb["nbformat_minor"] = JUPYTER_VERSION.minor
@ -601,7 +601,7 @@ function jupyter_notebook(chunks, config; inputfile::String="")
@info "executing notebook `$(config["name"] * ".ipynb")`" @info "executing notebook `$(config["name"] * ".ipynb")`"
try try
cd(config["literate_outputdir"]) do cd(config["literate_outputdir"]) do
nb = execute_notebook(nb; inputfile=inputfile) nb = execute_notebook(nb; inputfile=config["literate_inputfile"])
end end
catch err catch err
@error "error when executing notebook based on input file: " * @error "error when executing notebook based on input file: " *
@ -612,7 +612,7 @@ function jupyter_notebook(chunks, config; inputfile::String="")
return nb return nb
end end
function execute_notebook(nb; inputfile::String="") function execute_notebook(nb; inputfile::String="<unknown>")
sb = sandbox() sb = sandbox()
execution_count = 0 execution_count = 0
for cell in nb["cells"] for cell in nb["cells"]
@ -702,7 +702,7 @@ function Base.display(ld::LiterateDisplay, mime::MIME, x)
end end
# Execute a code-block in a module and capture stdout/stderr and the result # Execute a code-block in a module and capture stdout/stderr and the result
function execute_block(sb::Module, block::String; inputfile::String="") function execute_block(sb::Module, block::String; inputfile::String="<unknown>")
@debug """execute_block($sb, block) @debug """execute_block($sb, block)
``` ```
$(block) $(block)
@ -724,7 +724,7 @@ function execute_block(sb::Module, block::String; inputfile::String="")
if c.error if c.error
error(""" error("""
$(sprint(showerror, c.value)) $(sprint(showerror, c.value))
when executing the following code block in file $(repr(inputfile)) when executing the following code block in file `$(Base.contractuser(inputfile))`
```julia ```julia
$block $block

5
test/runtests.jl

@ -1038,14 +1038,15 @@ end end
# test error when executing notebook # test error when executing notebook
write(inputfile, "for i in 1:10\n println(i)") write(inputfile, "for i in 1:10\n println(i)")
r = @test_logs((:error, r"error when executing"), match_mode=:any, r = @test_logs((:error, r"error when executing notebook based on input file: "), match_mode=:any,
try try
Literate.notebook(inputfile, outdir) Literate.notebook(inputfile, outdir)
catch err catch err
err err
end) end)
@test isa(r, ErrorException) @test isa(r, ErrorException)
@test occursin("when executing the following code block", r.msg) @test occursin("when executing the following code block in file ", r.msg)
@test occursin(inputfile, r.msg)
# verify that inputfile exists # verify that inputfile exists
@test_throws ArgumentError Literate.notebook("nonexistent.jl", outdir) @test_throws ArgumentError Literate.notebook("nonexistent.jl", outdir)

Loading…
Cancel
Save