From 4ff5f5693d1595780fffc7cb39188c16dd1ffda1 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 18 Sep 2023 07:41:45 -0500 Subject: [PATCH 1/4] fix complex preamble layout in jats, asciidoc, add tests --- src/resources/filters/layout/asciidoc.lua | 10 ++- src/resources/filters/layout/jats.lua | 12 +++- tests/docs/smoke-all/2023/09/18/6871-adoc.md | 66 ++++++++++++++++++ tests/docs/smoke-all/2023/09/18/6871.md | 72 ++++++++++++++++++++ 4 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 tests/docs/smoke-all/2023/09/18/6871-adoc.md create mode 100644 tests/docs/smoke-all/2023/09/18/6871.md diff --git a/src/resources/filters/layout/asciidoc.lua b/src/resources/filters/layout/asciidoc.lua index da509a67ff5..16acd30fdee 100644 --- a/src/resources/filters/layout/asciidoc.lua +++ b/src/resources/filters/layout/asciidoc.lua @@ -116,9 +116,15 @@ end, function(layout) local content_str = pandoc.write(_quarto.ast.walk(pandoc.Pandoc(panel_content), render_extended_nodes()) or {}, "asciidoc") local figure_str = ". " .. caption_str .. "[#" .. layout.identifier .. "]\n" .. content_str - if layout.preamble then + local pt = pandoc.utils.type(layout.preamble) + if pt == "Blocks" then + layout.preamble:insert(pandoc.RawBlock("asciidoc", figure_str)) + return layout.preamble + elseif pt == "Block" then return pandoc.Blocks({ layout.preamble, pandoc.RawBlock("asciidoc", figure_str) }) - else + elseif pt == "nil" then return pandoc.RawBlock("asciidoc", figure_str) + else + internal_error() end end) \ No newline at end of file diff --git a/src/resources/filters/layout/jats.lua b/src/resources/filters/layout/jats.lua index 8c5684e960e..99c0f2129b1 100644 --- a/src/resources/filters/layout/jats.lua +++ b/src/resources/filters/layout/jats.lua @@ -103,9 +103,15 @@ end, function(layout) decorate_caption_with_crossref(layout.float) local result = pandoc.Figure(panel_content, {layout.float.caption_long}, attr) - if layout.preamble then - return pandoc.Blocks({ layout.preamble, result }) + local pt = pandoc.utils.type(layout.preamble) + if pt == "Blocks" then + layout.preamble:insert(figure) + return layout.preamble + elseif pt == "Block" then + return pandoc.Blocks({ layout.preamble, figure }) + elseif pt == "nil" then + return figure else - return result + internal_error() end end) \ No newline at end of file diff --git a/tests/docs/smoke-all/2023/09/18/6871-adoc.md b/tests/docs/smoke-all/2023/09/18/6871-adoc.md new file mode 100644 index 00000000000..8f27f62c8f5 --- /dev/null +++ b/tests/docs/smoke-all/2023/09/18/6871-adoc.md @@ -0,0 +1,66 @@ +--- + +title: Knitr Table Test +format: asciidoc +keep-md: true +\_quarto: +tests: +asciidoc: +ensureFileRegexMatches: # The checks here are looser than I'd like, because checking for asciidoc # nested structures in line-by-line regexes seems impossible, but this # is the only smoke-all test we can run in asciidoc.. # # TODO this is a problem. - - "\\[\\[tbl-cars\\]\\]" - "\\[\\[tbl-pressure\\]\\]" - "\\[#tbl-tables\\]" # TODO understand the difference between [[foo]] and [#foo]. From my reading of the asciidoc docs it seems ok but we should double-check. - "\\. Tables" - "\\. Cars" - "\\. Pressure" - [] + +--- + +::: {#tbl-tables .cell layout-ncol="2" tbl-cap='Tables'} + +```{.r .cell-code} +library(knitr) +``` + +::: {.cell-output .cell-output-stderr} + +``` +Warning: le package 'knitr' a été compilé avec la version R 4.2.3 +``` + +::: + +```{.r .cell-code} +kable(head(cars), caption = "Cars {#tbl-cars}") +``` + +::: {.cell-output-display} + +Table: Cars {#tbl-cars} + +| speed | dist | +| ----: | ---: | +| 4 | 2 | +| 4 | 10 | +| 7 | 4 | +| 7 | 22 | +| 8 | 16 | +| 9 | 10 | + +::: + +```{.r .cell-code} +kable(head(pressure), caption = "Pressure {#tbl-pressure}") +``` + +::: {.cell-output-display} + +Table: Pressure {#tbl-pressure} + +| temperature | pressure | +| ----------: | -------: | +| 0 | 0.0002 | +| 20 | 0.0012 | +| 40 | 0.0060 | +| 60 | 0.0300 | +| 80 | 0.0900 | +| 100 | 0.2700 | + +::: +::: + +See @tbl-cars for more information. diff --git a/tests/docs/smoke-all/2023/09/18/6871.md b/tests/docs/smoke-all/2023/09/18/6871.md new file mode 100644 index 00000000000..88c8fab3cbd --- /dev/null +++ b/tests/docs/smoke-all/2023/09/18/6871.md @@ -0,0 +1,72 @@ +--- +title: Knitr Table Test +format: jats +keep-md: true +_quarto: + tests: + jats: + ensureJatsXpath: + - - '//fig//fig[@id="tbl-cars"]//table' + - '//fig//fig[@id="tbl-cars"]//caption' + - '//fig//fig[@id="tbl-pressure"]//table' + - '//fig//fig[@id="tbl-pressure"]//caption' + - '//fig[@id="tbl-tables"]/caption' + - [] +--- + +::: {#tbl-tables .cell layout-ncol="2" tbl-cap='Tables'} + +```{.r .cell-code} +library(knitr) +``` + +::: {.cell-output .cell-output-stderr} + +``` +Warning: le package 'knitr' a été compilé avec la version R 4.2.3 +``` + +::: + +```{.r .cell-code} +kable(head(cars), caption = "Cars {#tbl-cars}") +``` + +::: {.cell-output-display} + +Table: Cars {#tbl-cars} + +| speed | dist | +| ----: | ---: | +| 4 | 2 | +| 4 | 10 | +| 7 | 4 | +| 7 | 22 | +| 8 | 16 | +| 9 | 10 | + +::: + +```{.r .cell-code} +kable(head(pressure), caption = "Pressure {#tbl-pressure}") +``` + +::: {.cell-output-display} + +Table: Pressure {#tbl-pressure} + +| temperature | pressure | +| ----------: | -------: | +| 0 | 0.0002 | +| 20 | 0.0012 | +| 40 | 0.0060 | +| 60 | 0.0300 | +| 80 | 0.0900 | +| 100 | 0.2700 | + +::: +::: + +See @tbl-cars for more information. + + From b3fccfba912569a91455c3424683f35247446aba Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 18 Sep 2023 07:49:09 -0500 Subject: [PATCH 2/4] don't fail if parsed table has no captions. Closes #6878 --- .../filters/quarto-pre/parsefiguredivs.lua | 3 - tests/docs/smoke-all/2023/09/18/6873.md | 534 ++++++++++++++++++ 2 files changed, 534 insertions(+), 3 deletions(-) create mode 100644 tests/docs/smoke-all/2023/09/18/6873.md diff --git a/src/resources/filters/quarto-pre/parsefiguredivs.lua b/src/resources/filters/quarto-pre/parsefiguredivs.lua index f96fbe68366..56a7c0c2f1f 100644 --- a/src/resources/filters/quarto-pre/parsefiguredivs.lua +++ b/src/resources/filters/quarto-pre/parsefiguredivs.lua @@ -116,12 +116,9 @@ function parse_floats() end end - -- luacov: disable if caption == nil then - internal_error() return nil end - -- luacov: enable local identifier = div.identifier local attr = pandoc.Attr(identifier, div.classes, div.attributes) diff --git a/tests/docs/smoke-all/2023/09/18/6873.md b/tests/docs/smoke-all/2023/09/18/6873.md new file mode 100644 index 00000000000..b6eadb85dbf --- /dev/null +++ b/tests/docs/smoke-all/2023/09/18/6873.md @@ -0,0 +1,534 @@ +--- +title: "table" +format: html +keep-md: true +--- + + +::: {#tbl-unetable .cell} + +```{.r .cell-code} +library(gt) +start_date <- "2010-06-07" +end_date <- "2010-06-14" +sp500 |> + dplyr::filter(date >= start_date & date <= end_date) |> + dplyr::select(-adj_close) |> + gt() |> + tab_header( + title = "S&P 500", + subtitle = glue::glue("{start_date} to {end_date}") + ) |> + fmt_currency() |> + fmt_date(columns = date, date_style = "wd_m_day_year") |> + fmt_number(columns = volume, suffixing = TRUE) +``` + +::: {.cell-output-display} + + +```{=html} +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
S&P 500
2010-06-07 to 2010-06-14
dateopenhighlowclosevolume
Mon, Jun 14, 2010$1,095.00$1,105.91$1,089.03$1,089.634.43B
Fri, Jun 11, 2010$1,082.65$1,092.25$1,077.12$1,091.604.06B
Thu, Jun 10, 2010$1,058.77$1,087.85$1,058.77$1,086.845.14B
Wed, Jun 9, 2010$1,062.75$1,077.74$1,052.25$1,055.695.98B
Tue, Jun 8, 2010$1,050.81$1,063.15$1,042.17$1,062.006.19B
Mon, Jun 7, 2010$1,065.84$1,071.36$1,049.86$1,050.475.47B
+
+``` + + +::: +::: \ No newline at end of file From cfb13a08ea74544cacb695a7921e1aab3f1cc047 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 18 Sep 2023 09:46:19 -0500 Subject: [PATCH 3/4] instrument test failure to check on CI --- tests/verify.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/verify.ts b/tests/verify.ts index 433066e9fe6..fd3d0066a29 100644 --- a/tests/verify.ts +++ b/tests/verify.ts @@ -344,7 +344,7 @@ const xmlChecker = ( (Array.isArray(xpathResult) && xpathResult.length > 0); assert( passes, - `Required XPath selector ${selector} returned empty array.`, + `Required XPath selector ${selector} returned empty array. Failing document follows:\n\n${xmlText}}`, ); } for (const falseSelector of noMatchSelectors ?? []) { @@ -353,7 +353,7 @@ const xmlChecker = ( (Array.isArray(xpathResult) && xpathResult.length > 0); assert( !passes, - `Illegal XPath selector ${falseSelector} returned non-empty array.`, + `Illegal XPath selector ${falseSelector} returned non-empty array. Failing document follows:\n\n${xmlText}}`, ); } return Promise.resolve(); From 4130baab7772d1ccf666021c27b1c300fa3affff Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 18 Sep 2023 13:15:51 -0500 Subject: [PATCH 4/4] use figure instead of result --- src/resources/filters/layout/jats.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/resources/filters/layout/jats.lua b/src/resources/filters/layout/jats.lua index 99c0f2129b1..aa753f2f0ec 100644 --- a/src/resources/filters/layout/jats.lua +++ b/src/resources/filters/layout/jats.lua @@ -105,12 +105,12 @@ end, function(layout) local pt = pandoc.utils.type(layout.preamble) if pt == "Blocks" then - layout.preamble:insert(figure) + layout.preamble:insert(result) return layout.preamble elseif pt == "Block" then - return pandoc.Blocks({ layout.preamble, figure }) + return pandoc.Blocks({ layout.preamble, result }) elseif pt == "nil" then - return figure + return result else internal_error() end