From 2304803bb13ce752e1ce0a70f791b5c57321a723 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Thu, 19 Sep 2024 09:35:14 -0700 Subject: [PATCH 1/4] Pick up `project` options from frontmatter --- src/server.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/server.jl b/src/server.jl index f881d75..23ee60b 100644 --- a/src/server.jl +++ b/src/server.jl @@ -178,6 +178,7 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict) julia_default = get(file_frontmatter, "julia", nothing) params_default = get(file_frontmatter, "params", Dict{String,Any}()) + project_default = get(file_frontmatter, "project", Dict{String,Any}()) if isempty(options) return _options_template(; @@ -191,6 +192,7 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict) julia = julia_default, daemon = daemon_default, params = params_default, + project = project_default, ) else format = get(D, options, "format") @@ -218,6 +220,8 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict) cli_params = get(options, "params", Dict()) params_merged = _recursive_merge(params_default, params, cli_params) + project = get(metadata, "project", Dict()) + return _options_template(; fig_width, fig_height, @@ -229,6 +233,7 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict) julia = julia_merged, daemon, params = params_merged, + project, ) end end @@ -244,6 +249,7 @@ function _options_template(; julia, daemon, params, + project, ) D = Dict{String,Any} return D( @@ -261,6 +267,7 @@ function _options_template(; "metadata" => D("julia" => julia), ), "params" => D(params), + "project" => D(project), ) end From bde02afec03480ecca759ccd9a4b1ec4c795a725 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Thu, 19 Sep 2024 09:40:22 -0700 Subject: [PATCH 2/4] Apply `execute-dir` logic in `refresh!` --- src/QuartoNotebookWorker/src/refresh.jl | 31 +++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/QuartoNotebookWorker/src/refresh.jl b/src/QuartoNotebookWorker/src/refresh.jl index ca6154e..5d4ad34 100644 --- a/src/QuartoNotebookWorker/src/refresh.jl +++ b/src/QuartoNotebookWorker/src/refresh.jl @@ -1,8 +1,31 @@ function refresh!(path, original_options, options = original_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. - cd(dirname(path)) + # We check the `execute-dir` key in the options, + if haskey(options, "project") && haskey(options["project"], "execute-dir") + ed = options["project"]["execute-dir"] + if ed == "directory" + println("cd(dirname(path))") + cd(dirname(path)) + elseif ed == "project" + # TODO: this doesn't seem right. How does one get the root path of the project here? + # Maybe piggyback on `options` with some ridiculous identifier? + println("cd(NotebookState.PROJECT[])") + if isfile(NotebookState.PROJECT[]) + cd(dirname(NotebookState.PROJECT[])) + elseif isdir(NotebookState.PROJECT[]) + cd(NotebookState.PROJECT[]) + else + @warn "Project path not found: $(NotebookState.PROJECT[])" + end + else + println("cd(abspath(options[\"execute-dir\"])) => cd($(abspath(ed)))") + cd(abspath(ed)) + end + else + # 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. + cd(dirname(path)) + end # Reset back to the original project environment if it happens to # have changed during cell evaluation. From ba3bc992b875c4272731b78eb953b540843d389b Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Thu, 19 Sep 2024 10:19:20 -0700 Subject: [PATCH 3/4] Remove `println`s --- src/QuartoNotebookWorker/src/refresh.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/QuartoNotebookWorker/src/refresh.jl b/src/QuartoNotebookWorker/src/refresh.jl index 5d4ad34..a712e9a 100644 --- a/src/QuartoNotebookWorker/src/refresh.jl +++ b/src/QuartoNotebookWorker/src/refresh.jl @@ -3,12 +3,11 @@ function refresh!(path, original_options, options = original_options) if haskey(options, "project") && haskey(options["project"], "execute-dir") ed = options["project"]["execute-dir"] if ed == "directory" - println("cd(dirname(path))") cd(dirname(path)) elseif ed == "project" # TODO: this doesn't seem right. How does one get the root path of the project here? # Maybe piggyback on `options` with some ridiculous identifier? - println("cd(NotebookState.PROJECT[])") + # We can't rely on `pwd`, because the notebook can change that. if isfile(NotebookState.PROJECT[]) cd(dirname(NotebookState.PROJECT[])) elseif isdir(NotebookState.PROJECT[]) @@ -17,7 +16,6 @@ function refresh!(path, original_options, options = original_options) @warn "Project path not found: $(NotebookState.PROJECT[])" end else - println("cd(abspath(options[\"execute-dir\"])) => cd($(abspath(ed)))") cd(abspath(ed)) end else From 3cffbfb4b827950a0897844bd13147fce4365286 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Thu, 19 Sep 2024 10:19:32 -0700 Subject: [PATCH 4/4] Allow arbitrary directories --- src/QuartoNotebookWorker/src/refresh.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/QuartoNotebookWorker/src/refresh.jl b/src/QuartoNotebookWorker/src/refresh.jl index a712e9a..1e7b28b 100644 --- a/src/QuartoNotebookWorker/src/refresh.jl +++ b/src/QuartoNotebookWorker/src/refresh.jl @@ -12,6 +12,8 @@ function refresh!(path, original_options, options = original_options) cd(dirname(NotebookState.PROJECT[])) elseif isdir(NotebookState.PROJECT[]) cd(NotebookState.PROJECT[]) + elseif isdir(ed) + cd(ed) else @warn "Project path not found: $(NotebookState.PROJECT[])" end