From 7ca4ad553bdcb94aeff5578b4468f6cdce4860a8 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Fri, 13 Sep 2024 23:34:23 +0200 Subject: [PATCH] Allow to use jog instead of walk Jog can be enable on a per-filter basis by setting the `traverse` field to `jog`. --- src/resources/filters/ast/customnodes.lua | 14 ++++++++++++-- src/resources/filters/ast/emulatedfilter.lua | 3 +++ src/resources/filters/ast/runemulation.lua | 2 +- src/resources/filters/common/layout.lua | 2 +- src/resources/filters/common/pandoc.lua | 6 +++--- src/resources/filters/common/wrapped-filter.lua | 4 ++-- src/resources/filters/modules/jog.lua | 5 +++++ src/resources/filters/quarto-post/book.lua | 7 ++++--- src/resources/filters/quarto-post/delink.lua | 2 +- src/resources/filters/quarto-post/latex.lua | 2 +- .../filters/quarto-post/render-asciidoc.lua | 2 +- src/resources/filters/quarto-pre/shiny.lua | 2 +- 12 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/resources/filters/ast/customnodes.lua b/src/resources/filters/ast/customnodes.lua index ecda0ccb24..45967e060f 100644 --- a/src/resources/filters/ast/customnodes.lua +++ b/src/resources/filters/ast/customnodes.lua @@ -41,7 +41,7 @@ function is_regular_node(node, name) return node end -function run_emulated_filter(doc, filter) +function run_emulated_filter(doc, filter, traverse) if doc == nil then return nil end @@ -73,7 +73,17 @@ function run_emulated_filter(doc, filter) -- luacov: enable end end - return node:walk(filter_param) + local old_use_walk = _QUARTO_USE_WALK + if traverse == nil or traverse == 'walk' then + _QUARTO_USE_WALK = true + elseif traverse == 'jog' then + _QUARTO_USE_WALK = false + else + warn('Unknown traverse method: ' .. tostring(traverse)) + end + local result = _quarto.modules.jog(node, filter_param) + _QUARTO_USE_WALK = old_use_walk + return result end -- performance: if filter is empty, do nothing diff --git a/src/resources/filters/ast/emulatedfilter.lua b/src/resources/filters/ast/emulatedfilter.lua index 34d05278f8..99e3df938d 100644 --- a/src/resources/filters/ast/emulatedfilter.lua +++ b/src/resources/filters/ast/emulatedfilter.lua @@ -68,6 +68,9 @@ inject_user_filters_at_entry_points = function(filter_list) end local filter = { name = entry_point .. "-user-" .. tostring(entry_point_counts[entry_point]), + -- The filter might not work as expected when doing a non-lazy jog, so + -- make sure it is processed with the default 'walk' function. + traverse = 'walk', } if is_many_filters then filter.filters = wrapped diff --git a/src/resources/filters/ast/runemulation.lua b/src/resources/filters/ast/runemulation.lua index 39b193a8f3..763b32ed6a 100644 --- a/src/resources/filters/ast/runemulation.lua +++ b/src/resources/filters/ast/runemulation.lua @@ -79,7 +79,7 @@ local function run_emulated_filter_chain(doc, filters, afterFilterPass, profilin print(pandoc.write(doc, "native")) else _quarto.ast._current_doc = doc - doc = run_emulated_filter(doc, v.filter) + doc = run_emulated_filter(doc, v.filter, v.traverse) ensure_vault(doc) add_trace(doc, v.name) diff --git a/src/resources/filters/common/layout.lua b/src/resources/filters/common/layout.lua index 0f0d7672c5..55bd175afd 100644 --- a/src/resources/filters/common/layout.lua +++ b/src/resources/filters/common/layout.lua @@ -56,7 +56,7 @@ end -- we often wrap a table in a div, unwrap it function tableFromLayoutCell(cell) local tbl - cell:walk({ + _quarto.modules.jog(cell, { Table = function(t) tbl = t end diff --git a/src/resources/filters/common/pandoc.lua b/src/resources/filters/common/pandoc.lua index e52a0de9cd..b1bc7ceda3 100644 --- a/src/resources/filters/common/pandoc.lua +++ b/src/resources/filters/common/pandoc.lua @@ -216,14 +216,14 @@ function string_to_quarto_ast_blocks(text, opts) -- run the whole normalization pipeline here to get extended AST nodes, etc. for _, filter in ipairs(quarto_ast_pipeline()) do - doc = doc:walk(filter.filter) + doc = _quarto.modules.jog(doc, filter.filter) end -- compute flags so we don't skip filters that depend on them - doc:walk(compute_flags()) + _quarto.modules.jog(doc, compute_flags()) return doc.blocks end function string_to_quarto_ast_inlines(text, sep) return pandoc.utils.blocks_to_inlines(string_to_quarto_ast_blocks(text), sep) -end \ No newline at end of file +end diff --git a/src/resources/filters/common/wrapped-filter.lua b/src/resources/filters/common/wrapped-filter.lua index 7bb7e1f189..4fc139ee3e 100644 --- a/src/resources/filters/common/wrapped-filter.lua +++ b/src/resources/filters/common/wrapped-filter.lua @@ -97,7 +97,7 @@ function makeWrappedJsonFilter(scriptFile, filterHandler) path = quarto.utils.resolve_path_relative_to_document(scriptFile) local custom_node_map = {} local has_custom_nodes = false - doc = doc:walk({ + doc = _quarto.modules.jog(doc, { -- FIXME: This is broken with new AST. Needs to go through Custom node instead. RawInline = function(raw) local custom_node, t, kind = _quarto.ast.resolve_custom_data(raw) @@ -130,7 +130,7 @@ function makeWrappedJsonFilter(scriptFile, filterHandler) return nil end if has_custom_nodes then - doc:walk({ + _quarto.modules.jog(doc, { Meta = function(meta) _quarto.ast.reset_custom_tbl(meta["quarto-custom-nodes"]) end diff --git a/src/resources/filters/modules/jog.lua b/src/resources/filters/modules/jog.lua index 988e997108..590acbe484 100644 --- a/src/resources/filters/modules/jog.lua +++ b/src/resources/filters/modules/jog.lua @@ -240,6 +240,10 @@ local element_name_map = { --- Function to traverse the pandoc AST with context. local function jog(element, filter) + if _QUARTO_USE_WALK then + return element:walk(filter) + end + local context = filter.context and List{} or nil -- Table elements have a `pandoc ` prefix in the name @@ -263,6 +267,7 @@ local function jog(element, filter) end end + -- Create and call traversal function local jog_internal = make_jogger(filter, context) return jog_internal(element) diff --git a/src/resources/filters/quarto-post/book.lua b/src/resources/filters/quarto-post/book.lua index ff354ec200..4a0d923a23 100644 --- a/src/resources/filters/quarto-post/book.lua +++ b/src/resources/filters/quarto-post/book.lua @@ -8,10 +8,11 @@ local license = require 'modules/license' local function clean (inlines) -- this is in post, so it's after render, so we don't need to worry about -- custom ast nodes - return inlines:walk { - Note = function (_) return {} end, + return _quarto.modules.jog(inlines, { + traverse = 'topdown', + Note = function (_) return {}, false end, Link = function (link) return link.content end, - } + }) end --- Creates an Inlines singleton containing the raw LaTeX. diff --git a/src/resources/filters/quarto-post/delink.lua b/src/resources/filters/quarto-post/delink.lua index f391781c29..d91610e428 100644 --- a/src/resources/filters/quarto-post/delink.lua +++ b/src/resources/filters/quarto-post/delink.lua @@ -19,7 +19,7 @@ function delink() -- find links and transform them to spans -- this is in post, so it's after render, so we don't need to worry about -- custom ast nodes - return pandoc.walk_block(div, { + return _quarto.modules.jog(div, { Link = function(link) return pandoc.Span(link.content) end diff --git a/src/resources/filters/quarto-post/latex.lua b/src/resources/filters/quarto-post/latex.lua index 132252a652..a78d159d65 100644 --- a/src/resources/filters/quarto-post/latex.lua +++ b/src/resources/filters/quarto-post/latex.lua @@ -407,7 +407,7 @@ function render_latex() end, Note = function(el) tappend(noteContents, {el.content}) - el.content:walk({ + _quarto.modules.jog(el.content, { CodeBlock = function(el) hasVerbatimInNotes = true end diff --git a/src/resources/filters/quarto-post/render-asciidoc.lua b/src/resources/filters/quarto-post/render-asciidoc.lua index 10ad3603ee..5121d55099 100644 --- a/src/resources/filters/quarto-post/render-asciidoc.lua +++ b/src/resources/filters/quarto-post/render-asciidoc.lua @@ -89,7 +89,7 @@ function render_asciidoc() local noteEl = el[i+1] -- if the note contains a code inline, we need to add a space local hasCode = false - pandoc.walk_inline(noteEl, { + _quarto.module.jog(noteEl, { Code = function(_el) hasCode = true end diff --git a/src/resources/filters/quarto-pre/shiny.lua b/src/resources/filters/quarto-pre/shiny.lua index 75dbdd9197..4ce3cdf5af 100644 --- a/src/resources/filters/quarto-pre/shiny.lua +++ b/src/resources/filters/quarto-pre/shiny.lua @@ -67,7 +67,7 @@ function server_shiny() -- blocks.) local context = nil - local res = pandoc.walk_block(divEl, { + local res = _quarto.modules.jog(divEl, { CodeBlock = function(el) if el.attr.classes:includes("python") and el.attr.classes:includes("cell-code") then