Skip to content

Instantly share code, notes, and snippets.

@kljensen
Last active January 7, 2020 20:37
Show Gist options
  • Save kljensen/b4d4580b678d44d2d85e07fc1e450c1c to your computer and use it in GitHub Desktop.
Save kljensen/b4d4580b678d44d2d85e07fc1e450c1c to your computer and use it in GitHub Desktop.
A Pandoc Lua filter that removes Spaces before Cite elements
local function remove_citation_space(para)
local table elements = {}
i = 0
local function logel(el)
i = i + 1
elements[i] = el.t
end
pandoc.walk_block(para, {Inline = logel})
-- See which Space elements should be deleted. It
-- is those that follow a Cite element.
local table todelete = {}
local post_cite = false
for i = #elements, 1, -1 do
el = elements[i]
if el == "Space" or el == "SoftBreak" then
if post_cite then
todelete[i] = true
end
elseif el == "Cite" then
post_cite = true
else
post_cite = false
end
end
j = 0
local function delete(el)
j = j + 1
if todelete[j] == true then
return {}
end
end
return pandoc.walk_block(para, {Inline = delete})
end
return {
{Para = remove_citation_space}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment