Browse Source

Support image/svg+xml for markdown execution. (#183)

pull/184/head
Tamas K. Papp 4 years ago committed by GitHub
parent
commit
cb788bc399
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/Literate.jl
  2. 5
      test/runtests.jl

16
src/Literate.jl

@ -224,6 +224,9 @@ end
filename(str) = first(splitext(last(splitdir(str)))) filename(str) = first(splitext(last(splitdir(str))))
isdocumenter(cfg) = cfg["flavor"]::AbstractFlavor isa DocumenterFlavor 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) function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
# Combine user config with user kwargs # Combine user config with user kwargs
user_config = Dict{String,Any}(string(k) => v for (k, v) in user_config) user_config = Dict{String,Any}(string(k) => v for (k, v) in user_config)
@ -260,6 +263,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
!get(user_config, "execute", cfg["execute"]) ? !get(user_config, "execute", cfg["execute"]) ?
("````@example $(get(user_config, "name", replace(cfg["name"], r"\s" => "_")))" => "````") : ("````@example $(get(user_config, "name", replace(cfg["name"], r"\s" => "_")))" => "````") :
("````julia" => "````") ("````julia" => "````")
cfg["image_formats"] = _DEFAULT_IMAGE_FORMATS
# Guess the package (or repository) root url # Guess the package (or repository) root url
edit_commit = "master" # TODO: Make this configurable like Documenter? edit_commit = "master" # TODO: Make this configurable like Documenter?
deploy_branch = "gh-pages" # TODO: Make this configurable like Documenter? deploy_branch = "gh-pages" # TODO: Make this configurable like Documenter?
@ -370,6 +374,9 @@ Available options:
- `repo_root_path`: Filepath to the root of the repository. Determined automatically on - `repo_root_path`: Filepath to the root of the repository. Determined automatically on
Travis CI, GitHub Actions and GitLab CI. Used for computing Travis CI, GitHub Actions and GitLab CI. Used for computing
[Documenters `EditURL`](@ref Interaction-with-Documenter). [Documenters `EditURL`](@ref Interaction-with-Documenter).
- `image_formats`: A vector of `(mime, ext)` tuples, with the default
`$(_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 const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation
@ -523,7 +530,9 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
write_code && write(iomd, seekstart(iocode)) write_code && write(iomd, seekstart(iocode))
if config["execute"]::Bool if config["execute"]::Bool
execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir; execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir;
inputfile=config["literate_inputfile"], flavor=config["flavor"]) inputfile=config["literate_inputfile"],
flavor=config["flavor"],
image_formats=config["image_formats"])
end end
end end
write(iomd, '\n') # add a newline between each chunk write(iomd, '\n') # add a newline between each chunk
@ -538,7 +547,8 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
end end
function execute_markdown!(io::IO, sb::Module, block::String, outputdir; function execute_markdown!(io::IO, sb::Module, block::String, outputdir;
inputfile::String="<unknown>", flavor::AbstractFlavor) inputfile::String="<unknown>", flavor::AbstractFlavor,
image_formats::Vector)
# TODO: Deal with explicit display(...) calls # TODO: Deal with explicit display(...) calls
r, str, _ = execute_block(sb, block; inputfile=inputfile) r, str, _ = execute_block(sb, block; inputfile=inputfile)
# issue #101: consecutive codefenced blocks need newline # issue #101: consecutive codefenced blocks need newline
@ -553,7 +563,7 @@ function execute_markdown!(io::IO, sb::Module, block::String, outputdir;
write(io, "\n", htmlfence.second, "\n") write(io, "\n", htmlfence.second, "\n")
return return
end end
for (mime, ext) in [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg")] for (mime, ext) in image_formats
if showable(mime, r) if showable(mime, r)
file = string(hash(block) % UInt32) * ext file = string(hash(block) % UInt32) * ext
open(joinpath(outputdir, file), "w") do io open(joinpath(outputdir, file), "w") do io

5
test/runtests.jl

@ -778,6 +778,10 @@ end end
Base.show(io::IO, mime::MIME"image/jpeg", ::JPEG) = print(io, "JPEG") Base.show(io::IO, mime::MIME"image/jpeg", ::JPEG) = print(io, "JPEG")
JPEG() JPEG()
#- #-
struct SVG end
Base.show(io::IO, mime::MIME"image/svg+xml", ::SVG) = print(io, "SVG")
SVG()
#-
struct MD end struct MD end
Base.show(io::IO, mime::MIME"text/markdown", ::MD) = print(io, "# " * "MD") Base.show(io::IO, mime::MIME"text/markdown", ::MD) = print(io, "# " * "MD")
Base.show(io::IO, mime::MIME"text/html", ::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("```\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+\.png\)", markdown) # image/png
@test occursin(r"!\[\]\(\d+\.jpeg\)", markdown) # image/jpeg @test occursin(r"!\[\]\(\d+\.jpeg\)", markdown) # image/jpeg
@test occursin(r"!\[\]\(\d+\.svg\)", markdown) # image/svg+xml, fredrikekre/Literate.jl#182
@test occursin("# MD", markdown) # text/markdown @test occursin("# MD", markdown) # text/markdown
@test occursin("```@raw html\n<h1>MD</h1>\n```", markdown) # text/html @test occursin("```@raw html\n<h1>MD</h1>\n```", markdown) # text/html
@test occursin("```\nhello, world\n```", markdown) # stdout/stderr @test occursin("```\nhello, world\n```", markdown) # stdout/stderr

Loading…
Cancel
Save