Browse Source

Require #+ for continuing a code block, fixes #41.

pull/43/head
Fredrik Ekre 6 years ago
parent
commit
36e8c21047
  1. 2
      docs/src/fileformat.md
  2. 4
      docs/src/pipeline.md
  3. 21
      src/Literate.jl
  4. 11
      test/runtests.jl

2
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: 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), - `#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 There is also some default convenience replacements that will always be performed, see
[Default Replacements](@ref). [Default Replacements](@ref).

4
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. 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. `#-------------` 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) ## [**3.3.** Document generation](@id Document-generation)

21
src/Literate.jl

@ -16,6 +16,7 @@ import .Documenter
# * Lines starting with `#md` are filtered out unless creating a markdown file # * 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 `#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, `#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 # * 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
@ -41,10 +42,15 @@ function parse(content; allow_continued = true)
for line in lines for line in lines
line = rstrip(line) line = rstrip(line)
# print("line = `$line`: ")
if occursin(r"^\h*#-", line) # new chunk if occursin(r"^\h*#-", line) # new chunk
# assume same as last chunk, will be cleaned up otherwise # assume same as last chunk, will be cleaned up otherwise
push!(chunks, typeof(chunks[end])()) 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 elseif ismdline(line) # markdown
if !(chunks[end] isa MDChunk) if !(chunks[end] isa MDChunk)
push!(chunks, MDChunk()) push!(chunks, MDChunk())
@ -78,19 +84,6 @@ function parse(content; allow_continued = true)
end end
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 we don't allow continued code blocks we need to merge MDChunks into the CodeChunks
if !allow_continued if !allow_continued
merged_chunks = Chunk[] merged_chunks = Chunk[]

11
test/runtests.jl

@ -53,25 +53,28 @@ end
#- #-
Line 26 Line 26
Line 27 Line 27
#- #+
Line 29 Line 29
#- #-
Line 31 Line 31
Line 32 Line 32
# Line 33 # Line 33
#+
Line 34 Line 34
#- #-
Line 36 Line 36
#- #+
Line 38 Line 38
#- #+
Line 40 Line 40
#- #-
Line 42 Line 42
Line 43 Line 43
# Line 44 # Line 44
#+
Line 45 Line 45
# Line 46 # Line 46
#+
Line 47 Line 47
# Line 48 # Line 48
#Line 49 #Line 49
@ -174,6 +177,7 @@ content = """
for i in 1:10 for i in 1:10
print(i) print(i)
# some markdown in a code block # some markdown in a code block
#+
end end
# name: @__NAME__ # name: @__NAME__
# Link to repo root: @__REPO_ROOT_URL__ # Link to repo root: @__REPO_ROOT_URL__
@ -195,6 +199,7 @@ content = """
# Indented markdown # Indented markdown
for i in 1:10 for i in 1:10
# Indented markdown # Indented markdown
#+
## Indented comment ## Indented comment
end end
""" """

Loading…
Cancel
Save