Skip to content

Commit

Permalink
Require #+ for continuing a code block, fixes #41.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jul 19, 2019
1 parent f63cc93 commit 36e8c21
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/src/fileformat.md
Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions docs/src/pipeline.md
Expand Up @@ -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)

Expand Down
21 changes: 7 additions & 14 deletions src/Literate.jl
Expand Up @@ -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

Expand All @@ -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())
Expand Down Expand Up @@ -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[]
Expand Down
11 changes: 8 additions & 3 deletions test/runtests.jl
Expand Up @@ -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
Expand Down Expand Up @@ -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__
Expand All @@ -195,6 +199,7 @@ content = """
# Indented markdown
for i in 1:10
# Indented markdown
#+
## Indented comment
end
"""
Expand Down

0 comments on commit 36e8c21

Please sign in to comment.