Browse Source

Implement #hide EOL token to filter lines after execution in markdown(...).

pull/122/head
Fredrik Ekre 5 years ago
parent
commit
6d1aec90b1
  1. 14
      docs/src/fileformat.md
  2. 18
      src/Literate.jl
  3. 29
      test/runtests.jl

14
docs/src/fileformat.md

@ -57,10 +57,20 @@ certain lines:
- `#src `: line exclusive to the source code and thus filtered out unconditionally. - `#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 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" !!! 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 !!! tip
The tokens can also be negated, for example a line starting with `#!nb` would The tokens can also be negated, for example a line starting with `#!nb` would

18
src/Literate.jl

@ -421,20 +421,26 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
write(iomd, line.second, '\n') # skip indent here write(iomd, line.second, '\n') # skip indent here
end end
else # isa(chunk, CodeChunk) else # isa(chunk, CodeChunk)
iocode = IOBuffer()
codefence = config["codefence"]::Pair 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 # make sure the code block is finalized if we are printing to ```@example
if chunk.continued && startswith(codefence.first, "```@example") && config["documenter"]::Bool if chunk.continued && startswith(codefence.first, "```@example") && config["documenter"]::Bool
write(iomd, "; continued = true") write(iocode, "; continued = true")
end end
write(iomd, '\n') write(iocode, '\n')
for line in chunk.lines 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 end
if config["documenter"]::Bool && REPL.ends_with_semicolon(chunk.lines[end]) if config["documenter"]::Bool && REPL.ends_with_semicolon(chunk.lines[end])
write(iomd, "nothing #hide\n") write(iocode, "nothing #hide\n")
end 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 if config["execute"]::Bool
execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir) execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir)
end end

29
test/runtests.jl

@ -236,6 +236,14 @@ content = """
# Semicolon output supression # Semicolon output supression
1 + 1; 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 # A notebook cell with special metadata
#nb %% Meta1 {"meta": "data"} #nb %% Meta1 {"meta": "data"}
#nb 1+1 #nb 1+1
@ -319,6 +327,12 @@ const GITLAB_ENV = Dict(
1 + 1; 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 # This file was generated using Literate.jl, https://github.com/fredrikekre/Literate.jl
""" """
@ -528,6 +542,20 @@ end end
nothing #hide 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).* *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("```@example", markdown)
@test !occursin("continued = true", markdown) @test !occursin("continued = true", markdown)
@test !occursin("EditURL", markdown) @test !occursin("EditURL", markdown)
@test !occursin("#hide", markdown)
# codefence # codefence
Literate.markdown(inputfile, outdir, codefence = "```c" => "```") Literate.markdown(inputfile, outdir, codefence = "```c" => "```")

Loading…
Cancel
Save