From 36e8c210478a8be83ce0b2ce961ecd5c1abc8b45 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 19 Jul 2019 23:18:23 +0200 Subject: [PATCH] Require #+ for continuing a code block, fixes #41. --- docs/src/fileformat.md | 2 +- docs/src/pipeline.md | 4 ++++ src/Literate.jl | 21 +++++++-------------- test/runtests.jl | 11 ++++++++--- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/docs/src/fileformat.md b/docs/src/fileformat.md index fbc0885..d70b727 100644 --- a/docs/src/fileformat.md +++ b/docs/src/fileformat.md @@ -39,7 +39,7 @@ julia code. We note a couple of things: For simple use this is all you need to know. The following additional special syntax can also be used: - `#md`, `#nb`, `#jl`, `#src`: tags to filter lines, see [Filtering Lines](@ref Filtering-Lines), -- `#-`: tag to manually control chunk-splits, see [Custom control over chunk splits](@ref). +- `#-` (`#+`): tag to manually control chunk-splits, see [Custom control over chunk splits](@ref). There is also some default convenience replacements that will always be performed, see [Default Replacements](@ref). diff --git a/docs/src/pipeline.md b/docs/src/pipeline.md index 647b49f..267677b 100644 --- a/docs/src/pipeline.md +++ b/docs/src/pipeline.md @@ -106,6 +106,10 @@ The example above would result in two consecutive code-chunks. The rest of the line, after `#-`, is discarded, so it is possible to use e.g. `#-------------` as a chunk splitter, which may make the source code more readable. +It is also possible to use `#+` as a chunk splitter. The difference between `#+` and `#-` +is that `#+` enables Documenter's "continued"-blocks, see the +[Documenter manual](https://juliadocs.github.io/Documenter.jl/stable/). + ## [**3.3.** Document generation](@id Document-generation) diff --git a/src/Literate.jl b/src/Literate.jl index a5d46f3..c7db519 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -16,6 +16,7 @@ import .Documenter # * 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 # * Whitespace within a chunk is preserved # * Empty chunks are removed, leading and trailing empty lines in a chunk are also removed @@ -41,10 +42,15 @@ function parse(content; allow_continued = true) for line in lines line = rstrip(line) - # print("line = `$line`: ") if occursin(r"^\h*#-", line) # new chunk # assume same as last chunk, will be cleaned up otherwise push!(chunks, typeof(chunks[end])()) + elseif occursin(r"^\h*#\+", line) # new code chunk, that continues the previous one + idx = findlast(x -> isa(x, CodeChunk), chunks) + if idx !== nothing + chunks[idx].continued = true + end + push!(chunks, CodeChunk()) elseif ismdline(line) # markdown if !(chunks[end] isa MDChunk) push!(chunks, MDChunk()) @@ -78,19 +84,6 @@ function parse(content; allow_continued = true) end end - # find code chunks that are continued - last_code_chunk = 0 - for (i, chunk) in enumerate(chunks) - isa(chunk, MDChunk) && continue - if startswith(last(chunk.lines)," ") - chunk.continued = true - end - if startswith(first(chunk.lines)," ") - chunks[last_code_chunk].continued = true - end - last_code_chunk = i - end - # if we don't allow continued code blocks we need to merge MDChunks into the CodeChunks if !allow_continued merged_chunks = Chunk[] diff --git a/test/runtests.jl b/test/runtests.jl index e7a9889..044f4e3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -53,25 +53,28 @@ end #- Line 26 Line 27 - #- + #+ Line 29 #- Line 31 Line 32 # Line 33 + #+ Line 34 #- Line 36 - #- + #+ Line 38 - #- + #+ Line 40 #- Line 42 Line 43 # Line 44 + #+ Line 45 # Line 46 + #+ Line 47 # Line 48 #Line 49 @@ -174,6 +177,7 @@ content = """ for i in 1:10 print(i) # some markdown in a code block + #+ end # name: @__NAME__ # Link to repo root: @__REPO_ROOT_URL__ @@ -195,6 +199,7 @@ content = """ # Indented markdown for i in 1:10 # Indented markdown + #+ ## Indented comment end """