From 39365cf9dc263989dd877b1c27ce4c95e606dc94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20K=2E=20Papp?= Date: Mon, 17 Jan 2022 16:11:53 +0100 Subject: [PATCH 1/7] Add configuration for captured mime types. Fixes #182. --- src/Literate.jl | 14 +++++++++++--- test/runtests.jl | 11 +++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Literate.jl b/src/Literate.jl index aecdb372..b063b1c1 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -260,6 +260,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) !get(user_config, "execute", cfg["execute"]) ? ("````@example $(get(user_config, "name", replace(cfg["name"], r"\s" => "_")))" => "````") : ("````julia" => "````") + cfg["mime_extensions"] = [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg")] # Guess the package (or repository) root url edit_commit = "master" # TODO: Make this configurable like Documenter? deploy_branch = "gh-pages" # TODO: Make this configurable like Documenter? @@ -370,6 +371,10 @@ Available options: - `repo_root_path`: Filepath to the root of the repository. Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for computing [Documenters `EditURL`](@ref Interaction-with-Documenter). +- `mime_extensions`: A vector of `(mime, ext)` tuples, with the default + `[(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg")]`. Results which are + `showable` with a MIME type are saved with the first match, with the corresponding + extension. """ const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation @@ -523,7 +528,9 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...) write_code && write(iomd, seekstart(iocode)) if config["execute"]::Bool execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir; - inputfile=config["literate_inputfile"], flavor=config["flavor"]) + inputfile=config["literate_inputfile"], + flavor=config["flavor"], + mime_extensions=config["mime_extensions"]) end end write(iomd, '\n') # add a newline between each chunk @@ -538,7 +545,8 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...) end function execute_markdown!(io::IO, sb::Module, block::String, outputdir; - inputfile::String="", flavor::AbstractFlavor) + inputfile::String="", flavor::AbstractFlavor, + mime_extensions::Vector) # TODO: Deal with explicit display(...) calls r, str, _ = execute_block(sb, block; inputfile=inputfile) # issue #101: consecutive codefenced blocks need newline @@ -553,7 +561,7 @@ function execute_markdown!(io::IO, sb::Module, block::String, outputdir; write(io, "\n", htmlfence.second, "\n") return end - for (mime, ext) in [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg")] + for (mime, ext) in mime_extensions if showable(mime, r) file = string(hash(block) % UInt32) * ext open(joinpath(outputdir, file), "w") do io diff --git a/test/runtests.jl b/test/runtests.jl index 45c8f2f5..64c5ebeb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -840,6 +840,17 @@ end end write(f, "1 + 1") Literate.markdown(f, outdir) @test occursin("file_with_space", read(joinpath(outdir, "file with space.md"), String)) + + # fredrikekre/Literate.jl#182 + write(inputfile, """ + struct SVG end + Base.show(io::IO, mime::MIME"image/svg", ::SVG) = print(io, "SVG") + SVG() + """) + Literate.markdown(inputfile, outdir; execute=true, + config = Dict("mime_extensions" => [(MIME("image/svg"), ".svg")])) + markdown = read(joinpath(outdir, "inputfile.md"), String) + @test occursin(r"!\[\]\(\d+\.svg\)", markdown) # image/svg end end end end From d587d1af09807feb2ffacb44d491ac54f57f5ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20K=2E=20Papp?= Date: Mon, 17 Jan 2022 16:39:50 +0100 Subject: [PATCH 2/7] Use an actual registered MIME type (cosmetic). --- test/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 64c5ebeb..94bdb99d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -844,11 +844,11 @@ end end # fredrikekre/Literate.jl#182 write(inputfile, """ struct SVG end - Base.show(io::IO, mime::MIME"image/svg", ::SVG) = print(io, "SVG") + Base.show(io::IO, mime::MIME"image/svg+xml", ::SVG) = print(io, "SVG") SVG() """) Literate.markdown(inputfile, outdir; execute=true, - config = Dict("mime_extensions" => [(MIME("image/svg"), ".svg")])) + config = Dict("mime_extensions" => [(MIME("image/svg+xml"), ".svg")])) markdown = read(joinpath(outdir, "inputfile.md"), String) @test occursin(r"!\[\]\(\d+\.svg\)", markdown) # image/svg end From 83b4512e67f427a9f59adf1b3225c9850723cb19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20K=2E=20Papp?= Date: Mon, 24 Jan 2022 14:53:10 +0100 Subject: [PATCH 3/7] Handle svg+xml by default. --- src/Literate.jl | 3 ++- test/runtests.jl | 15 +++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/Literate.jl b/src/Literate.jl index b063b1c1..58ba2ad9 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -260,7 +260,8 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) !get(user_config, "execute", cfg["execute"]) ? ("````@example $(get(user_config, "name", replace(cfg["name"], r"\s" => "_")))" => "````") : ("````julia" => "````") - cfg["mime_extensions"] = [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg")] + cfg["mime_extensions"] = [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg"), + (MIME("image/svg+xml"), ".svg")] # Guess the package (or repository) root url edit_commit = "master" # TODO: Make this configurable like Documenter? deploy_branch = "gh-pages" # TODO: Make this configurable like Documenter? diff --git a/test/runtests.jl b/test/runtests.jl index 94bdb99d..1a7ed3eb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -778,6 +778,10 @@ end end Base.show(io::IO, mime::MIME"image/jpeg", ::JPEG) = print(io, "JPEG") JPEG() #- + struct SVG end + Base.show(io::IO, mime::MIME"image/svg+xml", ::SVG) = print(io, "SVG") + SVG() + #- struct MD end Base.show(io::IO, mime::MIME"text/markdown", ::MD) = print(io, "# " * "MD") Base.show(io::IO, mime::MIME"text/html", ::MD) = @@ -805,6 +809,7 @@ end end @test occursin("```\n2×2 $(Matrix{Int}):\n 1 2\n 3 4\n```", markdown) # text/plain @test occursin(r"!\[\]\(\d+\.png\)", markdown) # image/png @test occursin(r"!\[\]\(\d+\.jpeg\)", markdown) # image/jpeg +e @test occursin(r"!\[\]\(\d+\.svg\)", markdown) # image/svg+xml, fredrikekre/Literate.jl#182 @test occursin("# MD", markdown) # text/markdown @test occursin("```@raw html\n

