From f62d953da8069c3f8d67046a8e2879f2272ff882 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Sat, 12 Sep 2020 16:19:00 +0200 Subject: [PATCH] Support multiline comments for markdown input, fixes #26. --- docs/src/fileformat.md | 25 +++++++++++++++++++++++++ src/Literate.jl | 9 +++++++++ test/runtests.jl | 23 +++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/docs/src/fileformat.md b/docs/src/fileformat.md index 2892c85..d9eefa4 100644 --- a/docs/src/fileformat.md +++ b/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 [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) diff --git a/src/Literate.jl b/src/Literate.jl index 4d118d0..92cf041 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -146,6 +146,15 @@ function replace_default(content, sym; 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 push!(repls, r"^#src.*\n?"m => "") # remove leading #src lines push!(repls, r".*#src$\n?"m => "") # remove trailing #src lines diff --git a/test/runtests.jl b/test/runtests.jl index b054736..2a7b135 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -255,6 +255,15 @@ content = """ #nb #- #nb # %% [markdown] {"meta": "data"} #nb # # Explicit markdown cell with metadata + + #= + First multiline + comment + =# + + #======================= + Second multiline comment + =======================# """ const TRAVIS_ENV = Dict( @@ -424,6 +433,8 @@ const GITLAB_ENV = Dict( @test occursin("# # Example", script) @test occursin("# foo, bar", script) @test occursin("# \\int f(x) dx", script) + @test occursin("# First multiline", script) + @test occursin("# Second multiline comment", script) # verify that inputfile exists @test_throws ArgumentError Literate.script("nonexistent.jl", outdir) @@ -556,6 +567,11 @@ end end hidden2 * hidden2 ``` + First multiline + comment + + Second multiline comment + --- *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": [ "---\\n",