Browse Source

Hide code lines with `#hide` when executing Markdown, fixes #166 (#188)

pull/190/head
David Widmann 4 years ago committed by GitHub
parent
commit
a7f40596cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      CHANGELOG.md
  2. 16
      src/Literate.jl
  3. 12
      test/runtests.jl

3
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). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [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 ## [2.12.1] - 2022-02-10
### Fixed ### Fixed

16
src/Literate.jl

@ -529,19 +529,19 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
write(iocode, "; continued = true") write(iocode, "; continued = true")
end end
write(iocode, '\n') 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 for line in chunk.lines
# filter out trailing #hide (unless leaving it for Documenter) write_line(line) && write(iocode, line, '\n')
if !(endswith(line, "#hide") && !isdocumenter(config))
write(iocode, line, '\n')
end end
end if write_hide && REPL.ends_with_semicolon(chunk.lines[end])
if isdocumenter(config) && 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)) any(write_line, chunk.lines) && write(iomd, seekstart(iocode))
write_code && write(iomd, seekstart(iocode)) if execute
if config["execute"]::Bool
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"],

12
test/runtests.jl

@ -817,6 +817,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)
@ -830,9 +835,16 @@ end end
@test occursin("```\nPlain\n```", markdown) # text/plain, fredrikekre/Literate#187 @test occursin("```\nPlain\n```", markdown) # text/plain, fredrikekre/Literate#187
@test occursin("```\nhello, world\n```", markdown) # stdout/stderr @test occursin("```\nhello, world\n```", markdown) # stdout/stderr
@test occursin("```\n42\n```", markdown) # result over 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("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