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

Use quarto-provided markdown if present #154

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 17 additions & 2 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,34 @@ Evaluate the code and markdown in `f` and return a vector of cells with the
results in all available mimetypes.

`output` can be a file path, or an IO stream.
`markdown` can be used to pass an override for the file content, which is used
to pass the modified markdown that quarto creates after resolving shortcodes
"""
function evaluate!(
f::File,
output::Union{AbstractString,IO,Nothing} = nothing;
showprogress = true,
options::Union{String,Dict{String,Any}} = Dict{String,Any}(),
chunk_callback = (i, n, c) -> nothing,
markdown::Union{String,Nothing} = nothing,
)
_check_output_dst(output)

options = _parsed_options(options)
path = abspath(f.path)
if isfile(path)
raw_chunks, file_frontmatter = raw_text_chunks(f)
display(markdown)
raw_chunks, file_frontmatter = if markdown === nothing
raw_text_chunks(f)
else
mktempdir() do dir
tempfile = joinpath(dir, "input.qmd")
open(tempfile, "w") do io
write(io, markdown)
end
raw_text_chunks(tempfile)
MichaelHatherly marked this conversation as resolved.
Show resolved Hide resolved
end
end
merged_options = _extract_relevant_options(file_frontmatter, options)
cells =
evaluate_raw_cells!(f, raw_chunks, merged_options; showprogress, chunk_callback)
Expand Down Expand Up @@ -1067,6 +1081,7 @@ function run!(
server::Server,
path::AbstractString;
output::Union{AbstractString,IO,Nothing} = nothing,
markdown::Union{Nothing,String} = nothing,
showprogress::Bool = true,
options::Union{String,Dict{String,Any}} = Dict{String,Any}(),
chunk_callback = (i, n, c) -> nothing,
Expand All @@ -1076,7 +1091,7 @@ function run!(
close(file.timeout_timer)
file.timeout_timer = nothing
end
result = evaluate!(file, output; showprogress, options, chunk_callback)
result = evaluate!(file, output; showprogress, options, markdown, chunk_callback)
if file.timeout > 0
file.timeout_timer = Timer(file.timeout) do _
close!(server, file.path)
Expand Down
23 changes: 22 additions & 1 deletion src/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ function _handle_response(

if type == "run"
options = _get_options(request.content)
markdown = _get_markdown(options)

function chunk_callback(i, n, chunk)
_write_json(
Expand All @@ -269,7 +270,16 @@ function _handle_response(
end

result = try
(; notebook = run!(notebooks, file; options, showprogress, chunk_callback))
(;
notebook = run!(
notebooks,
file;
options,
markdown,
showprogress,
chunk_callback,
)
)
catch error
_log_error("Failed to run notebook: $file", error, catch_backtrace())
end
Expand Down Expand Up @@ -356,6 +366,17 @@ _get_file(content::String) = content
_get_options(content::Dict) = get(Dict{String,Any}, content, "options")
_get_options(::String) = Dict{String,Any}()

function _get_nested(d::Dict, keys...)
_d = d
for key in keys
_d = get(_d, key, nothing)
_d === nothing && return
end
return _d
end
_get_markdown(options::Dict)::Union{Nothing,String} =
_get_nested(options, "target", "markdown", "value")

# Compat:

if !isdefined(Base, :errormonitor)
Expand Down
Loading