Skip to content

Commit

Permalink
Implement #hide EOL token to filter lines after execution in markdown…
Browse files Browse the repository at this point in the history
…(...).
  • Loading branch information
fredrikekre committed Aug 14, 2020
1 parent e08ca0a commit 6d1aec9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
14 changes: 12 additions & 2 deletions docs/src/fileformat.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions src/Literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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).*
Expand Down Expand Up @@ -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" => "```")
Expand Down

0 comments on commit 6d1aec9

Please sign in to comment.