Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown text format doesn't produce markdown alignment #67

Closed
thchr opened this issue Aug 10, 2020 · 17 comments
Closed

Markdown text format doesn't produce markdown alignment #67

thchr opened this issue Aug 10, 2020 · 17 comments

Comments

@thchr
Copy link
Contributor

thchr commented Aug 10, 2020

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!

@ronisbr
Copy link
Owner

ronisbr commented Aug 10, 2020

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 :)

@DhruvaSambrani
Copy link
Contributor

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

@ronisbr
Copy link
Owner

ronisbr commented Nov 28, 2020

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.

@DhruvaSambrani
Copy link
Contributor

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?

@ronisbr
Copy link
Owner

ronisbr commented Nov 28, 2020

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.

@DhruvaSambrani
Copy link
Contributor

Right, I see what you mean.

@kleinschmidt
Copy link

I would also like this feature!

@ronisbr
Copy link
Owner

ronisbr commented Jun 28, 2021

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

@tbeason
Copy link

tbeason commented Aug 3, 2022

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

@ronisbr
Copy link
Owner

ronisbr commented Aug 3, 2022

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.

@mahiki
Copy link

mahiki commented Jan 19, 2023

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

@ronisbr
Copy link
Owner

ronisbr commented Jan 19, 2023

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.

@sethaxen
Copy link

+1 this would also be great to have!

@ronisbr
Copy link
Owner

ronisbr commented Oct 15, 2023

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?

@tbeason
Copy link

tbeason commented Oct 15, 2023 via email

@ronisbr
Copy link
Owner

ronisbr commented Oct 29, 2023

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   |

@thchr
Copy link
Contributor Author

thchr commented Oct 30, 2023

Amazing! Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants