From 7b24d3c5472d956bcd54a4b38e7d7a8301d218d8 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 3 Jan 2024 13:28:35 -0700 Subject: [PATCH 1/2] asciidoc - fix captions in floats --- .../filters/customnodes/floatreftarget.lua | 65 +++++++++++++++++-- src/resources/filters/layout/asciidoc.lua | 3 +- src/resources/filters/normalize/parsehtml.lua | 1 - .../filters/quarto-post/foldcode.lua | 1 - tests/docs/smoke-all/2024/01/03/8055-b.qmd | 13 ++++ .../asciidoc-float-caption-formatting-1.qmd | 2 +- .../float/asciidoc/asciidoc-jupyter-2.qmd | 2 +- .../asciidoc-knitr-table-captions-1.qmd | 6 +- .../float/asciidoc/asciidoc-listings-1.qmd | 2 +- .../float/asciidoc/asciidoc-subfloat-1.adoc | 20 ++++++ .../float/asciidoc/asciidoc-subfloat-1.qmd | 2 +- .../float/asciidoc/rawtablecaption.qmd | 2 +- 12 files changed, 101 insertions(+), 18 deletions(-) create mode 100644 tests/docs/smoke-all/2024/01/03/8055-b.qmd create mode 100644 tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.adoc diff --git a/src/resources/filters/customnodes/floatreftarget.lua b/src/resources/filters/customnodes/floatreftarget.lua index e110b3fe9f2..77e0df3a30e 100644 --- a/src/resources/filters/customnodes/floatreftarget.lua +++ b/src/resources/filters/customnodes/floatreftarget.lua @@ -787,14 +787,65 @@ end, function(float) float.identifier) end - float.caption_long.content:insert(1, pandoc.RawInline("asciidoc", ". ")) - float.caption_long.content:insert(pandoc.RawInline("asciidoc", "\n[[" .. float.identifier .. "]]\n====")) - return pandoc.Div({ - float.caption_long, - -- pandoc.RawBlock("asciidoc", "[[" .. float.identifier .. "]]\n====\n"), - float.content, - pandoc.RawBlock("asciidoc", "====\n\n") + if float.type == "Table" and float.content.t == "Table" then + -- special-case the situation where the figure is Table and the content is Table + -- + -- just return the table itself with the caption inside the table + float.content.caption.long = float.caption_long + float.content.attr = pandoc.Attr(float.identifier, float.classes or {}, float.attributes or {}) + return pandoc.Blocks({ + pandoc.RawBlock("asciidoc", "[[" .. float.identifier .. "]]\n"), + float.content + }) + end + + -- if this is a "linked figure Div", render it as such. + local link = quarto.utils.match("Plain/[1]/{Link}/[1]/{Image}")(float.content) + if link then + link[2].identifier = float.identifier + local caption = quarto.utils.as_inlines(float.caption_long) + table.insert(caption, 1, pandoc.RawInline("asciidoc", ".")) + table.insert(caption, pandoc.RawInline("asciidoc", "\n[[" .. float.identifier .. "]]\n")) + table.insert(caption, link[1]) + return caption + end + + -- if the float consists of exactly one image, + -- render it as a pandoc Figure node. + local count = 0 + local img + _quarto.ast.walk(float.content, { + Image = function(node) + count = count + 1 + img = node + end }) + if count == 1 then + print(float.content) + img.identifier = float.identifier + img.caption = quarto.utils.as_inlines(float.caption_long) + return pandoc.Figure( + {img}, + {float.caption_long}, + float.identifier) + end + + -- Fallthrough case, render into a div. + float.caption_long.content:insert(1, pandoc.RawInline("asciidoc", ".")) + float.caption_long.content:insert(pandoc.RawInline("asciidoc", "\n[[" .. float.identifier .. "]]\n====")) + + if pandoc.utils.type(float.content) == "Blocks" then + float.content:insert(1, float.caption_long) + float.content:insert(pandoc.RawBlock("asciidoc", "====\n")) + return float.content + else + return pandoc.Blocks({ + float.caption_long, + -- pandoc.RawBlock("asciidoc", "[[" .. float.identifier .. "]]\n====\n"), + float.content, + pandoc.RawBlock("asciidoc", "====\n\n") + }) + end end) diff --git a/src/resources/filters/layout/asciidoc.lua b/src/resources/filters/layout/asciidoc.lua index 16acd30fdee..f968f4b50d7 100644 --- a/src/resources/filters/layout/asciidoc.lua +++ b/src/resources/filters/layout/asciidoc.lua @@ -110,11 +110,12 @@ end, function(layout) end -- this is exceedingly hacky, but it works. + -- It will fail if the caption contains citations that need to be resolved... local caption_str = pandoc.write(pandoc.Pandoc({layout.float.caption_long}), "asciidoc") -- we need to recurse into render_extended_nodes here, sigh 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 + local figure_str = "." .. caption_str .. "[#" .. layout.identifier .. "]\n" .. content_str local pt = pandoc.utils.type(layout.preamble) if pt == "Blocks" then diff --git a/src/resources/filters/normalize/parsehtml.lua b/src/resources/filters/normalize/parsehtml.lua index 127f34f169c..c9837b01e88 100644 --- a/src/resources/filters/normalize/parsehtml.lua +++ b/src/resources/filters/normalize/parsehtml.lua @@ -32,7 +32,6 @@ function parse_html_tables() local htmlTableProcessing = div.attributes[constants.kHtmlTableProcessing] div.attributes[constants.kHtmlTableProcessing] = nil if htmlTableProcessing == "none" then - quarto.log.output(div.attr == pandoc.Attr()) if div.attr == pandoc.Attr() then -- if no other attributes are set on the div, don't keep it return div.content, false diff --git a/src/resources/filters/quarto-post/foldcode.lua b/src/resources/filters/quarto-post/foldcode.lua index c521b5e22ee..67de3d8b7af 100644 --- a/src/resources/filters/quarto-post/foldcode.lua +++ b/src/resources/filters/quarto-post/foldcode.lua @@ -77,7 +77,6 @@ function fold_code_and_lift_codeblocks() return nil, false end, CodeBlock = function(block) - print("walking code block") local folded_block = render_folded_block(block) if block.classes:includes("code-annotation-code") then print("found code annotation code block") diff --git a/tests/docs/smoke-all/2024/01/03/8055-b.qmd b/tests/docs/smoke-all/2024/01/03/8055-b.qmd new file mode 100644 index 00000000000..7eb820d0ea4 --- /dev/null +++ b/tests/docs/smoke-all/2024/01/03/8055-b.qmd @@ -0,0 +1,13 @@ +--- +title: Caption +format: asciidoc +--- + +::: {#fig-try} + +![Test](surus.png) + +![Test2](surus.png) + +Hello +::: \ No newline at end of file diff --git a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-float-caption-formatting-1.qmd b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-float-caption-formatting-1.qmd index 1875bd14e78..aa91f501746 100644 --- a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-float-caption-formatting-1.qmd +++ b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-float-caption-formatting-1.qmd @@ -6,7 +6,7 @@ _quarto: asciidoc: ensureFileRegexMatches: - - - "\\. Customers _query_" + - "\\.Customers _query_" - "<>" - "\\[\\[lst-customers\\]\\]" --- diff --git a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-jupyter-2.qmd b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-jupyter-2.qmd index 00c861e0954..0713791d2de 100644 --- a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-jupyter-2.qmd +++ b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-jupyter-2.qmd @@ -10,7 +10,7 @@ _quarto: - "\\[#fig-plots\\]" - "\\[#fig-plots-1\\]" - "\\[#fig-plots-2\\]" - - "\\. Plots" + - "\\.Plots" - "\\.Plot 1" - "\\.Plot 2" - [] diff --git a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-knitr-table-captions-1.qmd b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-knitr-table-captions-1.qmd index f18a8f7a582..cb28c0b3989 100644 --- a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-knitr-table-captions-1.qmd +++ b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-knitr-table-captions-1.qmd @@ -14,9 +14,9 @@ _quarto: - "\\[\\[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" + - "\\.Tables" + - "\\.Cars" + - "\\.Pressure" - [] --- diff --git a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-listings-1.qmd b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-listings-1.qmd index 453b7677c3b..3f47a80b9ba 100644 --- a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-listings-1.qmd +++ b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-listings-1.qmd @@ -8,7 +8,7 @@ _quarto: - - "<>" - "\\[\\[lst-customers\\]\\]" - - "\\. Customers Query" + - "\\.Customers Query" --- ```{#lst-customers .sql lst-cap="Customers Query"} diff --git a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.adoc b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.adoc new file mode 100644 index 00000000000..8838bfbaa9f --- /dev/null +++ b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.adoc @@ -0,0 +1,20 @@ += Crossref Test + +== Simple Sub Figure + +.Famous Elephants +[#fig-elephants] +[width="100%",cols="<50%,<50%",] +|=== +a| +.Surus +[#fig-surus] +image::img/surus.jpg[img/surus] + +a| +.Abbas +[#fig-abbas] +image::img/abbas.jpg[img/abbas] + +|=== +See <> for examples. In particular, <> and <>. diff --git a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.qmd b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.qmd index 15ec1759a7c..405be6be56c 100644 --- a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.qmd +++ b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.qmd @@ -8,7 +8,7 @@ _quarto: - - "\\.Surus" - "\\.Abbas" - - "\\. Famous Elephants" # TODO We should try to understand why some captions have spaces in front of them and others don't + - "\\.Famous Elephants" - "\\[#fig-elephants\\]" - "\\[#fig-surus\\]" - "\\[#fig-abbas\\]" diff --git a/tests/docs/smoke-all/crossrefs/float/asciidoc/rawtablecaption.qmd b/tests/docs/smoke-all/crossrefs/float/asciidoc/rawtablecaption.qmd index 6079104ce5f..2ea1a4981d7 100644 --- a/tests/docs/smoke-all/crossrefs/float/asciidoc/rawtablecaption.qmd +++ b/tests/docs/smoke-all/crossrefs/float/asciidoc/rawtablecaption.qmd @@ -6,7 +6,7 @@ _quarto: asciidoc: ensureFileRegexMatches: - - - "\\. This is a caption\\." + - "\\.This is a caption\\." - "<>" - "\\[\\[tbl-1\\]\\]" - [] From f18edbc3e72285cd965c43629d970feaf7b67976 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 3 Jan 2024 13:28:58 -0700 Subject: [PATCH 2/2] remove file --- .../float/asciidoc/asciidoc-subfloat-1.adoc | 20 ------------------- 1 file changed, 20 deletions(-) delete mode 100644 tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.adoc diff --git a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.adoc b/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.adoc deleted file mode 100644 index 8838bfbaa9f..00000000000 --- a/tests/docs/smoke-all/crossrefs/float/asciidoc/asciidoc-subfloat-1.adoc +++ /dev/null @@ -1,20 +0,0 @@ -= Crossref Test - -== Simple Sub Figure - -.Famous Elephants -[#fig-elephants] -[width="100%",cols="<50%,<50%",] -|=== -a| -.Surus -[#fig-surus] -image::img/surus.jpg[img/surus] - -a| -.Abbas -[#fig-abbas] -image::img/abbas.jpg[img/abbas] - -|=== -See <> for examples. In particular, <> and <>.