MD

\n```", markdown) # text/html @test occursin("```\nhello, world\n```", markdown) # stdout/stderr @@ -841,16 +846,6 @@ end end Literate.markdown(f, outdir) @test occursin("file_with_space", read(joinpath(outdir, "file with space.md"), String)) - # fredrikekre/Literate.jl#182 - write(inputfile, """ - struct SVG end - Base.show(io::IO, mime::MIME"image/svg+xml", ::SVG) = print(io, "SVG") - SVG() - """) - Literate.markdown(inputfile, outdir; execute=true, - config = Dict("mime_extensions" => [(MIME("image/svg+xml"), ".svg")])) - markdown = read(joinpath(outdir, "inputfile.md"), String) - @test occursin(r"!\[\]\(\d+\.svg\)", markdown) # image/svg end end end end From 270c4705a16715006db047d1e0f50833dc27300f Mon Sep 17 00:00:00 2001 From: "Tamas K. Papp" Date: Mon, 24 Jan 2022 14:14:09 +0000 Subject: [PATCH 4/7] Update test/runtests.jl Co-authored-by: Fredrik Ekre --- test/runtests.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 1a7ed3eb..760b492a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -845,7 +845,6 @@ e @test occursin(r"!\[\]\(\d+\.svg\)", markdown) # image/svg+xml, fre write(f, "1 + 1") Literate.markdown(f, outdir) @test occursin("file_with_space", read(joinpath(outdir, "file with space.md"), String)) - end end end end From 4f7e415589c52291b663edf51ad75e4a7ae3d576 Mon Sep 17 00:00:00 2001 From: "Tamas K. Papp" Date: Mon, 24 Jan 2022 14:14:19 +0000 Subject: [PATCH 5/7] Update test/runtests.jl Co-authored-by: Fredrik Ekre --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 760b492a..c229c7e6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -809,7 +809,7 @@ end end @test occursin("```\n2×2 $(Matrix{Int}):\n 1 2\n 3 4\n```", markdown) # text/plain @test occursin(r"!\[\]\(\d+\.png\)", markdown) # image/png @test occursin(r"!\[\]\(\d+\.jpeg\)", markdown) # image/jpeg -e @test occursin(r"!\[\]\(\d+\.svg\)", markdown) # image/svg+xml, fredrikekre/Literate.jl#182 + @test occursin(r"!\[\]\(\d+\.svg\)", markdown) # image/svg+xml, fredrikekre/Literate.jl#182 @test occursin("# MD", markdown) # text/markdown @test occursin("```@raw html\n

MD

