diff --git a/src/resources/filters/customnodes/decoratedcodeblock.lua b/src/resources/filters/customnodes/decoratedcodeblock.lua index 9ed8674b85e..26e618d9ec2 100644 --- a/src/resources/filters/customnodes/decoratedcodeblock.lua +++ b/src/resources/filters/customnodes/decoratedcodeblock.lua @@ -38,7 +38,9 @@ _quarto.ast.add_renderer("DecoratedCodeBlock", return true end, function(node) - return node.code_block + return _quarto.ast.walk(node.code_block, { + CodeBlock = render_folded_block + }) end) -- markdown renderer @@ -66,7 +68,9 @@ _quarto.ast.add_renderer("DecoratedCodeBlock", pandoc.Attr("", {"code-with-filename"}) ) else - return el + return _quarto.ast.walk(quarto.utils.as_blocks(el), { + CodeBlock = render_folded_block + }) end end) @@ -76,9 +80,17 @@ _quarto.ast.add_renderer("DecoratedCodeBlock", return _quarto.format.isLatexOutput() end, function(node) - local el = node.code_block -- add listing class to the code block - el.attr.classes:insert("listing") + -- need to walk the code block instead of assigning directly + -- because upstream filters might have replaced the code block with + -- more than one element + node.code_block = _quarto.ast.walk(quarto.utils.as_blocks(node.code_block), { + CodeBlock = function(el) + el.attr.classes:insert("listing") + return render_folded_block(el) + end + }) or node.code_block -- unneeded but the Lua analyzer doesn't know that + -- 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 @@ -123,7 +135,7 @@ _quarto.ast.add_renderer("DecoratedCodeBlock", listingDiv.content:insert(pandoc.RawBlock("latex", "\\end{codelisting}")) return listingDiv end - return el + return node.code_block end) -- html renderer @@ -132,32 +144,40 @@ _quarto.ast.add_renderer("DecoratedCodeBlock", return _quarto.format.isHtmlOutput() end, function(node) + if node.filename == nil then + return _quarto.ast.walk(quarto.utils.as_blocks(node.code_block), { + CodeBlock = render_folded_block + }) + end local el = node.code_block local filenameEl local caption local classes = pandoc.List() - local fancy_output = false - if node.filename ~= nil then - filenameEl = pandoc.Div({pandoc.Plain{ - pandoc.RawInline("html", "
"),
- pandoc.Strong{pandoc.Str(node.filename)},
- pandoc.RawInline("html", "")
- }}, pandoc.Attr("", {"code-with-filename-file"}))
- classes:insert("code-with-filename")
- fancy_output = true
- end
- if not fancy_output then
- return el
- end
+ filenameEl = pandoc.Div({pandoc.Plain{
+ pandoc.RawInline("html", ""),
+ pandoc.Strong{pandoc.Str(node.filename)},
+ pandoc.RawInline("html", "")
+ }}, pandoc.Attr("", {"code-with-filename-file"}))
+ classes:insert("code-with-filename")
local blocks = pandoc.Blocks({})
if caption ~= nil then
blocks:insert(caption)
end
+ el = _quarto.ast.walk(quarto.utils.as_blocks(el), {
+ CodeBlock = render_folded_block
+ }) or pandoc.Blocks({})
if filenameEl ~= nil then
- blocks:insert(filenameEl)
+ el = _quarto.ast.walk(quarto.utils.as_blocks(el), {
+ CodeBlock = function(block)
+ return pandoc.Blocks({
+ filenameEl,
+ block
+ })
+ end
+ }) or pandoc.Blocks({})
end
- blocks:extend(quarto.utils.as_blocks(el) or {})
+ blocks:extend(el)
return pandoc.Div(blocks, pandoc.Attr("", classes))
end)
diff --git a/src/resources/filters/quarto-post/foldcode.lua b/src/resources/filters/quarto-post/foldcode.lua
index 73dc101cb31..a26bc33ecf2 100644
--- a/src/resources/filters/quarto-post/foldcode.lua
+++ b/src/resources/filters/quarto-post/foldcode.lua
@@ -2,94 +2,101 @@
-- Copyright (C) 2021-2022 Posit Software, PBC
-- slightly fancy code here to make two operations work in a single pass
+function render_folded_block(block)
+ local make_code_fold_html = function(fold, summary)
+ local div = pandoc.Div({}, pandoc.Attr("", {
+ "quarto-scaffold"
+ }))
+ quarto_global_state.codeFoldingCss = _quarto.format.isHtmlOutput()
+ local open = ""
+ if fold == "show" then
+ open = " open"
+ end
+ local style = ""
+ local clz = 'code-fold'
+ if block.attr.classes:includes("hidden") then
+ clz = clz .. " hidden"
+ end
-local pass_uuid = "8c6b3915-b784-4ce5-8e73-59b368a9f289"
+ style = ' class="' .. clz .. '"'
+ local beginPara = pandoc.Plain({
+ pandoc.RawInline("html", "