Skip to content

Commit

Permalink
Add configuration for captured mime types.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpapp committed Jan 17, 2022
1 parent 29d33b7 commit 39365cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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="<unknown>", flavor::AbstractFlavor)
inputfile::String="<unknown>", 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
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 39365cf

Please sign in to comment.