diff --git a/docs/src/fileformat.md b/docs/src/fileformat.md index d70b727f..ce28a3ef 100644 --- a/docs/src/fileformat.md +++ b/docs/src/fileformat.md @@ -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 diff --git a/src/Literate.jl b/src/Literate.jl index c7db5199..3f1d0aa2 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 044f4e32..8aff4926 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -221,6 +227,10 @@ content = """ expected_script = """ x = 1 + x * 1 + + x * 2 + x + 3 # # Comment # another comment @@ -319,6 +329,18 @@ end ```@example inputfile x + 1 + ``` + + Not notebook + + ```@example inputfile + x * 2 + ``` + + Not script + + ```@example inputfile + x * 3 # # Comment # another comment ``` @@ -461,6 +483,18 @@ end ] """, + """ + "source": [ + "Not markdown" + ], + """, + + """ + "source": [ + "x * 1" + ], + """, + """ "source": [ "Only notebook" @@ -469,10 +503,22 @@ end """ "source": [ - "x + 2\\n", + "x + 2" + ] + """, + + """ + "source": [ + "Not script" + ], + """, + + """ + "source": [ + "x * 3\\n", "# # Comment\\n", "# another comment" - ] + ], """, """