Skip to content

OrderedList and BulletList break when empty in lua scripts  #8916

Closed
@CodeSandwich

Description

@CodeSandwich

Explain the problem.

OrderedLists and BulletLists break internally when empty, they start collapsing their content into one entry.

Example

The example consists of 5 examples, list1 to list5. list3 and list4 present the bug. list5 would be very convenient if it worked. It behaves the same for both BulletList and OrderedList.

Tested using a dummy command:

echo "" | pandoc -f markdown -t markdown -L bug.lua

bug.lua has the following content:

function Pandoc()
    -- LIST1
    -- Initialize a list with a single item - works fine
    local list1 = pandoc.BulletList(pandoc.Plain "a")
    -- local list1 = pandoc.OrderedList(pandoc.Plain "a")
    list1.content:insert(pandoc.Plain "b")
    print("list 1", pandoc.json.encode(list1))

    -- LIST2
    -- Initialize a list with a list of items - works fine
    local list2 = pandoc.BulletList({ pandoc.Plain "a" })
    -- local list2 = pandoc.OrderedList({ pandoc.Plain "a" })
    list2.content:insert(pandoc.Plain "b")
    print("list 2", pandoc.json.encode(list2))

    -- LIST3
    -- Initialize a list with an empty list - fails
    local list3 = pandoc.BulletList({ })
    -- local list3 = pandoc.OrderedList({ })
    list3.content:insert(pandoc.Plain "a")
    list3.content:insert(pandoc.Plain "b")
    print("list 3", pandoc.json.encode(list3))

    -- LIST4
    -- Initialize a list with a single item, then remove it - fails
    local list4 = pandoc.BulletList(pandoc.Plain "x")
    -- local list4 = pandoc.OrderedList(pandoc.Plain "x")
    list4.content:remove()
    list4.content:insert(pandoc.Plain "a")
    list4.content:insert(pandoc.Plain "b")
    print("list 4", pandoc.json.encode(list4))

    -- LIST5
    -- Initialize a list with a nil - throws an exception
    -- local list5 = pandoc.BulletList()
    -- local list5 = pandoc.OrderedList()

    return pandoc.Pandoc({
        list1,
        pandoc.HorizontalRule(),
        list2,
        pandoc.HorizontalRule(),
        list3,
        pandoc.HorizontalRule(),
        list4,
    })
end

The output is:

list 1  {"c":[[{"c":[{"c":"a","t":"Str"}],"t":"Plain"}],[{"c":[{"c":"b","t":"Str"}],"t":"Plain"}]],"t":"BulletList"}
list 2  {"c":[[{"c":[{"c":"a","t":"Str"}],"t":"Plain"}],[{"c":[{"c":"b","t":"Str"}],"t":"Plain"}]],"t":"BulletList"}
list 3  {"c":[[{"c":[{"c":"a","t":"Str"}],"t":"Plain"},{"c":[{"c":"b","t":"Str"}],"t":"Plain"}]],"t":"BulletList"}
list 4  {"c":[[{"c":[{"c":"a","t":"Str"}],"t":"Plain"},{"c":[{"c":"b","t":"Str"}],"t":"Plain"}]],"t":"BulletList"}
-   a
-   b

------------------------------------------------------------------------

-   a
-   b

------------------------------------------------------------------------

-   a
    b

------------------------------------------------------------------------

-   a
    b

Pandoc version?

pandoc 3.1.3
Features: +server +lua
Scripting engine: Lua 5.4
OS: Linux

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions