1. You have two lists, the original/old one and the new one.
2. Push elements from the old list onto the new list until you get to the first element which should go into the new nested div.
3. Create a third list and
push elements from the old list onto that list until you get to the last
element which should go into the div.
4. Create a div from the third
list.
5. Push the div onto the new list.
6. Either repeat steps 2–5 or resume pushing elements from the old
list onto the new list until you get to the last element.
``````lua
function Pandoc(doc)
local old = doc.blocks
local new = pandoc.Blocks({ })
local div = false
local i = 0
local elem = nil
while i < #old do
i = i + 1
elem = old[i]
if test_div_start_condition(elem) then
div = pandoc.Blocks({ elem })
elseif test_div_end_condition(elem) then
if div then
div[#div + 1] = elem
new[#new + 1] = pandoc.Div(div, { class = 'foo', bar = 'baz' })
div = nil
-- -- If this is the only div
-- while i < #old do
-- i = i + 1
-- new[#new + 1] = = old[i]
-- end
else
new[#new + 1] = elem
end
elseif div then
div[#div + 1] = elem
else
new[#new + 1] = elem
end
end
doc.blocks = new
return doc
end
``````