Since Pandoc version 2.0, there has been the ability to write Lua Filters. However, in Pandoc 2.0, I find that using Lua's pairs
on an element table does not show all keys in the table.
Here is a minimal example to illustrate the point. In filter.lua
I have:
function Para(elem)
io.stderr:write("A: " .. type(elem) .. "\n")
for k, v in pairs(elem) do
io.stderr:write("B: " .. k .. "\n")
end
io.stderr:write("C: " .. elem["t"] .. "\n")
io.stderr:write("D: " .. tostring(elem["c"]) .. "\n")
-- Return elem unchanged
return nil
end
Now from the command-line, I run:
echo "Hello." | pandoc -f markdown -t native --lua-filter filter.lua
This produces the output:
A: table
B: c
C: Para
D: table: 0x53adb40
[Para [Str "Hello."]]
I can change the -t native
to -t json
so that the last line becomes:
{"blocks":[{"t":"Para","c":[{"t":"Str","c":"Hello."}]}],"pandoc-api-version":[1,17,2],"meta":{}}
So from the output at (B), it looks like c
is the only key in elem
, but from (C) it is clear that t
is also a key as I can access it to obtain Para
. What is happening here and why is the t
key hidden from the loop done with pairs
?