astanin / python-tabulate

Pretty-print tabular data in Python, a library and a command-line utility. Repository migrated from bitbucket.org/astanin/python-tabulate.
https://pypi.org/project/tabulate/
MIT License
2.08k stars 162 forks source link

Add new Pandoc grid table format #332

Open Bruntaz opened 1 month ago

Bruntaz commented 1 month ago

The current "grid" table format in Tabulate's documentation says:

It corresponds to grid_tables in Pandoc Markdown extensions

It is true that the tables are similar to each other, but there is a nasty interaction between the padding that Tabulate provides and how Pandoc interprets grid tables. Specifically, the documentation for Pandoc's grid_tables states:

The cells of grid tables may contain arbitrary block elements (multiple paragraphs, code blocks, lists, etc.).

So each cell in the grid table is treated as essentially a new Markdown scope. In the Markdown spec, Markdown indented by 4 spaces is treated as a verbatim code block. For a column of integers, Tabulate will default to right-aligning the content in the cells, which Pandoc then interprets as a verbatim code block instead of standard text. For example:

+------------+------------+------------+
|   Column 1 | Column 2   |   Column 3 |
+============+============+============+
|          1 | a          |       1.23 |
+------------+------------+------------+
|          2 | b          |      12.3  |
+------------+------------+------------+
|          3 | c          |     123    |
+------------+------------+------------+

is rendered as:

image

Forcing Tabulate to left-align the cells does allow it to render properly:

+------------+------------+------------+
| Column 1   | Column 2   | Column 3   |
+============+============+============+
| 1          | a          | 1.23       |
+------------+------------+------------+
| 2          | b          | 12.3       |
+------------+------------+------------+
| 3          | c          | 123        |
+------------+------------+------------+

image

But Pandoc does support syntax for right-aligning cell content (using a colon, similar to pipe tables):

+------------+------------+------------+
| Column 1   | Column 2   | Column 3   |
+===========:+============+===========:+
| 1          | a          | 1.23       |
+------------+------------+------------+
| 2          | b          | 12.3       |
+------------+------------+------------+
| 3          | c          | 123        |
+------------+------------+------------+

image

I think Tabulate should provide a new output format, pandoc_grid or grid_tables or something that produces a table similar to existing grid tables, but with cell content always left-aligned and inserting appropriate :s in the row below the header to select the alignment that Pandoc will use.