Skip to content
Open
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
30 changes: 26 additions & 4 deletions src/QuartoNotebookWorker/src/refresh.jl
Original file line number Diff line number Diff line change
@@ -1,9 +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))
task_local_storage()[:SOURCE_PATH] = 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 == "file"
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?
# We can't rely on `pwd`, because the notebook can change that.
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
error("Quarto only accepts `file` or `project` as arguments to `execute-dir`, got `$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.
Expand Down
26 changes: 26 additions & 0 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,18 @@ 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}())

# Get project from execute section in frontmatter
execute_frontmatter = get(file_frontmatter, "execute", Dict{String,Any}())
project_default = get(execute_frontmatter, "project", Dict{String,Any}())

# Validate execute-dir from frontmatter if present
if haskey(project_default, "execute-dir")
execute_dir = project_default["execute-dir"]
if !(execute_dir in ["file", "project"])
Base.error("Invalid execute-dir value: '$execute_dir'. Quarto only accepts 'file' or 'project'.")
end
end

if isempty(options)
return _options_template(;
Expand All @@ -441,6 +453,7 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict)
daemon = daemon_default,
params = params_default,
cache = cache_default,
project = project_default,
)
else
format = get(D, options, "format")
Expand Down Expand Up @@ -469,6 +482,16 @@ 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())

# Validate execute-dir if present
if haskey(project, "execute-dir")
execute_dir = project["execute-dir"]
if !(execute_dir in ["file", "project"])
Base.error("Invalid execute-dir value: '$execute_dir'. Quarto only accepts 'file' or 'project'.")
end
end

return _options_template(;
fig_width,
fig_height,
Expand All @@ -481,6 +504,7 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict)
daemon,
params = params_merged,
cache,
project,
)
end
end
Expand All @@ -497,6 +521,7 @@ function _options_template(;
daemon,
params,
cache,
project,
)
D = Dict{String,Any}
return D(
Expand All @@ -515,6 +540,7 @@ function _options_template(;
"metadata" => D("julia" => julia),
),
"params" => D(params),
"project" => D(project),
)
end

Expand Down
26 changes: 26 additions & 0 deletions test/examples/execute_dir_file.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: "Execute Dir File"
engine: julia
execute:
project:
execute-dir: file
---

```{julia}
# Should be in the same directory as this file
pwd()
```

```{julia}
# Should be able to read marker file in same directory
if isfile("marker_file.txt")
read("marker_file.txt", String)
else
"Marker file not found in current directory"
end
```

```{julia}
# Verify we're in the examples directory
basename(pwd())
```
26 changes: 26 additions & 0 deletions test/examples/execute_dir_project.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: "Execute Dir Project"
engine: julia
execute:
project:
execute-dir: project
---

```{julia}
# Should be in the project root directory
pwd()
```

```{julia}
# Should be able to read project marker file in project root
if isfile("project_marker.txt")
read("project_marker.txt", String)
else
"Project marker file not found in current directory"
end
```

```{julia}
# Check if we can see Project.toml in project root
isfile("Project.toml")
```
31 changes: 31 additions & 0 deletions test/examples/subdirectory/execute_dir_nested.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: "Execute Dir Nested"
engine: julia
execute:
project:
execute-dir: file
---

```{julia}
# Should be in the subdirectory folder
pwd()
```

```{julia}
# Should see subdirectory as basename
basename(pwd())
```

```{julia}
# Should be able to read marker file in subdirectory
if isfile("subdir_marker.txt")
read("subdir_marker.txt", String)
else
"Subdirectory marker file not found"
end
```

```{julia}
# Should NOT be able to see parent directory marker without ../
!isfile("marker_file.txt") && isfile("../marker_file.txt")
```
Loading
Loading