From 09d79b744193133b4cd9021e46de9d6ce9ab70db Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Sun, 29 Oct 2023 19:16:29 -0700 Subject: [PATCH] use as_blocks(el.code_block) everywhere in the renderer. Closes #6985 (again) --- .../filters/customnodes/decoratedcodeblock.lua | 15 +++++++++++---- tests/docs/smoke-all/2023/10/29/6985-b.qmd | 12 ++++++++++++ tests/docs/smoke-all/2023/10/29/script.R | 1 + tests/docs/smoke-all/2023/10/29/script.js | 1 + 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 tests/docs/smoke-all/2023/10/29/6985-b.qmd create mode 100644 tests/docs/smoke-all/2023/10/29/script.R create mode 100644 tests/docs/smoke-all/2023/10/29/script.js diff --git a/src/resources/filters/customnodes/decoratedcodeblock.lua b/src/resources/filters/customnodes/decoratedcodeblock.lua index a041f5a7b6..9ed8674b85 100644 --- a/src/resources/filters/customnodes/decoratedcodeblock.lua +++ b/src/resources/filters/customnodes/decoratedcodeblock.lua @@ -55,9 +55,14 @@ _quarto.ast.add_renderer("DecoratedCodeBlock", -- But that'll be done in 1.4 with crossrefs overhaul. if node.filename then + -- a user filter could have replaced + -- a single code block in a decorated code block with a list of elements, + -- so we need to handle that. + local blocks = quarto.utils.as_blocks(el) or pandoc.Blocks({}) -- if we have a filename, add it as a header + blocks:insert(1, pandoc.Plain{pandoc.Strong{pandoc.Str(node.filename)}}) return pandoc.Div( - { pandoc.Plain{pandoc.Strong{pandoc.Str(node.filename)}}, el }, + blocks, pandoc.Attr("", {"code-with-filename"}) ) else @@ -74,7 +79,6 @@ _quarto.ast.add_renderer("DecoratedCodeBlock", local el = node.code_block -- add listing class to the code block el.attr.classes:insert("listing") - -- if we are use the listings package we don't need to do anything -- further, otherwise generate the listing div and return it if not param("listings", false) then @@ -112,7 +116,10 @@ _quarto.ast.add_renderer("DecoratedCodeBlock", listingDiv.content:insert(listingCaption) end - listingDiv.content:insert(el) + -- a user filter could have replaced + -- a single code block in a decorated code block with a list of elements, + -- so we need to handle that. + listingDiv.content:extend(quarto.utils.as_blocks(el) or {}) listingDiv.content:insert(pandoc.RawBlock("latex", "\\end{codelisting}")) return listingDiv end @@ -150,7 +157,7 @@ _quarto.ast.add_renderer("DecoratedCodeBlock", if filenameEl ~= nil then blocks:insert(filenameEl) end - blocks:insert(el) + blocks:extend(quarto.utils.as_blocks(el) or {}) return pandoc.Div(blocks, pandoc.Attr("", classes)) end) diff --git a/tests/docs/smoke-all/2023/10/29/6985-b.qmd b/tests/docs/smoke-all/2023/10/29/6985-b.qmd new file mode 100644 index 0000000000..b06b9ee5a7 --- /dev/null +++ b/tests/docs/smoke-all/2023/10/29/6985-b.qmd @@ -0,0 +1,12 @@ +--- +code-fold: true +--- + +```{r} +#| file: script.R +#| filename: script.R +``` + +```{js} +#| file: script.js +``` diff --git a/tests/docs/smoke-all/2023/10/29/script.R b/tests/docs/smoke-all/2023/10/29/script.R new file mode 100644 index 0000000000..8d2f0971e2 --- /dev/null +++ b/tests/docs/smoke-all/2023/10/29/script.R @@ -0,0 +1 @@ +1 + 1 diff --git a/tests/docs/smoke-all/2023/10/29/script.js b/tests/docs/smoke-all/2023/10/29/script.js new file mode 100644 index 0000000000..019c0f4bc8 --- /dev/null +++ b/tests/docs/smoke-all/2023/10/29/script.js @@ -0,0 +1 @@ +console.log("Hello World!");