diff --git a/src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl b/src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl index 0a9fdbd..37e666f 100644 --- a/src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl +++ b/src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl @@ -14,10 +14,15 @@ function configure() kwargs = Dict{Symbol,Any}() end if fm.fig_format == "pdf" - CairoMakie.activate!(; type = "png", kwargs...) + kwargs[:type] = "png" else - CairoMakie.activate!(; type = fm.fig_format, kwargs...) + if isa(fm.fig_format, AbstractString) + kwargs[:type] = fm.fig_format + end end + CairoMakie.activate!(; kwargs...) + + return nothing end function __init__() diff --git a/src/QuartoNotebookWorker/src/NotebookState.jl b/src/QuartoNotebookWorker/src/NotebookState.jl index fa580b5..421711e 100644 --- a/src/QuartoNotebookWorker/src/NotebookState.jl +++ b/src/QuartoNotebookWorker/src/NotebookState.jl @@ -11,6 +11,7 @@ const CELL_OPTIONS = Ref(Dict{String,Any}()) function __init__() if ccall(:jl_generating_output, Cint, ()) == 0 PROJECT[] = Base.active_project() + OPTIONS[] = task_local_storage(:QUARTO_NOTEBOOK_WORKER_OPTIONS, Dict{String,Any}()) define_notebook_module!() end end diff --git a/src/QuartoNotebookWorker/src/refresh.jl b/src/QuartoNotebookWorker/src/refresh.jl index 9885711..ca6154e 100644 --- a/src/QuartoNotebookWorker/src/refresh.jl +++ b/src/QuartoNotebookWorker/src/refresh.jl @@ -24,13 +24,25 @@ function refresh!(path, original_options, options = original_options) return nothing end +function rget(dict, keys, default) + value = dict + for key in keys + if haskey(value, key) + value = value[key] + else + return default + end + end + return value +end + function _figure_metadata() options = NotebookState.OPTIONS[] - fig_width_inch = options["format"]["execute"]["fig-width"] - fig_height_inch = options["format"]["execute"]["fig-height"] - fig_format = options["format"]["execute"]["fig-format"] - fig_dpi = options["format"]["execute"]["fig-dpi"] + fig_width_inch = rget(options, ("format", "execute", "fig-width"), nothing) + fig_height_inch = rget(options, ("format", "execute", "fig-height"), nothing) + fig_format = rget(options, ("format", "execute", "fig-format"), nothing) + fig_dpi = rget(options, ("format", "execute", "fig-dpi"), nothing) if fig_format == "retina" fig_format = "svg" diff --git a/src/QuartoNotebookWorker/src/render.jl b/src/QuartoNotebookWorker/src/render.jl index c21aef4..5f1a1c7 100644 --- a/src/QuartoNotebookWorker/src/render.jl +++ b/src/QuartoNotebookWorker/src/render.jl @@ -326,7 +326,8 @@ function render_mimetypes(value, cell_options; inline::Bool = false) # what the package defines itself. value = _mimetype_wrapper(value) - to_format = NotebookState.OPTIONS[]["format"]["pandoc"]["to"] + options = NotebookState.OPTIONS[] + to_format = rget(options, ("format", "pandoc", "to"), nothing) result = Dict{String,@NamedTuple{error::Bool, data::Vector{UInt8}}}() # Some output formats that we want to write to need different diff --git a/src/QuartoNotebookWorker/test/runtests.jl b/src/QuartoNotebookWorker/test/runtests.jl index de4ebe6..3c07efa 100644 --- a/src/QuartoNotebookWorker/test/runtests.jl +++ b/src/QuartoNotebookWorker/test/runtests.jl @@ -4,4 +4,10 @@ using Test, QuartoNotebookWorker # Just a dummy test for now. We can start adding real tests in follow-up PRs # that make changes to the worker code. @test QuartoNotebookWorker.Packages.is_precompiling() == false + @test QuartoNotebookWorker._figure_metadata() == ( + fig_width_inch = nothing, + fig_height_inch = nothing, + fig_format = nothing, + fig_dpi = nothing, + ) end diff --git a/src/WorkerSetup.jl b/src/WorkerSetup.jl index a023a5a..35744b5 100644 --- a/src/WorkerSetup.jl +++ b/src/WorkerSetup.jl @@ -245,11 +245,13 @@ function test(; exeflags = String[]) write( file, """ + pushfirst!(LOAD_PATH, "@stdlib") + import Pkg + popfirst!(LOAD_PATH) + cd($(repr(QNW))) pushfirst!(LOAD_PATH, $(repr(project))) - - import Pkg Pkg.test("QuartoNotebookWorker") """, ) diff --git a/src/worker.jl b/src/worker.jl index c047a60..986b2c0 100644 --- a/src/worker.jl +++ b/src/worker.jl @@ -4,12 +4,14 @@ function worker_init(f::File, options::Dict) return quote push!(LOAD_PATH, $(project)) - let QNW = Base.require( - Base.PkgId( - Base.UUID("38328d9c-a911-4051-bc06-3f7f556ffeda"), - "QuartoNotebookWorker", - ), - ) + let QNW = task_local_storage(:QUARTO_NOTEBOOK_WORKER_OPTIONS, $(options)) do + Base.require( + Base.PkgId( + Base.UUID("38328d9c-a911-4051-bc06-3f7f556ffeda"), + "QuartoNotebookWorker", + ), + ) + end global refresh!(args...) = QNW.refresh!($(f.path), $(options), args...) global render(args...; kwargs...) = QNW.render(args...; kwargs...) end diff --git a/test/worker_tests.jl b/test/worker_tests.jl new file mode 100644 index 0000000..40cbebf --- /dev/null +++ b/test/worker_tests.jl @@ -0,0 +1,6 @@ +using Test +import QuartoNotebookRunner + +@testset "QuartoNotebookWorker" begin + @test success(QuartoNotebookRunner.WorkerSetup.test()) +end