Skip to content
Merged
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
1 change: 1 addition & 0 deletions news/changelog-1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All changes included in 1.6:
## Lua Filters and extensions

- ([#8179](https://github.com/quarto-dev/quarto-cli/issues/8179)): When merging code cells for complex layouts, do not merge cells with different languages.
- ([#8428](https://github.com/quarto-dev/quarto-cli/issues/8428)): only forward cell labels to tables when tables will be cross-referenceable.
- ([#10004](https://github.com/quarto-dev/quarto-cli/issues/10004)): Resolve callout titles, theorem names, and `code-summary` content through `quarto_ast_pipeline()` and `process_shortcodes()`.
- ([#10196](https://github.com/quarto-dev/quarto-cli/issues/10196)): Protect against nil values in `float.caption_long`.
- ([#10328](https://github.com/quarto-dev/quarto-cli/issues/10328)): Interpret subcells as subfloats when subcap count matches subcell count.
Expand Down
1 change: 1 addition & 0 deletions src/resources/filters/normalize/flags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function compute_flags()

-- FIXME: are we actually triggering this with FloatRefTargets?
-- table captions
local kTblCap = "tbl-cap"
local tblCap = extractTblCapAttrib(node,kTblCap)
if hasTableRef(node) or tblCap then
flags.has_table_captions = true
Expand Down
104 changes: 51 additions & 53 deletions src/resources/filters/quarto-pre/table-captions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,64 @@

local patterns = require("modules/patterns")

kTblCap = "tbl-cap"
kTblSubCap = "tbl-subcap"

function table_captions()
return {
function table_captions()
local kTblCap = "tbl-cap"
local kTblSubCap = "tbl-subcap"
return {
Div = function(el)
if tcontains(el.attr.classes, "cell") then
-- extract table attributes
local tblCap = extractTblCapAttrib(el,kTblCap)
local tblCap = extractTblCapAttrib(el, kTblCap)
local tblSubCap = extractTblCapAttrib(el, kTblSubCap, true)
if hasTableRef(el) or tblCap then
local tables = countTables(el)
if tables > 0 then

-- apply captions and labels if we have a tbl-cap or tbl-subcap
if tblCap or tblSubCap then

-- special case: knitr::kable will generate a \begin{tablular} without
-- a \begin{table} wrapper -- put the wrapper in here if need be
if _quarto.format.isLatexOutput() then
el = _quarto.ast.walk(el, {
RawBlock = function(raw)
if _quarto.format.isRawLatex(raw) then
local tabular_match = _quarto.modules.patterns.match_all_in_table(_quarto.patterns.latexTabularPattern)
local table_match = _quarto.modules.patterns.match_all_in_table(_quarto.patterns.latexTablePattern)
if tabular_match(raw.text) and not table_match(raw.text) then
raw.text = raw.text:gsub(
_quarto.modules.patterns.combine_patterns(_quarto.patterns.latexTabularPattern),
"\\begin{table}\n\\centering\n%1%2%3\n\\end{table}\n",
1)
return raw
end
end
end
})
end

-- compute all captions and labels
local label = el.attr.identifier
local mainCaption, tblCaptions, mainLabel, tblLabels = table_captionsAndLabels(
label,
tables,
tblCap,
tblSubCap
)
-- apply captions and label
el.attr.identifier = mainLabel
if mainCaption then
el.content:insert(pandoc.Para(mainCaption))
end
if #tblCaptions > 0 then
el = applyTableCaptions(el, tblCaptions, tblLabels)
if not (tblCap or hasTableRef(el)) then
return
end
if not (tblCap or tblSubCap) then
return
end
local tables = countTables(el)
if tables <= 0 then
return
end

-- special case: knitr::kable will generate a \begin{tabular} without
-- a \begin{table} wrapper -- put the wrapper in here if need be
if _quarto.format.isLatexOutput() then
el = _quarto.ast.walk(el, {
RawBlock = function(raw)
if _quarto.format.isRawLatex(raw) then
local tabular_match = _quarto.modules.patterns.match_all_in_table(_quarto.patterns.latexTabularPattern)
local table_match = _quarto.modules.patterns.match_all_in_table(_quarto.patterns.latexTablePattern)
if tabular_match(raw.text) and not table_match(raw.text) then
raw.text = raw.text:gsub(
_quarto.modules.patterns.combine_patterns(_quarto.patterns.latexTabularPattern),
"\\begin{table}\n\\centering\n%1%2%3\n\\end{table}\n",
1)
return raw
end
end
return el
end
end
})
end

-- compute all captions and labels
local label = el.attr.identifier
local mainCaption, tblCaptions, mainLabel, tblLabels = table_captionsAndLabels(
label,
tables,
tblCap,
tblSubCap
)
-- apply captions and label
el.attr.identifier = mainLabel
if mainCaption then
el.content:insert(pandoc.Para(mainCaption))
end
if #tblCaptions > 0 then
el = applyTableCaptions(el, tblCaptions, tblLabels)
end
return el
end


end
}

Expand Down Expand Up @@ -138,7 +136,7 @@ function applyTableCaptions(el, tblCaptions, tblLabels)
cap:extend(tblCaptions[idx])
cap:insert(pandoc.Space())
end
if #tblLabels[idx] > 0 then
if #tblLabels[idx] > 0 and tblLabels[idx]:match("^tbl%-") then
cap:insert(pandoc.Str("{#" .. tblLabels[idx] .. "}"))
end
idx = idx + 1
Expand Down
32 changes: 32 additions & 0 deletions tests/docs/smoke-all/2024/10/23/issue-8428.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: "Test"
format: html
keep-md: true
_quarto:
tests:
html:
ensureFileRegexMatches:
- []
- ['{#test}']
---


::: {#test .cell tbl-cap='Test caption' execution_count=1}
``` {.python .cell-code}
import pandas as pd
from IPython.display import display, Markdown
penguins = pd.read_csv("https://pos.it/palmer-penguins-github-csv")
markdown_result = penguins.groupby("species").size().to_markdown(index=False, tablefmt="pipe", stralign="right")
display(Markdown(markdown_result))
```

::: {.cell-output .cell-output-display .cell-output-markdown}
| 0 |
|----:|
| 152 |
| 68 |
| 124 |
:::
:::


Loading