\n```", markdown) # text/html @test occursin("```\nhello, world\n```", markdown) # stdout/stderr From 132bca9caf916c3e421ad8e3d877d1c1d1b67454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20K=2E=20Papp?= Date: Mon, 24 Jan 2022 15:34:59 +0100 Subject: [PATCH 6/7] rename mime_extensions to image_formats --- src/Literate.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Literate.jl b/src/Literate.jl index 58ba2ad9..c4108744 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -260,8 +260,8 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) !get(user_config, "execute", cfg["execute"]) ? ("````@example $(get(user_config, "name", replace(cfg["name"], r"\s" => "_")))" => "````") : ("````julia" => "````") - cfg["mime_extensions"] = [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg"), - (MIME("image/svg+xml"), ".svg")] + cfg["image_formats"] = [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg"), + (MIME("image/svg+xml"), ".svg")] # Guess the package (or repository) root url edit_commit = "master" # TODO: Make this configurable like Documenter? deploy_branch = "gh-pages" # TODO: Make this configurable like Documenter? @@ -372,10 +372,10 @@ Available options: - `repo_root_path`: Filepath to the root of the repository. Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for computing [Documenters `EditURL`](@ref Interaction-with-Documenter). -- `mime_extensions`: A vector of `(mime, ext)` tuples, with the default - `[(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg")]`. Results which are - `showable` with a MIME type are saved with the first match, with the corresponding - extension. +- `image_formats`: A vector of `(mime, ext)` tuples, with the default + `[(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg"), (MIME("image/svg+xml"), ".svg")]`. + Results which are `showable` with a MIME type are saved with the first match, with the + corresponding extension. """ const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation @@ -531,7 +531,7 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...) execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir; inputfile=config["literate_inputfile"], flavor=config["flavor"], - mime_extensions=config["mime_extensions"]) + image_formats=config["image_formats"]) end end write(iomd, '\n') # add a newline between each chunk @@ -547,7 +547,7 @@ end function execute_markdown!(io::IO, sb::Module, block::String, outputdir; inputfile::String="", flavor::AbstractFlavor, - mime_extensions::Vector) + image_formats::Vector) # TODO: Deal with explicit display(...) calls r, str, _ = execute_block(sb, block; inputfile=inputfile) # issue #101: consecutive codefenced blocks need newline @@ -562,7 +562,7 @@ function execute_markdown!(io::IO, sb::Module, block::String, outputdir; write(io, "\n", htmlfence.second, "\n") return end - for (mime, ext) in mime_extensions + for (mime, ext) in image_formats if showable(mime, r) file = string(hash(block) % UInt32) * ext open(joinpath(outputdir, file), "w") do io From a05a3cf52e192eda6e2ca92e4908401504406e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20K=2E=20Papp?= Date: Mon, 24 Jan 2022 15:46:16 +0100 Subject: [PATCH 7/7] svg first, factored out to a constant (DRY) --- src/Literate.jl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Literate.jl b/src/Literate.jl index c4108744..37350f15 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -224,6 +224,9 @@ end filename(str) = first(splitext(last(splitdir(str)))) isdocumenter(cfg) = cfg["flavor"]::AbstractFlavor isa DocumenterFlavor +_DEFAULT_IMAGE_FORMATS = [(MIME("image/svg+xml"), ".svg"), (MIME("image/png"), ".png"), + (MIME("image/jpeg"), ".jpeg")] + function create_configuration(inputfile; user_config, user_kwargs, type=nothing) # Combine user config with user kwargs user_config = Dict{String,Any}(string(k) => v for (k, v) in user_config) @@ -260,8 +263,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) !get(user_config, "execute", cfg["execute"]) ? ("````@example $(get(user_config, "name", replace(cfg["name"], r"\s" => "_")))" => "````") : ("````julia" => "````") - cfg["image_formats"] = [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg"), - (MIME("image/svg+xml"), ".svg")] + cfg["image_formats"] = _DEFAULT_IMAGE_FORMATS # Guess the package (or repository) root url edit_commit = "master" # TODO: Make this configurable like Documenter? deploy_branch = "gh-pages" # TODO: Make this configurable like Documenter? @@ -373,9 +375,8 @@ Available options: Travis CI, GitHub Actions and GitLab CI. Used for computing [Documenters `EditURL`](@ref Interaction-with-Documenter). - `image_formats`: A vector of `(mime, ext)` tuples, with the default - `[(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg"), (MIME("image/svg+xml"), ".svg")]`. - Results which are `showable` with a MIME type are saved with the first match, with the - corresponding extension. + `$(_DEFAULT_IMAGE_FORMATS)`. Results which are `showable` with a MIME type are saved with + the first match, with the corresponding extension. """ const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation