diff --git a/src/Literate.jl b/src/Literate.jl index 8142081..47e85c3 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -522,6 +522,7 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg else # isa(chunk, CodeChunk) iocode = IOBuffer() codefence = config["codefence"]::Pair + execute = config["execute"]::Bool write(iocode, codefence.first) # make sure the code block is finalized if we are printing to ```@example # (or ````@example, any number of backticks >= 3 works) @@ -531,17 +532,17 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg write(iocode, '\n') for line in chunk.lines # filter out trailing #hide (unless leaving it for Documenter) - if !(endswith(line, "#hide") && !isdocumenter(config)) + if !endswith(line, "#hide") || (isdocumenter(config) && !execute) write(iocode, line, '\n') end end - if isdocumenter(config) && REPL.ends_with_semicolon(chunk.lines[end]) + if isdocumenter(config) && !execute && REPL.ends_with_semicolon(chunk.lines[end]) write(iocode, "nothing #hide\n") end write(iocode, codefence.second, '\n') - write_code = !(all(l -> endswith(l, "#hide"), chunk.lines) && !isdocumenter(config)) + write_code = !all(l -> endswith(l, "#hide"), chunk.lines) || (isdocumenter(config) && !execute) write_code && write(iomd, seekstart(iocode)) - if config["execute"]::Bool + if execute execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir; inputfile=config["literate_inputfile"], flavor=config["flavor"], diff --git a/test/runtests.jl b/test/runtests.jl index 9d34427..a149f58 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -810,6 +810,11 @@ end end #- print("hello there") nothing + #- + a = 2 + 2 + print("a: ", a); nothing #hide + #- + 47 #hide """) Literate.markdown(inputfile, outdir; execute=true) markdown = read(joinpath(outdir, "inputfile.md"), String) @@ -825,6 +830,12 @@ end end @test !occursin("246", markdown) # empty output because trailing ; @test !occursin("```\nnothing\n```", markdown) # empty output because nothing as return value @test occursin("```\nhello there\n```", markdown) # nothing as return value, non-empty stdout + @test occursin("```julia\na = 2 + 2\n```", markdown) # line with `#hide` removed + @test occursin("```\na: 4\n```", markdown) # nothing as return value, non-empty stdout + @test !occursin("```julia\n47 #hide\n```", markdown) # line with `#hide` removed + @test !occursin("```julia\n```", markdown) # no empty code block + @test occursin("```\n47\n```", markdown) # return value (even though line/block removed) + # FranklinFlavor Literate.markdown(inputfile, outdir; execute=true, flavor=Literate.FranklinFlavor()) markdown = read(joinpath(outdir, "inputfile.md"), String)