larstvei / ox-gfm

Github Flavored Markdown Back-End for Org Export Engine
230 stars 44 forks source link

Supporting GFM tables #6

Closed scottoasis closed 8 years ago

scottoasis commented 8 years ago

Implemented org-gfm-table-cell, org-gfm-table-row and org-gfm-table functions to transcode Org Mode Tables info GFM Tables.

R= @larstvei

larstvei commented 8 years ago

Great, thanks for this!

I've tested it, and found a common use-case that's not supported, so it would be great if you could fix that before I merge.

In org-mode separators are optional, and can be placed on any row in the table. In Markdown a separator is mandatory on the second line, but not allowed anywhere else.

Here's an example! In org-mode this is a perfectly valid table:

| single cell |

But the produced table:

| single cell |

is not a valid markdown table. Github Flavored Markdown requires that the second line (implying there must be a second line in the table) consists of pipes | and dashes -.

A possible fix is to put in a dummy-header where there is no header, so produce something like:

|             |
| ----------- |
| single cell |

Which renders like this:

single cell
scottoasis commented 8 years ago

@larstvei Thanks a lot for the information! And a new commit should updates the issue above.

Just out of my curiosity, is a table, with empty cells in it like the one below, being considered valid in Org Mode?

| cell | cell |   |
|------+------+---|
| cell | cell |   |

AFAIK, GFM doesn't allow empty cells.

jingtaozf commented 8 years ago

I have test this script and find following issues:

  1. the hline may be generated as following:
a b
c d

Which is wrong,("--" is too small, github seems accept the minimal length is 3, which should be "---"

  1. the exception occurs when make string with length -1

I have fixed above issues and run it fine by followign patches:

< (defconst gfm-table-left-border "| ")
---
> (defconst gfm-table-left-border "|")
98c98
< (defconst gfm-table-separator " | ")
---
> (defconst gfm-table-separator " |")
138c138
<      (let ((max-width (org-gfm-table-col-width table col info)))
---
>      (let ((max-width (max 3 (org-gfm-table-col-width table col info))))
157c157
<                   (make-string (- width (string-width data))
---
>                   (make-string (max 0 (- width (string-width data)))
larstvei commented 8 years ago

Sorry, I had forgotten about this.

@scottoasis: I changed the no-header to only take place when the table was too short (i.e. just one row), because it seemed like any other table would also get a dummy header like that.

Regarding empty cells, they should be fine in both Org mode and GFM. I've tested a few exports and they look fine!

Added your patches @jingtaozf!

scottoasis commented 8 years ago

@larstvei Thanks for merging and updating! And thanks @jingtaozf for testing and patching! :-) Now can't wait to see other hackers using it.

Btw, @jingtaozf any plan for working on improve this project?

blaenk commented 8 years ago

Thanks for this! I just found out about it. I'm using it so that I can use orgtbl-mode to easily create tables, then I select the table with a region and do M-x org-gfm-convert-region-to-md, but it doesn't seem to line up well:

| First | Second |
|-------+--------|
| one   | two    |

becomes:

| First | Second |
|----- |------ |
| one   | two    |

Notice that it's not aligned.

Also, if the second row (the separator row) has dashes immediately following the cell separator, i.e. |- instead of | -, then when I press C-c C-c on the table, it ends up interpreting |- as a directive to turn that row into an org-table separator. In other words, with the above:

| First | Second |
|----- |------ |
| one   | two    |

If I press C-c C-c on the table, it becomes this:

| First | Second |
|-------+--------|
| one   | two    |

This effectively ends up turning the just-converted-to-gfm-table back to org-table.

I think this is because |- is a directive to org-table to turn it into that kind of separator. See this:

Any line starting with ‘|-’ is considered as a horizontal separator line and will be expanded on the next re-align to span the whole table width.

If instead we have a space after the cell separator, i.e. | -, it works correctly, and I'm able to align even a gfm-format table! That is, given this:

| First | Second |
| ----- | ------ |
| one   | two    |

I can press C-c C-c on the table after adding in content and so on, and the table remains gfm-formatted!