diff --git a/docs/src/fileformat.md b/docs/src/fileformat.md index db82522f..2892c852 100644 --- a/docs/src/fileformat.md +++ b/docs/src/fileformat.md @@ -57,10 +57,20 @@ certain lines: - `#src `: line exclusive to the source code and thus filtered out unconditionally. Lines *starting* or *ending* with one of these tokens are filtered out in the -[preprocessing step](@ref Pre-processing). +[preprocessing step](@ref Pre-processing). In addition, for markdown output, lines +ending with `#hide` are filtered out similar to Documenter.jl. + + +!!! note "Difference between `#src` and `#hide`" + `#src` and `#hide` are quite similar. The only difference is that `#src` lines + are filtered out *before* execution (if `execute=true`) and `#hide` lines + are filtered out *after* execution. + +!!! compat "Literate 2.6" + The `#hide` token requires at least Literate version 2.6. !!! compat "Literate 2.3" - Filter tokens at the end of the line requires at least Literate version 2.3. + Filter tokens at the *end of the line* requires at least Literate version 2.3. !!! tip The tokens can also be negated, for example a line starting with `#!nb` would diff --git a/src/Literate.jl b/src/Literate.jl index 516ebd2b..29bd1b0d 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -421,20 +421,26 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...) write(iomd, line.second, '\n') # skip indent here end else # isa(chunk, CodeChunk) + iocode = IOBuffer() codefence = config["codefence"]::Pair - write(iomd, codefence.first) + write(iocode, codefence.first) # make sure the code block is finalized if we are printing to ```@example if chunk.continued && startswith(codefence.first, "```@example") && config["documenter"]::Bool - write(iomd, "; continued = true") + write(iocode, "; continued = true") end - write(iomd, '\n') + write(iocode, '\n') for line in chunk.lines - write(iomd, line, '\n') + # filter out trailing #hide (unless leaving it for Documenter) + if !(endswith(line, "#hide") && !(config["documenter"]::Bool)) + write(iocode, line, '\n') + end end if config["documenter"]::Bool && REPL.ends_with_semicolon(chunk.lines[end]) - write(iomd, "nothing #hide\n") + write(iocode, "nothing #hide\n") end - write(iomd, codefence.second, '\n') + write(iocode, codefence.second, '\n') + write_code = !(all(l -> endswith(l, "#hide"), chunk.lines) && !(config["documenter"]::Bool)) + write_code && write(iomd, seekstart(iocode)) if config["execute"]::Bool execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir) end diff --git a/test/runtests.jl b/test/runtests.jl index 7553606f..b0547368 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -236,6 +236,14 @@ content = """ # Semicolon output supression 1 + 1; + # Completely hidden + hidden = 12 #hide + hidden * hidden #hide + + # Partially hidden + hidden2 = 12 #hide + hidden2 * hidden2 + #nb # A notebook cell with special metadata #nb %% Meta1 {"meta": "data"} #nb 1+1 @@ -319,6 +327,12 @@ const GITLAB_ENV = Dict( 1 + 1; + hidden = 12 #hide + hidden * hidden #hide + + hidden2 = 12 #hide + hidden2 * hidden2 + # This file was generated using Literate.jl, https://github.com/fredrikekre/Literate.jl """ @@ -528,6 +542,20 @@ end end nothing #hide ``` + Completely hidden + + ```@example inputfile + hidden = 12 #hide + hidden * hidden #hide + ``` + + Partially hidden + + ```@example inputfile + hidden2 = 12 #hide + hidden2 * hidden2 + ``` + --- *This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).* @@ -632,6 +660,7 @@ end end @test !occursin("```@example", markdown) @test !occursin("continued = true", markdown) @test !occursin("EditURL", markdown) + @test !occursin("#hide", markdown) # codefence Literate.markdown(inputfile, outdir, codefence = "```c" => "```")