pandoc / lua-filters

A collection of lua filters for pandoc
MIT License
611 stars 166 forks source link

Difficulty getting pandoc.read to return a list of blocks #41

Closed nrnrnr closed 4 years ago

nrnrnr commented 5 years ago

The new documentation from #31 is very helpful. But I am having trouble getting pandoc.read to return something that contains a list of blocks. There is a Lua table there, but the keys look like large negative integers, and ipairs does not produce the blocks. Also indexing blocks[1] returns nil.

Here is a minimal working example filter that demonstrates the issue:

local function block_issues()
  local results = { } -- supposed to include every block read
  local loaded = pandoc.read 'I rejected a *thing*.\n\nSecond block.\n'
  local outcome = 'Never found a block'
  for _, b in ipairs(loaded.blocks) do
    outcome = 'Found a block'
    table.insert(results, b)
    io.stderr:write('inserted', tostring(b), '\n')
  end
  io.stderr:write(outcome, '\n')
  io.stderr:write('returning ', tostring(#results), ' blocks\n')
  return results
end

block_issues()

return { }

I ran this by pandoc --lua-filter=./read-trouble < /dev/null, getting this output:

nr@yorkie ~/tmp> pandoc --lua-filter=./read-trouble < /dev/null
Never found a block
returning 0 blocks

Pandoc has this to say about its version:

pandoc 2.2.1
Compiled with pandoc-types 1.17.4.2, texmath 0.11.0.1, skylighting 0.7.1
Default user data directory: /home/nr/.pandoc
tarleb commented 5 years ago

read returns a new Pandoc document, as the input could also contain metadata. The blocks field should give the list of Blocks.

Type information for module functions got lost when we first converted documentation from the source into markdown. I'll consider this issue my reminder to fix this.

tarleb commented 5 years ago

I think I misread your question. This does look weird. I'll take a look later.

tarleb commented 5 years ago

I tested with pandoc 2.4 and 2.5, both gave the expected result. How was pandoc installed on your machine? I remember seeing similar things happen when I compiled against the wrong Lua version. Could that be the case?

nrnrnr commented 5 years ago

Update: while trying to provide more information for you, I ran the filter on a different computer, and everything is fine. On the good computer, I am running a pandoc from a Debian package. On the bad computer, I am running a pandoc built with cabal install pandoc.

nrnrnr commented 5 years ago

I've now tried two more machines and have confirmed that in our production environment, function pandoc.read works as advertised.

Please let me know if you'd like to try to track down the fault in my Cabal installation, to see if there is anything to be learned there, or if it's better just to close the issue.

tarleb commented 5 years ago

It is good to hear that it works on most systems. I probably won't have the time to look into the failing case in detail, there are too many things that influence the build. It is ok for me to close this.