From 36b0ff965f3221d4f86b1d4e494840cca994ccbf Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Wed, 1 Mar 2023 14:15:59 -0500 Subject: [PATCH 1/6] Add `capLocation()` Lua fn for preprocessing --- src/resources/filters/quarto-pre/options.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/resources/filters/quarto-pre/options.lua b/src/resources/filters/quarto-pre/options.lua index b284ef1c081..ff9195ce52e 100644 --- a/src/resources/filters/quarto-pre/options.lua +++ b/src/resources/filters/quarto-pre/options.lua @@ -30,6 +30,15 @@ function var(name, def) end end +function capLocation(scope, default) + local loc = option(scope .. '-cap-location', option('cap-location', nil)) + if loc ~= nil then + return inlinesToString(loc) + else + return default + end +end + function parseOption(name, options, def) local keys = split(name, ".") From adae72c04fabe960b48ba8a0cd19d393bc9079d3 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Wed, 1 Mar 2023 14:19:35 -0500 Subject: [PATCH 2/6] Ensure that figcap is a paragraph in correct order --- src/resources/filters/layout/wp.lua | 64 +++++++++++++---------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/src/resources/filters/layout/wp.lua b/src/resources/filters/layout/wp.lua index e54637378be..3c7e406611b 100644 --- a/src/resources/filters/layout/wp.lua +++ b/src/resources/filters/layout/wp.lua @@ -8,46 +8,40 @@ function tableWpPanel(divEl, layout, caption) }) end - function wpDivFigure(div) - -- options - options = { - pageWidth = wpPageWidth(), - } + local align = figAlignAttribute(div) + local capLoc = capLocation("fig", "bottom") - -- determine divCaption handler (always left-align) - local divCaption = nil - if _quarto.format.isDocxOutput() then - divCaption = docxDivCaption - elseif _quarto.format.isOdtOutput() then - divCaption = odtDivCaption - end - if divCaption then - options.divCaption = function(el, align) return divCaption(el, "left") end - end + local captionPara = div.content[2]:clone() + local figurePara = div.content[1]:clone() - -- get alignment - local align = figAlignAttribute(div) - - -- create the row/cell for the figure - local row = pandoc.List() - local cell = div:clone() - transferImageWidthToCell(div, cell) - row:insert(tableCellContent(cell, align, options)) - - -- make the table - local figureTable = pandoc.SimpleTable( - pandoc.List(), -- caption - { layoutTableAlign(align) }, - { 1 }, -- full width - pandoc.List(), -- no headers - { row } -- figure - ) - - -- return it - return pandoc.utils.from_simple_table(figureTable) + -- Switch to modern alignment directives for OOXML + local wordAligns = { + left = "start", + right = "end", + center = "center" + } + + -- Generate raw OOXML string that sets paragraph properties + local docxAlign = "" + captionPara.content:insert(1, pandoc.RawInline("openxml", docxAlign)) + + if capLoc == "top" then + + return pandoc.Div({ + captionPara, + figurePara + }) + + else + -- "bottom" or default + return pandoc.Div({ + figurePara, + captionPara + }) + end end function wpPageWidth() From 07af7643f8a684e8b69771db5ef6c5237f9a4163 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Wed, 1 Mar 2023 14:19:54 -0500 Subject: [PATCH 3/6] Add .qmd file to `smoke-all` --- tests/docs/smoke-all/2023/03/01/4004.qmd | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/docs/smoke-all/2023/03/01/4004.qmd diff --git a/tests/docs/smoke-all/2023/03/01/4004.qmd b/tests/docs/smoke-all/2023/03/01/4004.qmd new file mode 100644 index 00000000000..3724022232c --- /dev/null +++ b/tests/docs/smoke-all/2023/03/01/4004.qmd @@ -0,0 +1,39 @@ +--- +title: "issue 4004" +format: + docx: + fig-cap-location: top +--- + +Lets's see @fig-01. And we'll use different alignments. + +```{r} +#| label: fig-01 +#| fig-cap: The figure caption. +#| fig-align: left +plot(1:10) +``` + +```{r} +#| label: fig-02 +#| fig-cap: The figure caption. +#| fig-align: right +plot(1:10) +``` + +```{r} +#| label: fig-03 +#| fig-cap: The figure caption. +#| fig-align: center +plot(1:10) +``` + +Compare with tables (caption implementation not changed for this issue) + +```{r} +#| label: tbl-01 +#| tbl-cap: The table caption. +library(knitr) + +kable(mtcars[1:2, 1:2]) +``` From 37724a41803c440de3f764b7441ca76e4a18110b Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Wed, 1 Mar 2023 14:29:22 -0500 Subject: [PATCH 4/6] Modify comment --- src/resources/filters/layout/wp.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resources/filters/layout/wp.lua b/src/resources/filters/layout/wp.lua index 3c7e406611b..3bd2b93b706 100644 --- a/src/resources/filters/layout/wp.lua +++ b/src/resources/filters/layout/wp.lua @@ -9,7 +9,7 @@ function tableWpPanel(divEl, layout, caption) end function wpDivFigure(div) - + local align = figAlignAttribute(div) local capLoc = capLocation("fig", "bottom") @@ -23,7 +23,7 @@ function wpDivFigure(div) center = "center" } - -- Generate raw OOXML string that sets paragraph properties + -- Generate a raw OOXML string that sets paragraph properties local docxAlign = "" captionPara.content:insert(1, pandoc.RawInline("openxml", docxAlign)) From f682b915f615c48c28db1ff5ecb9f0f6d6e274c8 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Thu, 2 Mar 2023 10:38:00 -0500 Subject: [PATCH 5/6] Update changelog-1.3.md --- news/changelog-1.3.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/news/changelog-1.3.md b/news/changelog-1.3.md index cd47b761591..5f929128563 100644 --- a/news/changelog-1.3.md +++ b/news/changelog-1.3.md @@ -79,6 +79,10 @@ - Improve the performance of extremely large documents with margin elements by improving the efficiency of positioning the elements. +## Docx Format + +- Ensure that the figure caption and the figure itself is laid out as consecutive paragraphs. ([#4004](https://github.com/quarto-dev/quarto-cli/issues/4004)) + ## Listings - Listings now support `template-params`, which will be passed to custom EJS templates in a variable called `templateParams` when a listing is rendered. From f162dd2fa18261b4b08f7500023f40bef9405f61 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Thu, 2 Mar 2023 16:47:38 -0500 Subject: [PATCH 6/6] Update 4004.qmd --- tests/docs/smoke-all/2023/03/01/4004.qmd | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/docs/smoke-all/2023/03/01/4004.qmd b/tests/docs/smoke-all/2023/03/01/4004.qmd index 3724022232c..880295753d7 100644 --- a/tests/docs/smoke-all/2023/03/01/4004.qmd +++ b/tests/docs/smoke-all/2023/03/01/4004.qmd @@ -3,6 +3,15 @@ title: "issue 4004" format: docx: fig-cap-location: top +_quarto: + tests: + docx: + ensureDocxRegexMatches: + - [ + Figure 1.*?.*?, + Figure 2.*?.*?, + Figure 3.*?.*? + ] --- Lets's see @fig-01. And we'll use different alignments.