Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua - accessing div.content in empty div changes its content #9613

Closed
cscheid opened this issue Mar 28, 2024 · 6 comments
Closed

Lua - accessing div.content in empty div changes its content #9613

cscheid opened this issue Mar 28, 2024 · 6 comments
Assignees

Comments

@cscheid
Copy link
Contributor

cscheid commented Mar 28, 2024

Explain the problem.

In a Lua filter, accessing the content field of an empty div element appears to introduce an empty Plain [] element into the content.

Here's a repro:

test.md

::: {}
:::

test.lua

function Div(div)
  print("Before accessing content", div)

  div:walk({
    Plain = function(plain)
      print("oh oh")
    end
  })

  print("Access content here: ", div.content)
  print("This still shows empty: ", div.content[1])
  print("... and this shows 0: ", #div.content)
  print("But this prints an element", div)

  print("... and this finds an element (?)")
  div:walk({
    Plain = function(plain)
      print("oh oh")
    end
  })
end

repro

$ pandoc -f markdown test.md -L test.lua -o /dev/null
Before accessing content	Div ("",[],[]) []
Access content here: 	[]
This still shows empty: 	nil
... and this shows 0: 	0
But this prints an element	Div ("",[],[]) [Plain []]
... and this finds an element (?)
oh oh

Pandoc version?

3.1.12.3, (ARM macOS).

This appears to not be a regression, and reproduces going back to at least 2.19.2 (any other versions I tried further back would fail from a lack of a :walk{} method.)

@cscheid cscheid added the bug label Mar 28, 2024
cscheid added a commit to quarto-dev/quarto-cli that referenced this issue Mar 28, 2024
@jgm
Copy link
Owner

jgm commented Mar 29, 2024

@tarleb would know about this.

@tarleb tarleb self-assigned this Mar 29, 2024
@tarleb
Copy link
Collaborator

tarleb commented Mar 29, 2024

Just to add a bit to the weirdness:

local div = pandoc.Div({})
print(div)
x = div.content
print(div)
print(pandoc.Div(pandoc.Blocks{}))
div.content = pandoc.Blocks{}
print(div)

prints

Div ("",[],[]) []
Div ("",[],[]) [Plain []]
Div ("",[],[]) []
Div ("",[],[]) [Plain []]

Which makes peekBlock the prime suspect.

@tarleb
Copy link
Collaborator

tarleb commented Mar 29, 2024

Same issue with BlockQuote:

local bq = pandoc.BlockQuote({})
print(bq)
x = bq.content
print(bq)
BlockQuote []
BlockQuote [Plain []]

@tarleb
Copy link
Collaborator

tarleb commented Mar 29, 2024

I'm still in the dark. What I can say so far is that the empty list is pushed correctly, it's an empty list in Lua:

print(#div.content, rawlen(div.content))  -- 0   0

Also, the peekBlocksFuzzy function seems to be fine, too, as that's what we use in the pandoc.Div constructor:

print(pandoc.Div(div.content))   -- Div ("",[],[]) []

So the problem must be in the way that empty lists are read back from lazy properties.

Apparently the list is treated as if it was nested. That might be a hint.

print(pandoc.Div(pandoc.Blocks{{}}))  -- Div ("",[],[]) [Plain []]

@tarleb
Copy link
Collaborator

tarleb commented Mar 29, 2024

Also, the problem doesn't exist with Note, which also has a content property of type [Block]:

local note = pandoc.Note({})
print(note)
x = note.content
print(note)
Note []
Note []

@tarleb
Copy link
Collaborator

tarleb commented Mar 29, 2024

Found it. The issue was hidden in the setBlockContent function in Text.Pandoc.Lua.Marshal.Block. The function is a bit of a crutch that's required because content can have different types depending on the specific Block element. The case of an empty list wasn't handled correctly.

Patch incoming.

tarleb added a commit to pandoc/pandoc-lua-marshal that referenced this issue Mar 29, 2024
An empty list on Div, Figure, and BlockQuote elements is no longer read
back as `[Plain []]`.

See also: jgm/pandoc#9613
@tarleb tarleb closed this as completed in 2792d21 Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants