Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #143, pass :module to IOCapture.capture #149

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
Base64 = "1.6"
CommonMark = "0.8"
Compat = "4"
IOCapture = "0.2"
IOCapture = "0.2.5"
InteractiveUtils = "1.6"
IterTools = "1"
JSON3 = "1"
Expand Down
25 changes: 19 additions & 6 deletions src/QuartoNotebookWorker/src/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
code;
file,
line,
cell_options,
)
end,
)
Expand Down Expand Up @@ -173,15 +174,25 @@
return _parseall(code; filename, lineno)
end

function include_str(mod::Module, code::AbstractString; file::AbstractString, line::Integer)
function include_str(

Check warning on line 177 in src/QuartoNotebookWorker/src/render.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L177

Added line #L177 was not covered by tests
mod::Module,
code::AbstractString;
file::AbstractString,
line::Integer,
cell_options::AbstractDict,
)
loc = LineNumberNode(line, Symbol(file))
try
ast = _process_code(mod, code; filename = file, lineno = line)
@assert Meta.isexpr(ast, :toplevel)
# Note: IO capturing combines stdout and stderr into a single
# `.output`, but Jupyter notebook spec appears to want them
# separate. Revisit this if it causes issues.
return Packages.IOCapture.capture(; rethrow = InterruptException, color = true) do
return Packages.IOCapture.capture(;

Check warning on line 191 in src/QuartoNotebookWorker/src/render.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L191

Added line #L191 was not covered by tests
rethrow = InterruptException,
color = true,
io_context = _io_context(cell_options),
) do
result = nothing
line_and_ex = Expr(:toplevel, loc, nothing)
try
Expand Down Expand Up @@ -223,10 +234,12 @@

# passing our module removes Main.Notebook noise when printing types etc.
function with_context(io::IO, cell_options = Dict{String,Any}())
return IOContext(
io,
return IOContext(io, _io_context(cell_options)...)

Check warning on line 237 in src/QuartoNotebookWorker/src/render.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L237

Added line #L237 was not covered by tests
end

function _io_context(cell_options = Dict{String,Any}())
return [

Check warning on line 241 in src/QuartoNotebookWorker/src/render.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L240-L241

Added lines #L240 - L241 were not covered by tests
:module => NotebookState.notebook_module(),
:color => true,
:limit => true,
# This allows a `show` method implementation to check for
# metadata that may be of relevance to it's rendering. For
Expand All @@ -238,7 +251,7 @@
# TODO: perhaps preprocess the metadata provided here rather
# than just passing it through as-is.
:QuartoNotebookRunner => (; cell_options, options = NotebookState.OPTIONS[]),
)
]
end

function clean_bt_str(is_error::Bool, bt, err, prefix = "", mimetype = false)
Expand Down
11 changes: 11 additions & 0 deletions test/examples/io_context.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: io_context
---

```{julia}
struct M
a::Int
end
x = M(22)
print(x)
```
12 changes: 12 additions & 0 deletions test/testsets/io_context.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include("../utilities/prelude.jl")

test_example(joinpath(@__DIR__, "../examples/io_context.qmd")) do json
cells = json["cells"]
@test length(cells) == 2

cell = cells[2]
output = cell["outputs"][1]
@test output["output_type"] == "stream"
@test output["name"] == "stdout"
@test output["text"] == "M(22)"
end
Loading