Skip to content

Commit

Permalink
Allow to use jog instead of walk
Browse files Browse the repository at this point in the history
Jog can be enable on a per-filter basis by setting the `traverse` field
to `jog`.
  • Loading branch information
tarleb committed Nov 26, 2024
1 parent b5ee9fb commit 7ca4ad5
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 16 deletions.
14 changes: 12 additions & 2 deletions src/resources/filters/ast/customnodes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/resources/filters/ast/emulatedfilter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/resources/filters/ast/runemulation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/resources/filters/common/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/resources/filters/common/pandoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
4 changes: 2 additions & 2 deletions src/resources/filters/common/wrapped-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/resources/filters/modules/jog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/resources/filters/quarto-post/book.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/resources/filters/quarto-post/delink.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/resources/filters/quarto-post/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/resources/filters/quarto-post/render-asciidoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/resources/filters/quarto-pre/shiny.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7ca4ad5

Please sign in to comment.