Skip to content
Open
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
54 changes: 37 additions & 17 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,17 @@
# so to render an R formatted cell, we need to do a workaround. We push a cell before
# the actual code cell which contains a plain markdown block that wraps the code in ```r
# for the formatting.
opt_echo = get(extract_cell_options(chunk.source; chunk.file, chunk.line), "echo", "true")
source_echoed = ""
if opt_echo == "true" || opt_echo == true || opt_echo == ""
source_echoed = """```python\n$(strip_cell_options(chunk.source))```"""
elseif opt_echo == "fenced"
source_echoed = """```{{python}}\n$(strip_cell_options_echo(chunk.source))```"""
elseif !(opt_echo == "false" || opt_echo == false)
msg = """Error parsing cell attribute "echo" at $(chunk.file):$(chunk.line):\n\n""" *

Check warning on line 1058 in src/server.jl

View check run for this annotation

Codecov / codecov/patch

src/server.jl#L1055-L1058

Added lines #L1055 - L1058 were not covered by tests
"""```{python}\n$(chunk.source)\n```"""
error(msg)

Check warning on line 1060 in src/server.jl

View check run for this annotation

Codecov / codecov/patch

src/server.jl#L1060

Added line #L1060 was not covered by tests
end
push!(
cells,
(;
Expand All @@ -1057,14 +1068,7 @@
),
cell_type = :markdown,
metadata = (;),
source = process_cell_source(
"""
```python
$(strip_cell_options(chunk.source))
```
""",
Dict(),
),
source = process_cell_source(source_echoed, Dict()),
),
)
# We also need to hide the real code cell in this case, which contains possible formatting
Expand All @@ -1077,6 +1081,17 @@
# so to render an R formatted cell, we need to do a workaround. We push a cell before
# the actual code cell which contains a plain markdown block that wraps the code in ```r
# for the formatting.
opt_echo = get(extract_cell_options(chunk.source; chunk.file, chunk.line), "echo", "true")
source_echoed = ""
if opt_echo == "true" || opt_echo == true || opt_echo == ""
source_echoed = """```r\n$(strip_cell_options(chunk.source))```"""
elseif opt_echo == "fenced"
source_echoed = """```{{r}}\n$(strip_cell_options_echo(chunk.source))```"""
elseif !(opt_echo == "false" || opt_echo == false)
msg = """Error parsing cell attribute "echo" at $(chunk.file):$(chunk.line):\n\n""" *

Check warning on line 1091 in src/server.jl

View check run for this annotation

Codecov / codecov/patch

src/server.jl#L1088-L1091

Added lines #L1088 - L1091 were not covered by tests
"""```{r}\n$(chunk.source)\n```"""
error(msg)

Check warning on line 1093 in src/server.jl

View check run for this annotation

Codecov / codecov/patch

src/server.jl#L1093

Added line #L1093 was not covered by tests
end
push!(
cells,
(;
Expand All @@ -1086,14 +1101,7 @@
),
cell_type = :markdown,
metadata = (;),
source = process_cell_source(
"""
```r
$(strip_cell_options(chunk.source))
```
""",
Dict(),
),
source = process_cell_source(source_echoed, Dict()),
),
)
# We also need to hide the real code cell in this case, which contains possible formatting
Expand Down Expand Up @@ -1211,7 +1219,11 @@
# are written into the processed cell source when the cell is the result of an
# expansion of an `expand` cell.
function process_cell_source(source::AbstractString, cell_options::Dict = Dict())
lines = collect(eachline(IOBuffer(source); keep = true))
if haskey(cell_options, "echo")
lines = collect(eachline(IOBuffer(strip_cell_options_echo(source)), keep = true))
else
lines = collect(eachline(IOBuffer(source); keep = true))
end
if !isempty(lines)
lines[end] = rstrip(lines[end])
end
Expand All @@ -1234,6 +1246,14 @@
join(lines[keep_from:end])
end

function strip_cell_options_echo(source::AbstractString)
lines = collect(eachline(IOBuffer(source); keep = true))
keep_from = something(findfirst(lines) do line
!startswith(line, r"#\|[\t ]+echo[\t ]*:")
end, 1)
join(lines[keep_from:end])
end

function wrap_with_r_boilerplate(code)
"""
@isdefined(RCall) && RCall isa Module && Base.PkgId(RCall).uuid == Base.UUID("6f49c342-dc21-5d91-9882-a32aef131414") || error("RCall must be imported to execute R code cells with QuartoNotebookRunner")
Expand Down
Loading