Skip to content

Commit

Permalink
Rename frontmatter to options where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHatherly committed Feb 1, 2024
1 parent e256e4f commit e14508f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
22 changes: 11 additions & 11 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ function init!(file::File)
Malt.remote_eval_fetch(worker, worker_init(file))
end

function refresh!(file::File, frontmatter::Dict)
exeflags = frontmatter["format"]["metadata"]["julia"]["exeflags"]
function refresh!(file::File, options::Dict)
exeflags = options["format"]["metadata"]["julia"]["exeflags"]
if exeflags != file.exeflags
Malt.stop(file.worker)
file.worker = cd(() -> Malt.Worker(; exeflags), dirname(file.path))
file.exeflags = exeflags
init!(file)
end
expr = :(refresh!($(frontmatter)))
expr = :(refresh!($(options)))
Malt.remote_eval_fetch(file.worker, expr)
end

Expand All @@ -79,8 +79,8 @@ function evaluate!(
path = abspath(f.path)
if isfile(path)
raw_chunks, file_frontmatter = raw_text_chunks(f)
merged_frontmatter = _extract_relevant_frontmatter(file_frontmatter, options)
cells = evaluate_raw_cells!(f, raw_chunks, merged_frontmatter; showprogress)
merged_options = _extract_relevant_options(file_frontmatter, options)
cells = evaluate_raw_cells!(f, raw_chunks, merged_options; showprogress)
data = (
metadata = (
kernelspec = (
Expand All @@ -105,7 +105,7 @@ function evaluate!(
end
end

function _extract_relevant_frontmatter(file_frontmatter::Dict, options::Dict)
function _extract_relevant_options(file_frontmatter::Dict, options::Dict)
file_frontmatter = _recursive_merge(default_frontmatter(), file_frontmatter)

fig_width_default = get(file_frontmatter, "fig-width", nothing)
Expand All @@ -118,7 +118,7 @@ function _extract_relevant_frontmatter(file_frontmatter::Dict, options::Dict)
julia_default = get(file_frontmatter, "julia", nothing)

if isempty(options)
return _frontmatter_template(;
return _options_template(;
fig_width = fig_width_default,
fig_height = fig_height_default,
fig_format = fig_format_default,
Expand All @@ -142,7 +142,7 @@ function _extract_relevant_frontmatter(file_frontmatter::Dict, options::Dict)
metadata = get(D, format, "metadata")
julia = get(metadata, "julia", julia_default)

return _frontmatter_template(;
return _options_template(;
fig_width,
fig_height,
fig_format,
Expand All @@ -153,7 +153,7 @@ function _extract_relevant_frontmatter(file_frontmatter::Dict, options::Dict)
end
end

function _frontmatter_template(;
function _options_template(;
fig_width,
fig_height,
fig_format,
Expand Down Expand Up @@ -471,10 +471,10 @@ in all available mimetypes.
function evaluate_raw_cells!(
f::File,
chunks::Vector,
frontmatter::Dict;
options::Dict;
showprogress = true,
)
refresh!(f, frontmatter)
refresh!(f, options)
cells = []
@maybe_progress showprogress "Running $(relpath(f.path, pwd()))" for (nth, chunk) in
enumerate(chunks)
Expand Down
34 changes: 17 additions & 17 deletions src/worker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ function worker_init(f::File)

const PROJECT = Base.active_project()
const WORKSPACE = Ref(Module(:Notebook))
const FRONTMATTER = Ref($(default_frontmatter()))
const OPTIONS = Ref(Dict{String,Any}())

# Interface:

function refresh!(frontmatter = FRONTMATTER[])
function refresh!(options = OPTIONS[])
# Current directory should always start out as the directory of the
# notebook file, which is not necessarily right initially if the parent
# process was started from a different directory to the notebook.
Expand Down Expand Up @@ -66,16 +66,16 @@ function worker_init(f::File)
# can immediately activate a project environment if they want to.
Core.eval(WORKSPACE[], :(import Main: Pkg, ojs_define))

# Rerun the package loading hooks if the frontmatter has changed.
if FRONTMATTER[] != frontmatter
FRONTMATTER[] = frontmatter
# Rerun the package loading hooks if the options have changed.
if OPTIONS[] != options
OPTIONS[] = options
for (pkgid, hook) in PACKAGE_LOADING_HOOKS
if haskey(Base.loaded_modules, pkgid)
hook()
end
end
else
FRONTMATTER[] = frontmatter
OPTIONS[] = options
end

return nothing
Expand Down Expand Up @@ -193,7 +193,7 @@ function worker_init(f::File)
#
# TODO: perhaps preprocess the metadata provided here rather
# than just passing it through as-is.
:QuartoNotebookRunner => (; cell_options, frontmatter = FRONTMATTER[]),
:QuartoNotebookRunner => (; cell_options, options = OPTIONS[]),
)
end

Expand Down Expand Up @@ -222,7 +222,7 @@ function worker_init(f::File)
end

function render_mimetypes(value, cell_options)
to_format = FRONTMATTER[]["format"]["pandoc"]["to"]
to_format = OPTIONS[]["format"]["pandoc"]["to"]

result = Dict{String,@NamedTuple{error::Bool, data::Vector{UInt8}}}()
# Some output formats that we want to write to need different
Expand Down Expand Up @@ -359,13 +359,13 @@ function worker_init(f::File)
end
end

function _frontmatter()
fm = FRONTMATTER[]
function _figure_metadata()
options = OPTIONS[]

fig_width_inch = fm["format"]["execute"]["fig-width"]
fig_height_inch = fm["format"]["execute"]["fig-height"]
fig_format = fm["format"]["execute"]["fig-format"]
fig_dpi = fm["format"]["execute"]["fig-dpi"]
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"]

if fig_format == "retina"
fig_format = "svg"
Expand All @@ -392,7 +392,7 @@ function worker_init(f::File)
end

function _CairoMakie_hook(pkgid::Base.PkgId, CairoMakie::Module)
fm = _frontmatter()
fm = _figure_metadata()
if fm.fig_dpi !== nothing
kwargs = Dict{Symbol,Any}(
:px_per_unit => fm.fig_dpi / 96,
Expand All @@ -410,7 +410,7 @@ function worker_init(f::File)
_CairoMakie_hook(::Any...) = nothing

function _Makie_hook(pkgid::Base.PkgId, Makie::Module)
fm = _frontmatter()
fm = _figure_metadata()
# only change Makie theme if sizes are set, if only one is set, pick an aspect ratio of 4/3
# which might be more user-friendly than throwing an error
if fm.fig_width_inch !== nothing || fm.fig_height_inch !== nothing
Expand All @@ -436,7 +436,7 @@ function worker_init(f::File)
_Makie_hook(::Any...) = nothing

function _Plots_hook(pkgid::Base.PkgId, Plots::Module)
fm = _frontmatter()
fm = _figure_metadata()
# Convert inches to CSS pixels or device-independent pixels.
# Empirically, an SVG is saved by Plots with width and height taken directly as CSS pixels (without unit specified)
# so the conversion with the 96 factor would be correct in that setting.
Expand Down

0 comments on commit e14508f

Please sign in to comment.