local label_map = {} -- label has to be unique! local function collect_labels(div) -- only collect labels in div if div.identifier and div.identifier ~= "" then label_map[div.identifier] = div:clone() end return nil end local function words(s) local res = {} for part in s:gmatch("[^,]+") do -- split by commas local trimmed = part:match("^%s*(.-)%s*$") -- remove leading/trailing spaces if trimmed ~= "" then table.insert(res, trimmed) end end return res end local function replace(e) local include = e.attributes["include"] if include then local blocks = {} -- Replace the Div contents for _, l in ipairs(words(include)) do if label_map[l] then table.insert(blocks, label_map[l]:clone()) io.stderr:write("insert [" .. label_map[l].identifier .. ']\n') else io.stderr:write("Cannot find AST node with label " .. l) end end return pandoc.Div(blocks, e.attr) end return e -- no change end return { -- traverse = 'topdown', Pandoc = function(doc) -- collect labels for _, blk in ipairs(doc.blocks) do collect_labels(blk) end -- replace for i, blk in ipairs(doc.blocks) do doc.blocks[i] = replace(blk) end return doc end }