diff --git a/news/changelog-1.6.md b/news/changelog-1.6.md index a12d001ae75..47ebf7c209a 100644 --- a/news/changelog-1.6.md +++ b/news/changelog-1.6.md @@ -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. diff --git a/src/resources/filters/normalize/flags.lua b/src/resources/filters/normalize/flags.lua index a57a00e648f..8815da0a2fa 100644 --- a/src/resources/filters/normalize/flags.lua +++ b/src/resources/filters/normalize/flags.lua @@ -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 diff --git a/src/resources/filters/quarto-pre/table-captions.lua b/src/resources/filters/quarto-pre/table-captions.lua index ef50a29a01e..ede527f83e0 100644 --- a/src/resources/filters/quarto-pre/table-captions.lua +++ b/src/resources/filters/quarto-pre/table-captions.lua @@ -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 } @@ -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 diff --git a/tests/docs/smoke-all/2024/10/23/issue-8428.qmd b/tests/docs/smoke-all/2024/10/23/issue-8428.qmd new file mode 100644 index 00000000000..6b60e83372c --- /dev/null +++ b/tests/docs/smoke-all/2024/10/23/issue-8428.qmd @@ -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 | +::: +::: + +