Skip to content

Commit

Permalink
Implement negation of filter tokens, fixes #59.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jul 19, 2019
1 parent 36e8c21 commit 1d02868
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/src/fileformat.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ certain lines:
Lines *starting* with one of these tokens are filtered out in the
[preprocessing step](@ref Pre-processing).

!!! tip
The tokens can also be negated, for example a line starting with `#!nb` would
be included in markdown and script output, but filtered out for notebook output.

Suppose, for example, that we want to include a docstring within a `@docs` block
using Documenter. Obviously we don't want to include this in the notebook,
since `@docs` is Documenter syntax that the notebook will not understand. This
Expand Down
27 changes: 18 additions & 9 deletions src/Literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,27 @@ function replace_default(content, sym;
push!(repls, r".*#src$\n?"m => "")

if sym === :md
push!(repls, r"^#nb.*\n?"m => "") # remove #nb lines
push!(repls, r"^#jl.*\n?"m => "") # remove leading #jl lines
push!(repls, r"^#md "m => "") # remove leading #md
push!(repls, r"^#md "m => "") # remove leading #md
push!(repls, r"^#!md.*\n?"m => "") # remove leading #!md lines
push!(repls, r"^#nb.*\n?"m => "") # remove #nb lines
push!(repls, r"^#!nb "m => "") # remove leading #!nb
push!(repls, r"^#jl.*\n?"m => "") # remove leading #jl lines
push!(repls, r"^#!jl "m => "") # remove leading #!jl
elseif sym === :nb
push!(repls, r"^#md.*\n?"m => "") # remove #md lines
push!(repls, r"^#jl.*\n?"m => "") # remove leading #jl lines
push!(repls, r"^#nb "m => "") # remove leading #nb
push!(repls, r"^#md.*\n?"m => "") # remove #md lines
push!(repls, r"^#!md "m => "") # remove leading #!md
push!(repls, r"^#nb "m => "") # remove leading #nb
push!(repls, r"^#!nb.*\n?"m => "") # remove #!nb lines
push!(repls, r"^#jl.*\n?"m => "") # remove leading #jl lines
push!(repls, r"^#!jl "m => "") # remove leading #!jl
push!(repls, r"```math(.*?)```"s => s"\\begin{equation}\1\\end{equation}")
else # sym === :jl
push!(repls, r"^#md.*\n?"m => "") # remove #md lines
push!(repls, r"^#nb.*\n?"m => "") # remove #nb lines
push!(repls, r"^#jl "m => "") # remove leading #jl
push!(repls, r"^#md.*\n?"m => "") # remove #md lines
push!(repls, r"^#!md "m => "") # remove leading #!md
push!(repls, r"^#nb.*\n?"m => "") # remove #nb lines
push!(repls, r"^#!nb "m => "") # remove leading #!nb
push!(repls, r"^#jl "m => "") # remove leading #jl
push!(repls, r"^#!jl.*\n?"m => "") # remove #!jl lines
end

# name
Expand Down
50 changes: 48 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,16 @@ content = """
x = 1
#md # Only markdown
#md x + 1
#!md # Not markdown
#!md x * 1
#nb # Only notebook
#nb x + 2
#!nb # Not notebook
#!nb x * 2
#jl # Only script
#jl x + 3
#!jl # Not script
#!jl x * 3
#src # Source code only
Source code only #src
## # Comment
Expand Down Expand Up @@ -221,6 +227,10 @@ content = """
expected_script = """
x = 1
x * 1
x * 2
x + 3
# # Comment
# another comment
Expand Down Expand Up @@ -319,6 +329,18 @@ end
```@example inputfile
x + 1
```
Not notebook
```@example inputfile
x * 2
```
Not script
```@example inputfile
x * 3
# # Comment
# another comment
```
Expand Down Expand Up @@ -461,6 +483,18 @@ end
]
""",

"""
"source": [
"Not markdown"
],
""",

"""
"source": [
"x * 1"
],
""",

"""
"source": [
"Only notebook"
Expand All @@ -469,10 +503,22 @@ end

"""
"source": [
"x + 2\\n",
"x + 2"
]
""",

"""
"source": [
"Not script"
],
""",

"""
"source": [
"x * 3\\n",
"# # Comment\\n",
"# another comment"
]
],
""",

"""
Expand Down

0 comments on commit 1d02868

Please sign in to comment.