Browse Source

Support multiline comments for markdown input, fixes #26. (#123)

pull/132/head
Fredrik Ekre 5 years ago committed by GitHub
parent
commit
dc409d0f43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      docs/src/fileformat.md
  2. 9
      src/Literate.jl
  3. 23
      test/runtests.jl

25
docs/src/fileformat.md

@ -45,6 +45,31 @@ For simple use this is all you need to know. The following additional special sy
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).
!!! compat "Multiline comments in Literate 2.7"
Literate version 2.7 adds support for Julia multiline comments for markdown input.
All multiline comments in the input are rewritten to regular comments as part of the
preprocessing step, before any other processing is performed. For Literate to recognize
multiline comments it is required that the start token (`#=`) and end token (`=#`) are
placed on their own lines. Note also that it is allowed to have more than one `=` in the
tokens, for example
```juila
#=
This multiline comment
is treated as markdown.
=#
#=====================
This is also markdown.
=====================#
```
is rewritten to
```juila
# This multiline comment
# is treated as markdown.
# This is also markdown.
```
## [**2.2.** Filtering Lines](@id Filtering-Lines) ## [**2.2.** Filtering Lines](@id Filtering-Lines)

9
src/Literate.jl

@ -146,6 +146,15 @@ function replace_default(content, sym;
push!(repls, "\r\n" => "\n") # normalize line endings push!(repls, "\r\n" => "\n") # normalize line endings
# unconditionally rewrite multiline comments to regular comments
multiline_r = r"^#=+$\R^(\X*?)\R^=+#$"m
while (m = match(multiline_r, content); m !== nothing)
newlines = sprint() do io
foreach(l -> println(io, "# ", l), eachline(IOBuffer(m[1])))
end
content = replace(content, multiline_r => chop(newlines); count=1)
end
# unconditionally remove #src lines # unconditionally remove #src lines
push!(repls, r"^#src.*\n?"m => "") # remove leading #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

23
test/runtests.jl

@ -255,6 +255,15 @@ content = """
#nb #- #nb #-
#nb # %% [markdown] {"meta": "data"} #nb # %% [markdown] {"meta": "data"}
#nb # # Explicit markdown cell with metadata #nb # # Explicit markdown cell with metadata
#=
First multiline
comment
=#
#=======================
Second multiline comment
=======================#
""" """
const TRAVIS_ENV = Dict( const TRAVIS_ENV = Dict(
@ -424,6 +433,8 @@ const GITLAB_ENV = Dict(
@test occursin("# # Example", script) @test occursin("# # Example", script)
@test occursin("# foo, bar", script) @test occursin("# foo, bar", script)
@test occursin("# \\int f(x) dx", script) @test occursin("# \\int f(x) dx", script)
@test occursin("# First multiline", script)
@test occursin("# Second multiline comment", script)
# verify that inputfile exists # verify that inputfile exists
@test_throws ArgumentError Literate.script("nonexistent.jl", outdir) @test_throws ArgumentError Literate.script("nonexistent.jl", outdir)
@ -556,6 +567,11 @@ end end
hidden2 * hidden2 hidden2 * hidden2
``` ```
First multiline
comment
Second multiline comment
--- ---
*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).* *This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*
@ -877,6 +893,13 @@ end end
} }
""", """,
"""
"source": [
"First multiline\\n",
"comment"
]
""",
""" """
"source": [ "source": [
"---\\n", "---\\n",

Loading…
Cancel
Save