Browse Source

Write display() calls in markdown output

pull/256/head
Grant Bruer 1 year ago
parent
commit
afb0239caa
  1. 39
      src/Literate.jl
  2. 22
      test/runtests.jl

39
src/Literate.jl

@ -641,46 +641,59 @@ function execute_markdown!(io::IO, sb::Module, block::String, outputdir;
flavor::AbstractFlavor, image_formats::Vector, file_prefix::String, flavor::AbstractFlavor, image_formats::Vector, file_prefix::String,
softscope::Bool) softscope::Bool)
# TODO: Deal with explicit display(...) calls # TODO: Deal with explicit display(...) calls
r, str, _ = execute_block(sb, block; inputfile=inputfile, fake_source=fake_source, softscope=softscope) r, str, display_dicts = execute_block(sb, block; inputfile=inputfile, fake_source=fake_source, softscope=softscope)
# issue #101: consecutive codefenced blocks need newline # issue #101: consecutive codefenced blocks need newline
# issue #144: quadruple backticks allow for triple backticks in the output # issue #144: quadruple backticks allow for triple backticks in the output
plain_fence = "\n````\n" => "\n````" plain_fence = "\n````\n" => "\n````"
# Any explicit calls to display(...)
for dict in display_dicts
for (mime, data) in dict
display_markdown(io, data, outputdir, flavor, image_formats, file_prefix, plain_fence)
end
end
if r !== nothing && !REPL.ends_with_semicolon(block) if r !== nothing && !REPL.ends_with_semicolon(block)
display_markdown(io, r, outputdir, flavor, image_formats, file_prefix, plain_fence)
return
elseif !isempty(str)
write(io, plain_fence.first, str, plain_fence.second, '\n')
return
end
end
function display_markdown(io, data, outputdir, flavor, image_formats, file_prefix, plain_fence)
if (flavor isa FranklinFlavor || flavor isa DocumenterFlavor) && if (flavor isa FranklinFlavor || flavor isa DocumenterFlavor) &&
Base.invokelatest(showable, MIME("text/html"), r) Base.invokelatest(showable, MIME("text/html"), data)
htmlfence = flavor isa FranklinFlavor ? ("~~~" => "~~~") : ("```@raw html" => "```") htmlfence = flavor isa FranklinFlavor ? ("~~~" => "~~~") : ("```@raw html" => "```")
write(io, "\n", htmlfence.first, "\n") write(io, "\n", htmlfence.first, "\n")
Base.invokelatest(show, io, MIME("text/html"), r) Base.invokelatest(show, io, MIME("text/html"), data)
write(io, "\n", htmlfence.second, "\n") write(io, "\n", htmlfence.second, "\n")
return return
end end
for (mime, ext) in image_formats for (mime, ext) in image_formats
if Base.invokelatest(showable, mime, r) if Base.invokelatest(showable, mime, data)
file = file_prefix * ext file = file_prefix * ext
open(joinpath(outputdir, file), "w") do io open(joinpath(outputdir, file), "w") do io
Base.invokelatest(show, io, mime, r) Base.invokelatest(show, io, mime, data)
end end
write(io, "![](", file, ")\n") write(io, "![](", file, ")\n")
return return
end end
end end
if Base.invokelatest(showable, MIME("text/markdown"), r) if Base.invokelatest(showable, MIME("text/markdown"), data)
write(io, '\n') write(io, '\n')
Base.invokelatest(show, io, MIME("text/markdown"), r) Base.invokelatest(show, io, MIME("text/markdown"), data)
write(io, '\n') write(io, '\n')
return return
end end
# fallback to text/plain # fallback to text/plain
write(io, plain_fence.first) write(io, plain_fence.first)
Base.invokelatest(show, io, "text/plain", r) Base.invokelatest(show, io, "text/plain", data)
write(io, plain_fence.second, '\n') write(io, plain_fence.second, '\n')
return return
elseif !isempty(str)
write(io, plain_fence.first, str, plain_fence.second, '\n')
return
end end
end
const JUPYTER_VERSION = v"4.3.0" const JUPYTER_VERSION = v"4.3.0"

22
test/runtests.jl

@ -985,6 +985,28 @@ end end
flavor=Literate.CommonMarkFlavor()) flavor=Literate.CommonMarkFlavor())
@test read(joinpath(outdir, "inputfile-1.svg"), String) == "issue228" @test read(joinpath(outdir, "inputfile-1.svg"), String) == "issue228"
# Calls to display(x) and display(mime, x)
script = """
struct DF x end
Base.show(io::IO, ::MIME"text/plain", df::DF) = print(io, "DF(\$(df.x)) as text/plain")
Base.show(io::IO, ::MIME"text/html", df::DF) = print(io, "DF(\$(df.x)) as text/html")
Base.show(io::IO, ::MIME"text/latex", df::DF) = print(io, "DF(\$(df.x)) as text/latex")
#-
foreach(display, [DF(1), DF(2)])
DF(3)
#-
display(MIME("text/latex"), DF(4))
"""
write(inputfile, script)
Literate.markdown(inputfile, outdir; execute=true)
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("````\n\"DF(1) as text/plain\"\n````", markdown)
@test occursin("````\n\"DF(1) as text/html\"\n````", markdown)
@test occursin("````\n\"DF(2) as text/plain\"\n````", markdown)
@test occursin("````\n\"DF(2) as text/html\"\n````", markdown)
@test occursin("```@raw html\nDF(3) as text/html\n```", markdown)
@test occursin("````\n\"DF(4) as text/latex\"\n````", markdown)
# Softscope # Softscope
write( write(
inputfile, inputfile,

Loading…
Cancel
Save