Browse Source

cleanup and tests

pull/93/head
Fredrik Ekre 6 years ago
parent
commit
2f172cb144
  1. 2
      docs/src/fileformat.md
  2. 50
      src/Literate.jl
  3. 27
      test/runtests.jl

2
docs/src/fileformat.md

@ -56,7 +56,7 @@ certain lines:
- `#jl `: line exclusive to script output, - `#jl `: line exclusive to script output,
- `#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* 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).
!!! tip !!! tip

50
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 # * 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 switch context from markdown to code and vice versa.
# * Lines starting with `#-` can be used to start a new chunk. # * 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/ending with `#md` are filtered out unless creating a markdown file
# * Lines starting with `#nb` are filtered out unless creating a notebook # * Lines starting/ending 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/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, `#src` are filtered out unconditionally
# * #md, #nb, and #jl can be negated as #!md, #!nb, and #!jl
# * Whitespace within a chunk is preserved # * Whitespace within a chunk is preserved
# * Empty chunks are removed, leading and trailing empty lines in a chunk are also removed # * 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 push!(repls, "\r\n" => "\n") # normalize line endings
# unconditionally remove #src lines # 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 push!(repls, r".*#src$\n?"m => "") # remove trailing #src lines
if sym === :md if sym === :md
push!(repls, r"^#md "m => "") # remove leading #md push!(repls, r"^#(md|!nb|!jl) "m => "") # remove leading #md, #!nb, and #!jl
push!(repls, r"^#!md.*\n?"m => "") # remove leading #!md lines push!(repls, r" #(md|!nb|!jl)$"m => "") # remove trailing #md, #!nb, and #!jl
push!(repls, r".*#!md$\n?"m => "") # remove trailing #!md lines push!(repls, r"^#(!md|nb|jl).*\n?"m => "") # remove leading #!md, #nb and #jl lines
push!(repls, r"^#nb.*\n?"m => "") # remove leading #nb lines push!(repls, r".*#(!md|nb|jl)$\n?"m => "") # remove trailing #!md, #nb, and #jl 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
elseif sym === :nb elseif sym === :nb
push!(repls, r"^#md.*\n?"m => "") # remove leading #md lines push!(repls, r"^#(!md|nb|!jl) "m => "") # remove leading #!md, #nb, and #!jl
push!(repls, r".*#md$\n?"m => "") # remove trailing #md lines push!(repls, r" #(!md|nb|!jl)$"m => "") # remove trailing #!md, #nb, and #!jl
push!(repls, r"^#!md "m => "") # remove leading #!md push!(repls, r"^#(md|!nb|jl).*\n?"m => "") # remove leading #md, #!nb and #jl lines
push!(repls, r"^#nb "m => "") # remove leading #nb push!(repls, r".*#(md|!nb|jl)$\n?"m => "") # remove trailing #md, #!nb, and #jl lines
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"```math(.*?)```"s => s"$$\1$$") push!(repls, r"```math(.*?)```"s => s"$$\1$$")
else # sym === :jl else # sym === :jl
push!(repls, r"^#md.*\n?"m => "") # remove leading #md lines push!(repls, r"^#(!md|!nb|jl) "m => "") # remove leading #!md, #!nb, and #jl
push!(repls, r".*#md$\n?"m => "") # remove trailing #md lines push!(repls, r" #(!md|!nb|jl)$"m => "") # remove trailing #!md, #!nb, and #jl
push!(repls, r"^#!md "m => "") # remove leading #!md push!(repls, r"^#(md|nb|!jl).*\n?"m => "") # remove leading #md, #nb and #!jl lines
push!(repls, r"^#nb.*\n?"m => "") # remove leading #nb lines push!(repls, r".*#(md|nb|!jl)$\n?"m => "") # remove trailing #md, #nb, and #!jl 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
end end
# name # name

27
test/runtests.jl

@ -174,17 +174,29 @@ content = """
# [foo](@ref), [bar](@ref bbaarr) # [foo](@ref), [bar](@ref bbaarr)
x = 1 x = 1
#md # Only markdown #md # Only markdown
# Only markdown #md
#md x + 1 #md x + 1
x + 1 #md
#!md # Not markdown #!md # Not markdown
# Not markdown #!md
#!md x * 1 #!md x * 1
x * 1 #!md
#nb # Only notebook #nb # Only notebook
# Only notebook #nb
#nb x + 2 #nb x + 2
x + 2 #nb
#!nb # Not notebook #!nb # Not notebook
# Not notebook #!nb
#!nb x * 2 #!nb x * 2
x * 2 #!nb
#jl # Only script #jl # Only script
# Only script #jl
#jl x + 3 #jl x + 3
x + 3 #jl
#!jl # Not script #!jl # Not script
# Not script #!jl
#!jl x * 3 #!jl x * 3
x * 3 #!jl
#src # Source code only #src # Source code only
Source code only #src Source code only #src
## # Comment ## # Comment
@ -276,10 +288,13 @@ const GITLAB_ENV = Dict(
expected_script = """ expected_script = """
x = 1 x = 1
x * 1
x * 1 x * 1
x * 2
x * 2 x * 2
x + 3
x + 3 x + 3
# # Comment # # Comment
# another comment # another comment
@ -426,22 +441,28 @@ end end
x = 1 x = 1
``` ```
Only markdown
Only markdown Only markdown
```@example inputfile ```@example inputfile
x + 1 x + 1
x + 1
``` ```
Not notebook
Not notebook Not notebook
```@example inputfile ```@example inputfile
x * 2 x * 2
x * 2
``` ```
Not script
Not script Not script
```@example inputfile ```@example inputfile
x * 3 x * 3
x * 3
# # Comment # # Comment
# another comment # another comment
``` ```
@ -661,36 +682,42 @@ end end
""" """
"source": [ "source": [
"Not markdown\\n",
"Not markdown" "Not markdown"
], ],
""", """,
""" """
"source": [ "source": [
"x * 1\\n",
"x * 1" "x * 1"
], ],
""", """,
""" """
"source": [ "source": [
"Only notebook\\n",
"Only notebook" "Only notebook"
] ]
""", """,
""" """
"source": [ "source": [
"x + 2\\n",
"x + 2" "x + 2"
] ]
""", """,
""" """
"source": [ "source": [
"Not script\\n",
"Not script" "Not script"
], ],
""", """,
""" """
"source": [ "source": [
"x * 3\\n",
"x * 3\\n", "x * 3\\n",
"# # Comment\\n", "# # Comment\\n",
"# another comment" "# another comment"

Loading…
Cancel
Save