thombashi / pytablewriter

pytablewriter is a Python library to write a table in various formats: AsciiDoc / CSV / Elasticsearch / HTML / JavaScript / JSON / LaTeX / LDJSON / LTSV / Markdown / MediaWiki / NumPy / Excel / Pandas / Python / reStructuredText / SQLite / TOML / TSV.
https://pytablewriter.rtfd.io/
MIT License
610 stars 43 forks source link

Fix Markdown table alignment row to conform to GFM #35

Closed shawalli closed 3 years ago

shawalli commented 3 years ago

Currently, Markdown tables look like this:

| foo | foobar | baz |
|:----|--------|----:|
| aaa | bb     | c   |

According to GitHub, there should be a space between each pipe and the alignment string, like so:

| foo | foobar | baz |
| :-- | ------ | --: |
| aaa | bb     | c   |

This discrepancy causes linters (e.g. preettier) which do conform to GFM to constantly change the header row for tables that have been generated with this library.

shawalli commented 3 years ago

I've identified the fix and am willing to contribute it, but I wanted to make sure it would be accepted before spending any more time on it.

thombashi commented 3 years ago

@shawalli Thank you for your feedback.

Pull requests would always be welcome.

According to GitHub, there should be a space between each pipe and the alignment string

I just wonder which part describes that spec?

shawalli commented 3 years ago

Here's the part from the spec that describes table alignment.

The delimiter row consists of cells whose only content are hyphens (-), and optionally, a leading or trailing colon (:), or both, to indicate left, right, or center alignment respectively.

Here is the example Markdown table incuded right below this text in the spec

| foo | bar |
| --- | --- |
| baz | bim |

So, I think this is where it could have been more explicit. In all of the table examples, there is always a space between the separator character (i.;e. |) and any content, including alignment characters. So in the above documentation, while is doesn't say that there should be a space specifically in the alignment cells, it implies it by always having a space before and/or after the separator.

After investigating this further as part of this reply, perhaps the "right" fix is to have the alignment row respect the margin argument, so that a margin of 1 would produce this behavior. This way, all row/cell types (header, alignment,content) would obey margin. Right now, it uses margin to extend the alignment characters, but the calculation could be modified to left- and right-pad spaces by margin characters on each side.

What are your thoughts?

thombashi commented 3 years ago

In my opinion, examples are just examples, not a spec unless explicitly stated. GFM accepts tables even if without spaces around hyphens (---). However, it might be good idea to match the output table format with popular formatter tools like prettier.

the alignment row respect the margin argument, so that a margin of 1 would produce this behavior. This way, all row/cell types (header, alignment,content) would obey margin.

Sounds good.