From a7f40596cf0d03b54764a73f693543264d016b7f Mon Sep 17 00:00:00 2001 From: David Widmann Date: Sat, 19 Feb 2022 11:23:14 +0100 Subject: [PATCH] Hide code lines with `#hide` when executing Markdown, fixes #166 (#188) --- CHANGELOG.md | 3 +++ src/Literate.jl | 16 ++++++++-------- test/runtests.jl | 12 ++++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2580c8..e488fb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Lines with trailing `#hide` are not shown in output of Markdown execution with Documenter + flavour. ([#188][github-188]) ## [2.12.1] - 2022-02-10 ### Fixed diff --git a/src/Literate.jl b/src/Literate.jl index 8142081..91bfa44 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -529,19 +529,19 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg write(iocode, "; continued = true") end write(iocode, '\n') + # filter out trailing #hide unless code is executed by Documenter + execute = config["execute"]::Bool + write_hide = isdocumenter(config) && !execute + write_line(line) = write_hide || !endswith(line, "#hide") for line in chunk.lines - # filter out trailing #hide (unless leaving it for Documenter) - if !(endswith(line, "#hide") && !isdocumenter(config)) - write(iocode, line, '\n') - end + write_line(line) && write(iocode, line, '\n') end - if isdocumenter(config) && REPL.ends_with_semicolon(chunk.lines[end]) + if write_hide && 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 && write(iomd, seekstart(iocode)) - if config["execute"]::Bool + any(write_line, chunk.lines) && write(iomd, seekstart(iocode)) + 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 fbd7fc1..7784c13 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -817,6 +817,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) @@ -830,9 +835,16 @@ end end @test occursin("```\nPlain\n```", markdown) # text/plain, fredrikekre/Literate#187 @test occursin("```\nhello, world\n```", markdown) # stdout/stderr @test occursin("```\n42\n```", markdown) # result over stdout/stderr + @test occursin("```julia\n123+123;\n```", markdown) # no additional `nothing #hide`, fredrikekre/Literate.jl/issues/166#issuecomment-979987878 @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)