oli-obk / pandoc-ast

35 stars 11 forks source link

Crashes when parsing AST of table with caption. #11

Open xfbs opened 3 years ago

xfbs commented 3 years ago

Hey,

really awesome work here! I noticed a crash when parsing the AST of this table tho:

| First Header | Second Header |
|:-------------|:--------------|
| Content Cell | Content Cell  |

: Table example {#tbl:table1}

thread '' panicked at 'json is not in the pandoc format: Error("invalid type: map, expected a tuple of size 2", line: 283, column: 5)

Here is the raw JSON:

{
  "blocks": [
    {
      "t": "Table",
      "c": [
        [
          "",
          [],
          []
        ],
        {
          "t": "Caption",
          "c": [
            null,
            [
              {
                "t": "Plain",
                "c": [
                  {
                    "t": "Str",
                    "c": "Table"
                  },
                  {
                    "t": "Space"
                  },
                  {
                    "t": "Str",
                    "c": "example"
                  },
                  {
                    "t": "Space"
                  },
                  {
                    "t": "Str",
                    "c": "{#tbl:table1}"
                  }
                ]
              }
            ]
          ]
        },
        [
          [
            {
              "t": "AlignLeft"
            },
            {
              "t": "ColWidthDefault"
            }
          ],
          [
            {
              "t": "AlignLeft"
            },
            {
              "t": "ColWidthDefault"
            }
          ]
        ],
        {
          "t": "TableHead",
          "c": [
            [
              "",
              [],
              []
            ],
            [
              {
                "t": "Row",
                "c": [
                  [
                    "",
                    [],
                    []
                  ],
                  [
                    {
                      "t": "Cell",
                      "c": [
                        [
                          "",
                          [],
                          []
                        ],
                        {
                          "t": "AlignDefault"
                        },
                        {
                          "t": "RowSpan",
                          "c": 1
                        },
                        {
                          "t": "ColSpan",
                          "c": 1
                        },
                        [
                          {
                            "t": "Plain",
                            "c": [
                              {
                                "t": "Str",
                                "c": "First"
                              },
                              {
                                "t": "Space"
                              },
                              {
                                "t": "Str",
                                "c": "Header"
                              }
                            ]
                          }
                        ]
                      ]
                    },
                    {
                      "t": "Cell",
                      "c": [
                        [
                          "",
                          [],
                          []
                        ],
                        {
                          "t": "AlignDefault"
                        },
                        {
                          "t": "RowSpan",
                          "c": 1
                        },
                        {
                          "t": "ColSpan",
                          "c": 1
                        },
                        [
                          {
                            "t": "Plain",
                            "c": [
                              {
                                "t": "Str",
                                "c": "Second"
                              },
                              {
                                "t": "Space"
                              },
                              {
                                "t": "Str",
                                "c": "Header"
                              }
                            ]
                          }
                        ]
                      ]
                    }
                  ]
                ]
              }
            ]
          ]
        },
        [
          {
            "t": "TableBody",
            "c": [
              [
                "",
                [],
                []
              ],
              {
                "t": "RowHeadColumns",
                "c": 0
              },
              [],
              [
                {
                  "t": "Row",
                  "c": [
                    [
                      "",
                      [],
                      []
                    ],
                    [
                      {
                        "t": "Cell",
                        "c": [
                          [
                            "",
                            [],
                            []
                          ],
                          {
                            "t": "AlignDefault"
                          },
                          {
                            "t": "RowSpan",
                            "c": 1
                          },
                          {
                            "t": "ColSpan",
                            "c": 1
                          },
                          [
                            {
                              "t": "Plain",
                              "c": [
                                {
                                  "t": "Str",
                                  "c": "Content"
                                },
                                {
                                  "t": "Space"
                                },
                                {
                                  "t": "Str",
                                  "c": "Cell"
                                }
                              ]
                            }
                          ]
                        ]
                      },
                      {
                        "t": "Cell",
                        "c": [
                          [
                            "",
                            [],
                            []
                          ],
                          {
                            "t": "AlignDefault"
                          },
                          {
                            "t": "RowSpan",
                            "c": 1
                          },
                          {
                            "t": "ColSpan",
                            "c": 1
                          },
                          [
                            {
                              "t": "Plain",
                              "c": [
                                {
                                  "t": "Str",
                                  "c": "Content"
                                },
                                {
                                  "t": "Space"
                                },
                                {
                                  "t": "Str",
                                  "c": "Cell"
                                }
                              ]
                            }
                          ]
                        ]
                      }
                    ]
                  ]
                }
              ]
            ]
          }
        ],
        {
          "t": "TableFoot",
          "c": [
            [
              "",
              [],
              []
            ],
            []
          ]
        }
      ]
    }
  ],
  "pandoc-api-version": [
    1,
    21
  ],
  "meta": {}
}

I hope that is enough information to reproduce this issue. I'm using pandoc version 2.10.1, pandoc crate 0.8.2 and pandoc-ast 0.8.0.

oli-obk commented 3 years ago

I took the liberty of formatting your json to make it easier to read.

So I believe the issue is that the format changed and I should have updated pandoc-ast to handle the pandoc-api-version change to use structs instead of tuples (TableFoot, TableBody, TableHead and Caption at least should now be structs that eat up the "t": "TableFoot" and similar. I don't have the time and setup to work on this right now, so any implementation work would be appreciated.

xfbs commented 3 years ago

Thanks for looking into it, I already figured that it might be due to some change in the serialisation. I'll take a look at the code tomorrow and see if I can implement anything useful, should probably be able to get it fixed and tested tomorrow, I'll send a pull request your way if I make any useful progress.

shonfeder commented 3 years ago

Just noting that, afaict, this crashes result from any tables at all, whether or not they have captions. At least against pandoc 2.9.2.1 (using pandoc_ast 0.8.0).

Here's failing table:

| A | B | C |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 2 |

Which (when using filter) panics with:

thread 'main' panicked at 'json is not in the pandoc format: Error("invalid length 0, expected a tuple of size 3", line: 1350, column: 5)

I don't mean to add to your workload, but I think it could be helpful for users to add a warning to the readme noting that support for the pandoc api is not actively maintained, and indicating the current maximum pandoc version supported.