Browse Source

Hide code lines with `#hide` when executing Markdown

pull/188/head
David Widmann 4 years ago
parent
commit
ad8fb31765
  1. 9
      src/Literate.jl
  2. 11
      test/runtests.jl

9
src/Literate.jl

@ -522,6 +522,7 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
else # isa(chunk, CodeChunk) else # isa(chunk, CodeChunk)
iocode = IOBuffer() iocode = IOBuffer()
codefence = config["codefence"]::Pair codefence = config["codefence"]::Pair
execute = config["execute"]::Bool
write(iocode, codefence.first) write(iocode, codefence.first)
# make sure the code block is finalized if we are printing to ```@example # make sure the code block is finalized if we are printing to ```@example
# (or ````@example, any number of backticks >= 3 works) # (or ````@example, any number of backticks >= 3 works)
@ -531,17 +532,17 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
write(iocode, '\n') write(iocode, '\n')
for line in chunk.lines for line in chunk.lines
# filter out trailing #hide (unless leaving it for Documenter) # 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') write(iocode, line, '\n')
end end
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") write(iocode, "nothing #hide\n")
end end
write(iocode, codefence.second, '\n') 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)) write_code && write(iomd, seekstart(iocode))
if config["execute"]::Bool if execute
execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir; execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir;
inputfile=config["literate_inputfile"], inputfile=config["literate_inputfile"],
flavor=config["flavor"], flavor=config["flavor"],

11
test/runtests.jl

@ -810,6 +810,11 @@ end end
#- #-
print("hello there") print("hello there")
nothing nothing
#-
a = 2 + 2
print("a: ", a); nothing #hide
#-
47 #hide
""") """)
Literate.markdown(inputfile, outdir; execute=true) Literate.markdown(inputfile, outdir; execute=true)
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)
@ -825,6 +830,12 @@ end end
@test !occursin("246", markdown) # empty output because trailing ; @test !occursin("246", markdown) # empty output because trailing ;
@test !occursin("```\nnothing\n```", markdown) # empty output because nothing as return value @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("```\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 # FranklinFlavor
Literate.markdown(inputfile, outdir; execute=true, flavor=Literate.FranklinFlavor()) Literate.markdown(inputfile, outdir; execute=true, flavor=Literate.FranklinFlavor())
markdown = read(joinpath(outdir, "inputfile.md"), String) markdown = read(joinpath(outdir, "inputfile.md"), String)

Loading…
Cancel
Save