From a7460069020770d3c9e94a34e7600febdf089e1d Mon Sep 17 00:00:00 2001 From: Charles Teague Date: Fri, 13 Oct 2023 21:39:41 -0400 Subject: [PATCH] Improve table embed support (progress on #7210) --- .../filters/customnodes/floatreftarget.lua | 9 +++-- src/resources/filters/quarto-post/ipynb.lua | 22 +++++++++++ .../smoke-all/embed/tables/inset-table.qmd | 37 +++++++++++++++++++ tests/docs/smoke-all/embed/tables/parent.qmd | 9 +++++ 4 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 tests/docs/smoke-all/embed/tables/inset-table.qmd create mode 100644 tests/docs/smoke-all/embed/tables/parent.qmd diff --git a/src/resources/filters/customnodes/floatreftarget.lua b/src/resources/filters/customnodes/floatreftarget.lua index 61f14f03ed..7c7e0f1689 100644 --- a/src/resources/filters/customnodes/floatreftarget.lua +++ b/src/resources/filters/customnodes/floatreftarget.lua @@ -615,14 +615,15 @@ end, function(float) end return pandoc.Para({imgEl}) elseif float.in_code_cell_output then + -- If the float is in a code_cell_output, it is ok to drop the identifier -- and caption, because that infdormation is still carried by the cell itself return float.content + else - return pandoc.Figure( - {float.content}, - {float.caption_long}, - float.identifier) + -- TODO: Consider just putting together list of paragraphs + -- + return float.content end end) diff --git a/src/resources/filters/quarto-post/ipynb.lua b/src/resources/filters/quarto-post/ipynb.lua index a831e5fa64..044c03b23b 100644 --- a/src/resources/filters/quarto-post/ipynb.lua +++ b/src/resources/filters/quarto-post/ipynb.lua @@ -85,10 +85,32 @@ function ipynb() -- if we are in source notebook mode, we need to omit identifiers that appear on images -- and instead allow the cell yaml to declare things like ids if produceSourceNotebook and el.attr.classes:includes('cell-output-display') then + + -- First, we need to collapse tables that are surrounded by + -- raw blocks (which specifically addresses tables that are parsed and + -- surrounded by rawblocks to contain unparseable content) + -- This will catch GT tables and render HTML and markdown versions. + if #el.content == 3 and el.content[1].t == "RawBlock" and el.content[2].t == "Table" and el.content[3].t == "RawBlock" then + if el.content[1].format == "html" and el.content[3].format == "html" then + + local tbl = pandoc.Pandoc(el.content[2]) + local htmlRenderedTbl = pandoc.write(tbl, "html") + local htmlRawBlock = pandoc.RawBlock("html", el.content[1].text .. htmlRenderedTbl .. el.content[3].text) + + local mdRenderedTbl = pandoc.write(tbl, "markdown") + local mdRawBlock = pandoc.RawBlock("markdown", mdRenderedTbl) + + el.content = pandoc.Blocks({htmlRawBlock, mdRawBlock}) + end + end + el = _quarto.ast.walk(el, { Image = function(imgEl) imgEl.attr = pandoc.Attr() return imgEl + end, + FloatRefTarget = function(float) + end, Table = function(tbl) local rendered = pandoc.write(pandoc.Pandoc(tbl), "markdown") diff --git a/tests/docs/smoke-all/embed/tables/inset-table.qmd b/tests/docs/smoke-all/embed/tables/inset-table.qmd new file mode 100644 index 0000000000..9af2783e44 --- /dev/null +++ b/tests/docs/smoke-all/embed/tables/inset-table.qmd @@ -0,0 +1,37 @@ +--- +title: "Notebook" +format: + ipynb: + notebook-preserve-cells: true + produce-source-notebook: true + citeproc: false +--- + +```{r} +#| label: tbl-one +#| tbl-cap: This is a kable table + +library(knitr) +kable(head(mtcars)) +``` + +```{r} +#| label: tbl-two +#| tbl-cap: This is a gt table + +library(gt) +gt(head(mtcars)) +``` + + +```{r} +#| label: tbl-three +#| tbl-cap: This is a flextable table + +library(flextable) +flextable(head(mtcars)) +``` + + + + diff --git a/tests/docs/smoke-all/embed/tables/parent.qmd b/tests/docs/smoke-all/embed/tables/parent.qmd new file mode 100644 index 0000000000..07f518402c --- /dev/null +++ b/tests/docs/smoke-all/embed/tables/parent.qmd @@ -0,0 +1,9 @@ +--- +title: "Test of embedding tables" +format: + html: + toc: true +--- + +{{< embed inset-table.qmd#tbl-three >}} +