Skip to content

Commit 634916c

Browse files
authored
Merge pull request #11926 from quarto-dev/tmp-2025-01-21-coalesce-raw
perf - coalesce_raw
2 parents 7ad79ec + 9b50558 commit 634916c

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

src/resources/filters/quarto-finalize/coalesceraw.lua

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,37 @@ function coalesce_raw()
3232

3333
table.insert(filters, {
3434
Inlines = function(inlines)
35-
local list_of_lists = collate(inlines, function(block, prev_block)
36-
return block.t == "RawInline" and
37-
prev_block.t == "RawInline" and prev_block.format == block.format
38-
end)
39-
local result = pandoc.Inlines({})
40-
for _, lst in ipairs(list_of_lists) do
41-
local first_el = lst[1]
42-
if first_el.t == "RawInline" then
43-
local text = table.concat(lst:map(function(block) return block.text end), "")
44-
local new_block = pandoc.RawInline(first_el.format, text)
45-
result:insert(new_block)
35+
local current_node = nil
36+
for i = 1, #inlines do
37+
if inlines[i].t ~= "RawInline" then
38+
current_node = nil
4639
else
47-
result:insert(first_el)
40+
if current_node and inlines[i].format == current_node.format then
41+
current_node.text = current_node.text .. inlines[i].text
42+
inlines[i].text = ""
43+
else
44+
current_node = inlines[i]
45+
end
4846
end
4947
end
50-
return result
48+
return inlines
5149
end,
5250
Blocks = function(blocks)
53-
local list_of_lists = collate(blocks, function(block, prev_block)
54-
return block.t == "RawBlock" and block.format:match(".*-merge$") and
55-
prev_block.t == "RawBlock" and prev_block.format == block.format
56-
end)
57-
local result = pandoc.Blocks({})
58-
for _, lst in ipairs(list_of_lists) do
59-
local first_el = lst[1]
60-
if first_el.t == "RawBlock" and first_el.format:match(".*-merge") then
61-
local text = table.concat(lst:map(function(block) return block.text end), "%\n")
62-
local new_block = pandoc.RawBlock(first_el.format:gsub("-merge$", ""), text)
63-
result:insert(new_block)
51+
local current_node = nil
52+
for i = 1, #blocks do
53+
if blocks[i].t ~= "RawBlock" or not blocks[i].format:match(".*-merge$") then
54+
current_node = nil
6455
else
65-
result:insert(first_el)
56+
blocks[i].format = blocks[i].format:gsub("-merge$", "")
57+
if current_node and blocks[i].format == current_node.format then
58+
current_node.text = current_node.text .. blocks[i].text
59+
blocks[i].text = ""
60+
else
61+
current_node = blocks[i]
62+
end
6663
end
6764
end
68-
return result
65+
return blocks
6966
end
7067
})
7168
return filters

0 commit comments

Comments
 (0)