Thank you, it seems both your and mf's suggestion are similar so I should be all set.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 andpush elements from the old list onto that list until you get to the lastelement which should go into the div.4. Create a div from the thirdlist.5. Push the div onto the new list.6. Either repeat steps 2–5 or resume pushing elements from the oldlist onto the new list until you get to the last element.
``````luafunction Pandoc(doc)local old = doc.blockslocal new = pandoc.Blocks({ })local div = falselocal i = 0local elem = nilwhile i < #old doi = i + 1elem = old[i]if test_div_start_condition(elem) thendiv = pandoc.Blocks({ elem })elseif test_div_end_condition(elem) thenif div thendiv[#div + 1] = elemnew[#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]-- endelsenew[#new + 1] = elemendelseif div thendiv[#div + 1] = elemelsenew[#new + 1] = elemendenddoc.blocks = newreturn docend``````
Den tis 20 juni 2023 02:16H <agents-FcZObrvlYduBUy7/sJONFg@public.gmane.org> skrev:
On June 19, 2023 6:11:35 PM EDT, H <agents@meddatainc.com> wrote:
>What is the "best" way of wrapping parts of an existing document in
><div></div> in a filter?
>
>Using insert.table(existingtable, pandoc.Div(existingtextblocks)) does
>not work since the existing text blocks get duplicated in the output.
>
>I see two options but there might be others:
>
>- Surround existing text blocks with pandoc.RawInline(1, 'html,
>'<div>') and pandoc.RawInLine('html', '</div>').
>
>- Create a new table and use insert.table(newtable,
>pandoc.Div(existingtextblock)) and also insert the rest of the
>document.
>
>Are there other options?
Replying to my own question, it seems the most "elegant" way of accomplishing this requires this to be done be in two stages:
- First, delete the blocks in question from the block list table, eg doc.blocks.
- Second, insert them into the same table using pandoc.Div().
Unfortunately it seems table.delete() does not accept a list of blocks, only one block at a time, pandoc.Div, on the other hand, accepts a table. Here is a code example when I was trying it out:
local temp = {}
for i = 3, 1, -1 do
table.insert(temp, table.remove(doc.blocks, 5))
end
table.insert(doc.blocks, 2, pandoc.Div(temp))
The above code removes blocks 5 through 7 from doc.blocks, then inserts them, in the same order but surrounded by <div></div>, in position 2.
If there is a better way of accomplishing this, I would appreciate hearing it.