Skip to content

Commit 7ca4ad5

Browse files
committed
Allow to use jog instead of walk
Jog can be enable on a per-filter basis by setting the `traverse` field to `jog`.
1 parent b5ee9fb commit 7ca4ad5

File tree

12 files changed

+35
-16
lines changed

12 files changed

+35
-16
lines changed

src/resources/filters/ast/customnodes.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function is_regular_node(node, name)
4141
return node
4242
end
4343

44-
function run_emulated_filter(doc, filter)
44+
function run_emulated_filter(doc, filter, traverse)
4545
if doc == nil then
4646
return nil
4747
end
@@ -73,7 +73,17 @@ function run_emulated_filter(doc, filter)
7373
-- luacov: enable
7474
end
7575
end
76-
return node:walk(filter_param)
76+
local old_use_walk = _QUARTO_USE_WALK
77+
if traverse == nil or traverse == 'walk' then
78+
_QUARTO_USE_WALK = true
79+
elseif traverse == 'jog' then
80+
_QUARTO_USE_WALK = false
81+
else
82+
warn('Unknown traverse method: ' .. tostring(traverse))
83+
end
84+
local result = _quarto.modules.jog(node, filter_param)
85+
_QUARTO_USE_WALK = old_use_walk
86+
return result
7787
end
7888

7989
-- performance: if filter is empty, do nothing

src/resources/filters/ast/emulatedfilter.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ inject_user_filters_at_entry_points = function(filter_list)
6868
end
6969
local filter = {
7070
name = entry_point .. "-user-" .. tostring(entry_point_counts[entry_point]),
71+
-- The filter might not work as expected when doing a non-lazy jog, so
72+
-- make sure it is processed with the default 'walk' function.
73+
traverse = 'walk',
7174
}
7275
if is_many_filters then
7376
filter.filters = wrapped

src/resources/filters/ast/runemulation.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ local function run_emulated_filter_chain(doc, filters, afterFilterPass, profilin
7979
print(pandoc.write(doc, "native"))
8080
else
8181
_quarto.ast._current_doc = doc
82-
doc = run_emulated_filter(doc, v.filter)
82+
doc = run_emulated_filter(doc, v.filter, v.traverse)
8383
ensure_vault(doc)
8484

8585
add_trace(doc, v.name)

src/resources/filters/common/layout.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ end
5656
-- we often wrap a table in a div, unwrap it
5757
function tableFromLayoutCell(cell)
5858
local tbl
59-
cell:walk({
59+
_quarto.modules.jog(cell, {
6060
Table = function(t)
6161
tbl = t
6262
end

src/resources/filters/common/pandoc.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ function string_to_quarto_ast_blocks(text, opts)
216216

217217
-- run the whole normalization pipeline here to get extended AST nodes, etc.
218218
for _, filter in ipairs(quarto_ast_pipeline()) do
219-
doc = doc:walk(filter.filter)
219+
doc = _quarto.modules.jog(doc, filter.filter)
220220
end
221221

222222
-- compute flags so we don't skip filters that depend on them
223-
doc:walk(compute_flags())
223+
_quarto.modules.jog(doc, compute_flags())
224224
return doc.blocks
225225
end
226226

227227
function string_to_quarto_ast_inlines(text, sep)
228228
return pandoc.utils.blocks_to_inlines(string_to_quarto_ast_blocks(text), sep)
229-
end
229+
end

src/resources/filters/common/wrapped-filter.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function makeWrappedJsonFilter(scriptFile, filterHandler)
9797
path = quarto.utils.resolve_path_relative_to_document(scriptFile)
9898
local custom_node_map = {}
9999
local has_custom_nodes = false
100-
doc = doc:walk({
100+
doc = _quarto.modules.jog(doc, {
101101
-- FIXME: This is broken with new AST. Needs to go through Custom node instead.
102102
RawInline = function(raw)
103103
local custom_node, t, kind = _quarto.ast.resolve_custom_data(raw)
@@ -130,7 +130,7 @@ function makeWrappedJsonFilter(scriptFile, filterHandler)
130130
return nil
131131
end
132132
if has_custom_nodes then
133-
doc:walk({
133+
_quarto.modules.jog(doc, {
134134
Meta = function(meta)
135135
_quarto.ast.reset_custom_tbl(meta["quarto-custom-nodes"])
136136
end

src/resources/filters/modules/jog.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ local element_name_map = {
240240

241241
--- Function to traverse the pandoc AST with context.
242242
local function jog(element, filter)
243+
if _QUARTO_USE_WALK then
244+
return element:walk(filter)
245+
end
246+
243247
local context = filter.context and List{} or nil
244248

245249
-- Table elements have a `pandoc ` prefix in the name
@@ -263,6 +267,7 @@ local function jog(element, filter)
263267
end
264268
end
265269

270+
266271
-- Create and call traversal function
267272
local jog_internal = make_jogger(filter, context)
268273
return jog_internal(element)

src/resources/filters/quarto-post/book.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ local license = require 'modules/license'
88
local function clean (inlines)
99
-- this is in post, so it's after render, so we don't need to worry about
1010
-- custom ast nodes
11-
return inlines:walk {
12-
Note = function (_) return {} end,
11+
return _quarto.modules.jog(inlines, {
12+
traverse = 'topdown',
13+
Note = function (_) return {}, false end,
1314
Link = function (link) return link.content end,
14-
}
15+
})
1516
end
1617

1718
--- Creates an Inlines singleton containing the raw LaTeX.

src/resources/filters/quarto-post/delink.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function delink()
1919
-- find links and transform them to spans
2020
-- this is in post, so it's after render, so we don't need to worry about
2121
-- custom ast nodes
22-
return pandoc.walk_block(div, {
22+
return _quarto.modules.jog(div, {
2323
Link = function(link)
2424
return pandoc.Span(link.content)
2525
end

src/resources/filters/quarto-post/latex.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ function render_latex()
407407
end,
408408
Note = function(el)
409409
tappend(noteContents, {el.content})
410-
el.content:walk({
410+
_quarto.modules.jog(el.content, {
411411
CodeBlock = function(el)
412412
hasVerbatimInNotes = true
413413
end

src/resources/filters/quarto-post/render-asciidoc.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function render_asciidoc()
8989
local noteEl = el[i+1]
9090
-- if the note contains a code inline, we need to add a space
9191
local hasCode = false
92-
pandoc.walk_inline(noteEl, {
92+
_quarto.module.jog(noteEl, {
9393
Code = function(_el)
9494
hasCode = true
9595
end

src/resources/filters/quarto-pre/shiny.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function server_shiny()
6767
-- blocks.)
6868
local context = nil
6969

70-
local res = pandoc.walk_block(divEl, {
70+
local res = _quarto.modules.jog(divEl, {
7171
CodeBlock = function(el)
7272
if el.attr.classes:includes("python") and el.attr.classes:includes("cell-code") then
7373

0 commit comments

Comments
 (0)