pandoc/test/lua/inlines-filter.lua
Albert Krewinkel 672a4bdd1d Lua filters: allow filtering of element lists (#6040)
Lists of Inline and Block elements can now be filtered via `Inlines` and
`Blocks` functions, respectively. This is helpful if a filter conversion
depends on the order of elements rather than a single element.

For example, the following filter can be used to remove all spaces
before a citation:

    function isSpaceBeforeCite (spc, cite)
      return spc and spc.t == 'Space'
       and cite and cite.t == 'Cite'
    end

    function Inlines (inlines)
      for i = #inlines-1,1,-1 do
        if isSpaceBeforeCite(inlines[i], inlines[i+1]) then
          inlines:remove(i)
        end
      end
      return inlines
    end

Closes: #6038
2020-01-15 14:26:00 -08:00

19 lines
529 B
Lua

function isWorldAfterSpace (fst, snd)
return fst and fst.t == 'LineBreak'
and snd and snd.t == 'Str' and snd.text == 'World!'
end
function Inlines (inlns)
-- verify that this looks like a `pandoc.List`
if not inlns.find or not inlns.map or not inlns.filter then
error("table doesn't seem to be an instance of pandoc.List")
end
-- Remove spaces before string "World"
for i = #inlns-1,1,-1 do
if isWorldAfterSpace(inlns[i], inlns[i+1]) then
inlns[i] = pandoc.Space()
end
end
return inlns
end