From 356cd063662c292ef8f0a0bc5e4ebd88cf300573 Mon Sep 17 00:00:00 2001 From: Grant Bruer Date: Fri, 20 Sep 2024 23:17:24 -0400 Subject: [PATCH] Removed references to sandbox module --- src/IJulia.jl | 12 +++++++++++- src/Literate.jl | 18 +++++++++++------- test/runtests.jl | 25 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/IJulia.jl b/src/IJulia.jl index f1fb3022..4bf71df8 100644 --- a/src/IJulia.jl +++ b/src/IJulia.jl @@ -16,7 +16,7 @@ const application_vnd_vegalite_v2 = MIME("application/vnd.vegalite.v2+json") # return a String=>String dictionary of mimetype=>data # for passing to Jupyter display_data and execute_result messages. -function display_dict(x) +function display_dict(x, sandbox=nothing) data = Dict{String,Any}("text/plain" => limitstringmime(text_plain, x)) if showable(application_vnd_vegalite_v2, x) data[string(application_vnd_vegalite_v2)] = JSON.parse(limitstringmime(application_vnd_vegalite_v2, x)) @@ -39,9 +39,19 @@ function display_dict(x) elseif showable(text_latex2, x) data[string(text_latex)] = limitstringmime(text_latex2, x) end + + data["text/plain"] = remove_sandbox_from_output(data["text/plain"], sandbox) return data end +remove_sandbox_from_output(str, mod::Nothing) = nothing +function remove_sandbox_from_output(str, mod::Module) + replace(str, + Regex(("(Main\\.)?$(nameof(mod))")) => "Main", + Regex(("(Main\\.)?var\"$(nameof(mod))\"")) => "Main", + ) +end + # need special handling for showing a string as a textmime # type, since in that case the string is assumed to be # raw data unless it is text/plain diff --git a/src/Literate.jl b/src/Literate.jl index a54fabba..3d391297 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -653,7 +653,7 @@ function execute_markdown!(io::IO, sb::Module, block::String, outputdir; end if r !== nothing && !REPL.ends_with_semicolon(block) - display_markdown(io, r, outputdir, flavor, image_formats, file_prefix, plain_fence) + display_markdown(io, r, outputdir, flavor, image_formats, file_prefix, plain_fence, sb) return elseif !isempty(str) write(io, plain_fence.first, str, plain_fence.second, '\n') @@ -661,7 +661,7 @@ function execute_markdown!(io::IO, sb::Module, block::String, outputdir; end end -function display_markdown(io, data, outputdir, flavor, image_formats, file_prefix, plain_fence) +function display_markdown(io, data, outputdir, flavor, image_formats, file_prefix, plain_fence, sb) if (flavor isa FranklinFlavor || flavor isa DocumenterFlavor) && Base.invokelatest(showable, MIME("text/html"), data) htmlfence = flavor isa FranklinFlavor ? ("~~~" => "~~~") : ("```@raw html" => "```") @@ -688,8 +688,10 @@ function display_markdown(io, data, outputdir, flavor, image_formats, file_prefi end # fallback to text/plain write(io, plain_fence.first) - Base.invokelatest(show, io, "text/plain", data) + text = Base.invokelatest(repr, "text/plain", data) + text = IJulia.remove_sandbox_from_output(text, sb) write(io, plain_fence.second, '\n') + write(io, text, plain_fence.second, '\n') return end @@ -894,7 +896,7 @@ function execute_notebook(nb; inputfile::String, fake_source::String, softscope: execute_result["output_type"] = "execute_result" execute_result["metadata"] = Dict() execute_result["execution_count"] = execution_count - dict = Base.invokelatest(IJulia.display_dict, r) + dict = Base.invokelatest(IJulia.display_dict, r, sb) execute_result["data"] = split_mime(dict) push!(cell["outputs"], execute_result) end @@ -916,15 +918,17 @@ end # Capture display for notebooks struct LiterateDisplay <: AbstractDisplay data::Vector - LiterateDisplay() = new([]) + sandbox + LiterateDisplay(sandbox=nothing) = new([], sandbox) end function Base.display(ld::LiterateDisplay, x) - push!(ld.data, Base.invokelatest(IJulia.display_dict, x)) + push!(ld.data, Base.invokelatest(IJulia.display_dict, x, ld.sandbox)) return nothing end # TODO: Problematic to accept mime::MIME here? function Base.display(ld::LiterateDisplay, mime::MIME, x) r = Base.invokelatest(IJulia.limitstringmime, mime, x) + r = IJulia.remove_sandbox_from_output(r, ld.sandbox) display_dicts = Dict{String,Any}(string(mime) => r) # TODO: IJulia does this part below for unknown mimes # if istextmime(mime) @@ -942,7 +946,7 @@ function execute_block(sb::Module, block::String; inputfile::String, fake_source ``` """ # Push a capturing display on the displaystack - disp = LiterateDisplay() + disp = LiterateDisplay(sb) pushdisplay(disp) # We use the following fields of the object returned by IOCapture.capture: # - c.value: return value of the do-block (or the error object, if it throws) diff --git a/test/runtests.jl b/test/runtests.jl index e781d424..ffdd2239 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1054,6 +1054,19 @@ end end catch err @test occursin(r"`?ret`? not defined", sprint(Base.showerror, err)) end + + # Make sure references to sandbox module are removed. + write( + inputfile, + """ + struct DF x end + #- + DF + """ + ) + Literate.markdown(inputfile, outdir; execute=true) + markdown = read(joinpath(outdir, "inputfile.md"), String) + @test occursin("````\nMain.DF\n````", markdown) end # cd(sandbox) end # mktemp end end @@ -1504,6 +1517,18 @@ end end catch err @test occursin(r"`?ret`? not defined", sprint(Base.showerror, err)) end + + # Make sure references to sandbox module are removed. + write( + new_inputfile, + """ + struct DF x end + #- + DF + """ + ) + Literate.notebook(new_inputfile, outdir) + @test occursin("Main.DF", read(joinpath(outdir, "inputfile2.ipynb"), String)) end # cd(sandbox) end # mktempdir end end