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

print inline code results instead of showing them #153

Merged
merged 2 commits into from
Jun 14, 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
56 changes: 37 additions & 19 deletions src/QuartoNotebookWorker/src/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
code::AbstractString,
file::AbstractString,
line::Integer,
cell_options::AbstractDict = Dict{String,Any}(),
cell_options::AbstractDict = Dict{String,Any}();
inline::Bool = false,
)
# This records whether the outermost cell is an expandable cell, which we
# then return to the server so that it can decide whether to treat the cell
Expand All @@ -13,7 +14,7 @@
is_expansion_ref = Ref(false)
result = Base.@invokelatest(
collect(
_render_thunk(code, cell_options, is_expansion_ref) do
_render_thunk(code, cell_options, is_expansion_ref; inline) do
Base.@invokelatest include_str(
NotebookState.notebook_module(),
code;
Expand All @@ -35,7 +36,8 @@
thunk::Base.Callable,
code::AbstractString,
cell_options::AbstractDict = Dict{String,Any}(),
is_expansion_ref::Ref{Bool} = Ref(false),
is_expansion_ref::Ref{Bool} = Ref(false);
inline::Bool,
)
captured, display_results = with_inline_display(thunk, cell_options)

Expand Down Expand Up @@ -81,12 +83,18 @@
)
end
# **The recursive call:**
return Base.@invokelatest _render_thunk(wrapped, cell.code, cell.options)
return Base.@invokelatest _render_thunk(

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

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L86

Added line #L86 was not covered by tests
wrapped,
cell.code,
cell.options;
inline,
)
end
else
results = Base.@invokelatest render_mimetypes(
REPL.ends_with_semicolon(code) ? nothing : captured.value,
cell_options,
cell_options;
inline,
)
# Wrap the `NamedTuple` in a `Tuple` to avoid the `NamedTuple`
# being flattened into just it's values when passed into
Expand Down Expand Up @@ -290,7 +298,9 @@
Base.showable(mime::MIME, w::WrapperType) = Base.showable(mime, w.value)


function render_mimetypes(value, cell_options)
# for inline code chunks, `inline` should be set to `true` which causes "text/plain" output like
# what you'd get from `print` (Strings without quotes) and not from `show("text/plain", ...)`
function render_mimetypes(value, cell_options; inline::Bool = false)

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

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L303

Added line #L303 was not covered by tests
# Intercept objects prior to rendering so that we can wrap specific
# types in our own `WrapperType` to customised rendering instead of
# what the package defines itself.
Expand Down Expand Up @@ -329,23 +339,31 @@
"image/png",
],
)
mimes = get(mime_groups, to_format) do
[
"text/plain",
"text/markdown",
"text/html",
"text/latex",
"image/svg+xml",
"image/png",
"application/pdf",
"application/json",
]
mimes = if inline
["text/plain", "text/markdown"]

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

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L342-L343

Added lines #L342 - L343 were not covered by tests
else
get(mime_groups, to_format) do
[

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

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L345-L346

Added lines #L345 - L346 were not covered by tests
"text/plain",
"text/markdown",
"text/html",
"text/latex",
"image/svg+xml",
"image/png",
"application/pdf",
"application/json",
]
end
end
for mime in mimes
if showable(mime, value)
buffer = IOBuffer()
try
Base.@invokelatest show(with_context(buffer, cell_options), mime, value)
if inline && mime == "text/plain"
Base.@invokelatest print(with_context(buffer, cell_options), value)

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

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L362-L363

Added lines #L362 - L363 were not covered by tests
else
Base.@invokelatest show(with_context(buffer, cell_options), mime, value)

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

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L365

Added line #L365 was not covered by tests
end
catch error
backtrace = catch_backtrace()
result[mime] = (;
Expand Down Expand Up @@ -377,7 +395,7 @@
end
return result
end
render_mimetypes(value::Nothing, cell_options) =
render_mimetypes(value::Nothing, cell_options; inline::Bool = false) =

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

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L398

Added line #L398 was not covered by tests
Dict{String,@NamedTuple{error::Bool, data::Vector{UInt8}}}()


Expand Down
7 changes: 6 additions & 1 deletion src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,12 @@ function evaluate_raw_cells!(
if startswith(node.literal, "{r}")
source_code = wrap_with_r_boilerplate(source_code)
end
expr = :(render($(source_code), $(chunk.file), $(chunk.line)))
expr = :(render(
$(source_code),
$(chunk.file),
$(chunk.line);
inline = true,
))
# There should only ever be a single result from an
# inline evaluation since you can't pass cell
# options and so `expand` will always be `false`.
Expand Down
2 changes: 1 addition & 1 deletion src/worker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function worker_init(f::File, options::Dict)
),
)
global refresh!(args...) = QNW.refresh!($(f.path), $(options), args...)
global render(args...) = QNW.render(args...)
global render(args...; kwargs...) = QNW.render(args...; kwargs...)
end

nothing
Expand Down
2 changes: 1 addition & 1 deletion test/testsets/inline_code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ include("../utilities/prelude.jl")
@test any(contains("Some `Text` objects *\\*placeholder\\**."), cell.source)

cell = cells[9]
@test any(contains("Some plain `String`s \"\\*placeholder\\*\"."), cell.source)
@test any(contains("Some plain `String`s \\*placeholder\\*."), cell.source)
@test any(contains("A more complex expression 2."), cell.source)
end
end
Loading