Skip to content

Commit

Permalink
Merge pull request #7976 from quarto-dev/bugfix/6157
Browse files Browse the repository at this point in the history
lua: lift codeblocks and annotations together out of floats
  • Loading branch information
cscheid authored Dec 19, 2023
2 parents 1264faf + 342bf95 commit acf9c60
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/resources/filters/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ local quarto_post_filters = {
filter = cell_cleanup(),
flags = { "has_output_cells" } },
{ name = "post-cites", filter = indexCites() },
{ name = "post-fold-code-and-lift-codeblocks-from-floats", filter = fold_code_and_lift_codeblocks() },
{ name = "post-bibliography", filter = bibliography() },
{ name = "post-ipynb", filters = ipynb()},
{ name = "post-figureCleanupCombined", filter = combineFilters({
Expand Down Expand Up @@ -388,6 +387,7 @@ local quarto_layout_filters = {
{ name = "layout-columns", filter = columns() },
{ name = "layout-cites-preprocess", filter = cites_preprocess() },
{ name = "layout-cites", filter = cites() },
{ name = "post-fold-code-and-lift-codeblocks-from-floats", filter = fold_code_and_lift_codeblocks() },
{ name = "layout-panels", filter = layout_panels() },
}

Expand Down
36 changes: 31 additions & 5 deletions src/resources/filters/quarto-post/foldcode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,48 @@ function fold_code_and_lift_codeblocks()
end

local blocks = pandoc.Blocks({})
local prev_annotated_code_block_scaffold = nil
local prev_annotated_code_block = nil
-- ok to lift codeblocks
float.content = _quarto.ast.walk(float.content, {
traverse = "topdown",
Div = function(div)
other_content_found = true
return nil
end,
DecoratedCodeBlock = function(block)
-- defer the folding of code blocks to the DecoratedCodeBlock renderer
-- so that we can handle filename better
return nil, false
end,
CodeBlock = function(block)
blocks:insert(render_folded_block(block))
print("walking code block")
local folded_block = render_folded_block(block)
if block.classes:includes("code-annotation-code") then
print("found code annotation code block")
prev_annotated_code_block_scaffold = folded_block
prev_annotated_code_block = block
else
prev_annotated_code_block_scaffold = nil
end
blocks:insert(folded_block)
return {}
end,
Div = function(div)
if not div.classes:includes("cell-annotation") then
return nil
end
local need_to_move_dl = false
_quarto.ast.walk(div, {
Span = function(span)
if (prev_annotated_code_block and
prev_annotated_code_block.identifier == span.attributes["data-code-cell"]) then
need_to_move_dl = true
end
end,
})
if need_to_move_dl then
assert(prev_annotated_code_block_scaffold)
prev_annotated_code_block_scaffold.content:insert(div)
return {}
end
end,
})
if #blocks > 0 then
blocks:insert(float_node)
Expand Down
30 changes: 30 additions & 0 deletions tests/docs/smoke-all/2023/12/19/6157.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
format:
html:
code-annotations: hover
_quarto:
tests:
html:
ensureHtmlElements:
- []
- ["div.quarto-layout-row div.cell-annotation"]
---

```{r}
#| echo: true
#| layout-ncol: 2
#| fig-cap: "Caption."
#| fig-subcap:
#| - "Subcaption 1."
#| - "Subcaption 2."
#| fig-align: center
#| label: fig-1
library(ggplot2)
p1 <- ggplot() + # <1>
geom_point(data = mtcars, aes(x = wt, y = mpg))
p1
p1
```

1. Code annotation text.

0 comments on commit acf9c60

Please sign in to comment.