From 2f172cb144a4fb6b8728ef777838bc5b78a1b196 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Tue, 3 Mar 2020 01:06:12 +0100 Subject: [PATCH] cleanup and tests --- docs/src/fileformat.md | 2 +- src/Literate.jl | 50 +++++++++++++++--------------------------- test/runtests.jl | 27 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/docs/src/fileformat.md b/docs/src/fileformat.md index 025da39..611e26c 100644 --- a/docs/src/fileformat.md +++ b/docs/src/fileformat.md @@ -56,7 +56,7 @@ certain lines: - `#jl `: line exclusive to script output, - `#src `: line exclusive to the source code and thus filtered out unconditionally. -Lines *starting* 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). !!! tip diff --git a/src/Literate.jl b/src/Literate.jl index f30263b..94e3060 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -19,10 +19,11 @@ import .Documenter # * The file is parsed in "chunks" of code and markdown. A new chunk is created when the # lines switch context from markdown to code and vice versa. # * Lines starting with `#-` can be used to start a new chunk. -# * Lines starting with `#md` are filtered out unless creating a markdown file -# * Lines starting with `#nb` are filtered out unless creating a notebook -# * Lines starting with, or ending with, `#jl` are filtered out unless creating a script file -# * Lines starting with, or ending with, `#src` are filtered out unconditionally +# * Lines starting/ending with `#md` are filtered out unless creating a markdown file +# * Lines starting/ending with `#nb` are filtered out unless creating a notebook +# * Lines starting/ending with, `#jl` are filtered out unless creating a script file +# * Lines starting/ending with, `#src` are filtered out unconditionally +# * #md, #nb, and #jl can be negated as #!md, #!nb, and #!jl # * Whitespace within a chunk is preserved # * Empty chunks are removed, leading and trailing empty lines in a chunk are also removed @@ -146,40 +147,25 @@ function replace_default(content, sym; push!(repls, "\r\n" => "\n") # normalize line endings # unconditionally remove #src lines - push!(repls, r"^#src.*\n?"m => "") # remove leadig #src lines + push!(repls, r"^#src.*\n?"m => "") # remove leading #src lines push!(repls, r".*#src$\n?"m => "") # remove trailing #src lines if sym === :md - push!(repls, r"^#md "m => "") # remove leading #md - push!(repls, r"^#!md.*\n?"m => "") # remove leading #!md lines - push!(repls, r".*#!md$\n?"m => "") # remove trailing #!md lines - push!(repls, r"^#nb.*\n?"m => "") # remove leading #nb lines - push!(repls, r".*#nb$\n?"m => "") # remove trailing #nb lines - push!(repls, r"^#!nb "m => "") # remove leading #!nb - push!(repls, r"^#jl.*\n?"m => "") # remove leading #jl lines - push!(repls, r".*#jl$\n?"m => "") # remove trailing #jl lines - push!(repls, r"^#!jl "m => "") # remove leading #!jl + push!(repls, r"^#(md|!nb|!jl) "m => "") # remove leading #md, #!nb, and #!jl + push!(repls, r" #(md|!nb|!jl)$"m => "") # remove trailing #md, #!nb, and #!jl + push!(repls, r"^#(!md|nb|jl).*\n?"m => "") # remove leading #!md, #nb and #jl lines + push!(repls, r".*#(!md|nb|jl)$\n?"m => "") # remove trailing #!md, #nb, and #jl lines elseif sym === :nb - push!(repls, r"^#md.*\n?"m => "") # remove leading #md lines - push!(repls, r".*#md$\n?"m => "") # remove trailing #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 leading #!nb lines - push!(repls, r".*#!nb$\n?"m => "") # remove trailing #!nb lines - push!(repls, r"^#jl.*\n?"m => "") # remove leading #jl lines - push!(repls, r".*#jl$\n?"m => "") # remove trailing #jl lines - push!(repls, r"^#!jl "m => "") # remove leading #!jl + push!(repls, r"^#(!md|nb|!jl) "m => "") # remove leading #!md, #nb, and #!jl + push!(repls, r" #(!md|nb|!jl)$"m => "") # remove trailing #!md, #nb, and #!jl + push!(repls, r"^#(md|!nb|jl).*\n?"m => "") # remove leading #md, #!nb and #jl lines + push!(repls, r".*#(md|!nb|jl)$\n?"m => "") # remove trailing #md, #!nb, and #jl lines push!(repls, r"```math(.*?)```"s => s"$$\1$$") else # sym === :jl - push!(repls, r"^#md.*\n?"m => "") # remove leading #md lines - push!(repls, r".*#md$\n?"m => "") # remove trailing #md lines - push!(repls, r"^#!md "m => "") # remove leading #!md - push!(repls, r"^#nb.*\n?"m => "") # remove leading #nb lines - push!(repls, r".*#nb$\n?"m => "") # remove trailing #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 leading #!jl lines - push!(repls, r".*#!jl$\n?"m => "") # remove trailing #!jl lines + push!(repls, r"^#(!md|!nb|jl) "m => "") # remove leading #!md, #!nb, and #jl + push!(repls, r" #(!md|!nb|jl)$"m => "") # remove trailing #!md, #!nb, and #jl + push!(repls, r"^#(md|nb|!jl).*\n?"m => "") # remove leading #md, #nb and #!jl lines + push!(repls, r".*#(md|nb|!jl)$\n?"m => "") # remove trailing #md, #nb, and #!jl lines end # name diff --git a/test/runtests.jl b/test/runtests.jl index d5b3369..f5ca7b9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -174,17 +174,29 @@ content = """ # [foo](@ref), [bar](@ref bbaarr) x = 1 #md # Only markdown + # Only markdown #md #md x + 1 + x + 1 #md #!md # Not markdown + # Not markdown #!md #!md x * 1 + x * 1 #!md #nb # Only notebook + # Only notebook #nb #nb x + 2 + x + 2 #nb #!nb # Not notebook + # Not notebook #!nb #!nb x * 2 + x * 2 #!nb #jl # Only script + # Only script #jl #jl x + 3 + x + 3 #jl #!jl # Not script + # Not script #!jl #!jl x * 3 + x * 3 #!jl #src # Source code only Source code only #src ## # Comment @@ -276,10 +288,13 @@ const GITLAB_ENV = Dict( expected_script = """ x = 1 + x * 1 x * 1 + x * 2 x * 2 + x + 3 x + 3 # # Comment # another comment @@ -426,22 +441,28 @@ end end x = 1 ``` + Only markdown Only markdown ```@example inputfile x + 1 + x + 1 ``` + Not notebook Not notebook ```@example inputfile x * 2 + x * 2 ``` + Not script Not script ```@example inputfile x * 3 + x * 3 # # Comment # another comment ``` @@ -661,36 +682,42 @@ end end """ "source": [ + "Not markdown\\n", "Not markdown" ], """, """ "source": [ + "x * 1\\n", "x * 1" ], """, """ "source": [ + "Only notebook\\n", "Only notebook" ] """, """ "source": [ + "x + 2\\n", "x + 2" ] """, """ "source": [ + "Not script\\n", "Not script" ], """, """ "source": [ + "x * 3\\n", "x * 3\\n", "# # Comment\\n", "# another comment"