PrettyTables.jl icon indicating copy to clipboard operation
PrettyTables.jl copied to clipboard

Markdown text format doesn't produce markdown alignment

Open thchr opened this issue 4 years ago • 10 comments

I may be misunderstanding or assuming more of the tf=markdown option than is intended, but I had been hoping to use this option to easily generate files with aligned markdown-formatted tables.

The issue is best illustrated by example:

julia> pretty_table(rand(2,2), tf=markdown, alignment=:l)
| Col. 1              | Col. 2              |
|---------------------|---------------------|
| 0.7347007259374732  | 0.1569457707542219  |
| 0.45803533418867315 | 0.09505240329471065 |

julia> pretty_table(rand(2,2), tf=markdown, alignment=:c)
|       Col. 1       |       Col. 2        |
|--------------------|---------------------|
| 0.5909102056737221 | 0.29421329002977337 |
| 0.958966567581609  | 0.5793411919956417  |

julia> pretty_table(rand(2,2), tf=markdown, alignment=:r)
|              Col. 1 |             Col. 2 |
|---------------------|--------------------|
| 0.34038845162816256 | 0.1420028612960429 |
|  0.2574471632177133 | 0.5145358495723247 |

While the text entries are perfectly aligned, I had been hoping for :--, :--:, and --: header lines instead of just --: without this, markdown table won't be properly aligned if subsequently rendered as Markdown. I.e., I had been hoping the output would be:

julia> pretty_table(rand(2,2), tf=markdown, alignment=:l)
| Col. 1              | Col. 2              |
|:--------------------|:--------------------|
| 0.7347007259374732  | 0.1569457707542219  |
| 0.45803533418867315 | 0.09505240329471065 |

julia> pretty_table(rand(2,2), tf=markdown, alignment=:c)
|       Col. 1       |       Col. 2        |
|:------------------:|:-------------------:|
| 0.5909102056737221 | 0.29421329002977337 |
| 0.958966567581609  | 0.5793411919956417  |

julia> pretty_table(rand(2,2), tf=markdown, alignment=:r)
|              Col. 1 |             Col. 2 |
|--------------------:|-------------------:|
| 0.34038845162816256 | 0.1420028612960429 |
|  0.2574471632177133 | 0.5145358495723247 |

PS. Thanks for a great package!

thchr avatar Aug 10 '20 15:08 thchr

Hi @thchr

The markdown is just a table format for the Text backend. Hence, the alignment just aligns the cells when printing the table. What you want is a Markdown backend, where it will consider such things. It is planned and should not be that difficult to implement.

PS. Thanks for a great package!

Thanks! I am glad it is being useful :)

ronisbr avatar Aug 10 '20 17:08 ronisbr

When you say backend for markdown, what formats can it have? The different flavours of Markdown? And how do you plan to implement it? I may be able to put some time in this

DhruvaSambrani avatar Nov 28 '20 04:11 DhruvaSambrani

I am just thinking about a table like the text backend provides. The difference will be in the line below the header (which will have the alignment), and in the highlighter.

I started to work on this, but I still not sure how can I do it. The "easiest" would be copy the entire text backend and just modify what I want, but that's bad. Hence, I started to split text backend in many functions so that they can be reused. I think I might be reaching the point in which those derivative backend would be simple to implement.

ronisbr avatar Nov 28 '20 17:11 ronisbr

Why does this need a different backend though? At the end, we are still returning a string right? Or maybe, we should depend on Markdown.jl (stdlib, so anyway installed) and return an MD object. Have a tf_jlmd which is parsed by Markdown.jl?

DhruvaSambrani avatar Nov 28 '20 21:11 DhruvaSambrani

We have two things here:

  1. Text backend supports a lot of things that is not supported in markdown (like removing vertical lines, adding horizontal lines, ANSI colors, etc.).
  2. We need to modify the header line according to the alignment of the column. This is not supported by the text backend.

That's why we need a new backend. A very simple one that will use almost everything from the text backend.

ronisbr avatar Nov 28 '20 21:11 ronisbr

Right, I see what you mean.

DhruvaSambrani avatar Nov 28 '20 21:11 DhruvaSambrani

I would also like this feature!

kleinschmidt avatar Jun 28 '21 20:06 kleinschmidt

Yes... after the refactoring I performed, it should be very easy to implement it. I will try to find some time to do this :)

ronisbr avatar Jun 28 '21 22:06 ronisbr

Any updates or thoughts here? Preferably would support the same set of tables that Pandoc does https://pandoc.org/MANUAL%202.html#tables

tbeason avatar Aug 03 '22 13:08 tbeason

Sorry for the delay :D It took a while to clean the code and now we can add the Markdown backend very easily. However, I will only be able to work on this after I submit the PRs to make PrettyTables.jl the default printing mechanism for HTML and LaTeX.

ronisbr avatar Aug 03 '22 18:08 ronisbr

I'm looking forward to this one, the convenience level is 1000 for a few workflows I have sharing datasets.

mahiki avatar Jan 19 '23 05:01 mahiki

Yeah, sorry for the delay :) I did a huge revamp to split functions in the text backend. Now, it should be easy to create the markdown backend with this functionality. I will try to find some time to implement this.

ronisbr avatar Jan 19 '23 13:01 ronisbr

+1 this would also be great to have!

sethaxen avatar Aug 21 '23 20:08 sethaxen

Hi! After all those years, I am starting to implement the markdown mode finally. However, I reached a problem. Since markdown tables does not allow multiple header lines, what should we do with the sub-headers?

ronisbr avatar Oct 15 '23 00:10 ronisbr

I generally do not use them anyway, so I would not mind if they were not allowed in markdown.

Tyler Beason tbeason.com

On Sat, Oct 14, 2023, 8:34 PM Ronan Arraes Jardim Chagas < @.***> wrote:

Hi! After all those years, I am starting to implement the markdown mode finally. However, I reached a problem. Since markdown tables does not allow multiple header lines, what should we do with the sub-headers?

— Reply to this email directly, view it on GitHub https://github.com/ronisbr/PrettyTables.jl/issues/67#issuecomment-1763220159, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACSBDQMV73CV343ANSEQ4N3X7MVQPANCNFSM4P2CLZCA . You are receiving this because you commented.Message ID: @.***>

tbeason avatar Oct 15 '23 10:10 tbeason

Done! PrettyTables.jl now has a true markdown back end :)

julia> A = rand(4, 4)
4×4 Matrix{Float64}:
 0.057819  0.199259  0.915048  0.214648
 0.808271  0.914835  0.260779  0.288038
 0.777272  0.621872  0.97033   0.478155
 0.982788  0.707421  0.844574  0.741363

julia> pretty_table(A, alignment = [:l, :c, :r, :r], backend = Val(:markdown))
| **Col. 1** | **Col. 2** | **Col. 3** | **Col. 4** |
|:-----------|:----------:|-----------:|-----------:|
| 0.057819   | 0.199259   | 0.915048   | 0.214648   |
| 0.808271   | 0.914835   | 0.260779   | 0.288038   |
| 0.777272   | 0.621872   | 0.97033    | 0.478155   |
| 0.982788   | 0.707421   | 0.844574   | 0.741363   |

ronisbr avatar Oct 29 '23 22:10 ronisbr

Amazing! Many thanks!

thchr avatar Oct 30 '23 06:10 thchr