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/), @@ -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

16
src/Literate.jl

@ -529,19 +529,19 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg @@ -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')
write_line(line) && write(iocode, line, '\n')
end
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"],

12
test/runtests.jl

@ -817,6 +817,11 @@ end end @@ -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 @@ -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)

Loading…
Cancel
Save