diff --git a/src/config.jl b/src/config.jl index 7bc112c0..11147bdb 100644 --- a/src/config.jl +++ b/src/config.jl @@ -34,7 +34,9 @@ const defaultParams = :fig_env=> nothing, :out_width=> nothing, :out_height=> nothing, - :skip=>false + :skip=>false, + :output_as_code=>false, + :output_eval=>true ) ) #This one can be changed at runtime, initially a copy of defaults diff --git a/src/run.jl b/src/run.jl index 77bc177d..87577ce8 100644 --- a/src/run.jl +++ b/src/run.jl @@ -289,7 +289,7 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module) #Run post_execute chunks for hook in postexecute_hooks - chunk = Compat.invokelatest(hook, chunk) + chunk = Compat.invokelatest(hook, chunk) end if chunk.options[:term] @@ -300,6 +300,13 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module) chunks = collect_results(chunk, ScriptResult()) end + if (chunk.options[:output_as_code]) + newchunk = deepcopy(chunk) + newchunk.options[:output_as_code] = false + newchunk.options[:eval] = chunk.options[:output_eval] + newchunk.content = prod([r.stdout for r in chunk.result]) + chunks = eval_chunk(newchunk, report, SandBox) + end #else # chunk.options[:fig] && (chunk.figures = copy(report.figures)) #end diff --git a/test/documents/codefile.jl b/test/documents/codefile.jl new file mode 100644 index 00000000..1dbc0a73 --- /dev/null +++ b/test/documents/codefile.jl @@ -0,0 +1,4 @@ +using InteractiveUtils +@code_native +(1.0, π) +using Test +@test 1 == 1 diff --git a/test/documents/jupyter_test.jmd b/test/documents/jupyter_test.jmd index ae0570a9..5b61a435 100644 --- a/test/documents/jupyter_test.jmd +++ b/test/documents/jupyter_test.jmd @@ -11,7 +11,8 @@ And here's some code Here's some more complicated code ```julia - @code_native +(1.0, π) - using Test - @test 1 == 1 +using InteractiveUtils +@code_native +(1.0, π) +using Test +@test 1 == 1 ``` diff --git a/test/documents/output_as_code.jmd b/test/documents/output_as_code.jmd new file mode 100644 index 00000000..177a3086 --- /dev/null +++ b/test/documents/output_as_code.jmd @@ -0,0 +1,17 @@ +Here's some text + +And here's some code + +```julia; output_as_code=true +print(" x = 1 + y = 2 + @show x + y +") +``` + +Here's some more complicated code + +```julia; output_as_code=true +txt = read(WEAVE_ARGS["filename"], String) +print(txt) +``` diff --git a/test/output_as_code.jl b/test/output_as_code.jl new file mode 100644 index 00000000..02478597 --- /dev/null +++ b/test/output_as_code.jl @@ -0,0 +1,14 @@ +dir = @__DIR__ +refjmd = joinpath(dir, "documents", "jupyter_test.jmd") +testjmd = joinpath(dir, "documents", "output_as_code.jmd") + + +Weave.weave(refjmd, doctype="pandoc") +refresult = read(joinpath(dir, "documents", "jupyter_test.md"), String) + +Weave.weave(testjmd, doctype="pandoc", + args=Dict("filename" => joinpath(dir, "documents", "codefile.jl"))) +result = read(joinpath(dir, "documents", "output_as_code.md"), String) + +@test result == refresult + diff --git a/test/runtests.jl b/test/runtests.jl index 752043b0..f0a036ae 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -59,6 +59,10 @@ using Test include("inline_test.jl") end + @testset "Output as code" begin + @info("Testing output as code") + include("output_as_code.jl") + end # @testset "Notebooks" begin # @info("Testing Jupyter options") # include("notebooks.jl")