Skip to content

Commit

Permalink
Removed references to sandbox module
Browse files Browse the repository at this point in the history
  • Loading branch information
gbruer15 committed Sep 21, 2024
1 parent f26fb3d commit 356cd06
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/IJulia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

Check warning on line 47 in src/IJulia.jl

View check run for this annotation

Codecov / codecov/patch

src/IJulia.jl#L47

Added line #L47 was not covered by tests
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
Expand Down
18 changes: 11 additions & 7 deletions src/Literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -653,15 +653,15 @@ 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')
return
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" => "```")
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 356cd06

Please sign in to comment.