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

Support line break handling for RST output #32

Open wohali opened 3 years ago

wohali commented 3 years ago

RST tables can handle cells with line breaks in them. It would be nice if pytablewriter supported them, rather than discarding them all.

movermeyer commented 1 year ago

Providing an example:

.. table::

  +---------------------------------------+--------------------------------------------------+
  |             Header                    |Header                                            |
  +=======================================+==================================================+
  |.. raw:: html                          |                                                  |
  |                                       |N/A                                               |
  |   <span title="Tooltip">Hello</span>  |                                                  |
  +---------------------------------------+--------------------------------------------------+

Which renders as:

image

Using pytablewriter:

import pytablewriter

rst_tooltip = ".. raw:: html\n\n   <span title=\"Tooltip\">Hello</span>"

writer = pytablewriter.RstGridTableWriter()

writer.headers = ["Header", "Header"]
writer.type_hints = [pytablewriter.String, pytablewriter.String]
writer.value_matrix = [
  [rst_tooltip, "N/A"]
]
with open("test.rst", "w") as fout:
  writer.stream = fout
  writer.write_table()

It outputs:

.. table::

    +----------------------------------------------------+------+
    |                       Header                       |Header|
    +====================================================+======+
    |.. raw:: html     <span title="Tooltip">Hello</span>|N/A   |
    +----------------------------------------------------+------+

which renders as:

image
movermeyer commented 1 year ago

Interestingly, you get a different behaviour if you use a wrapper to wrap the string in a class:

import pytablewriter

class Wrapper:
  def __init__(self, child) -> None:
    self.child = child

  def __str__(self) -> str:
    return self.child

rst_tooltip = ".. raw:: html\n\n   <span title=\"Tooltip\">Hello</span>"

writer = pytablewriter.RstGridTableWriter()

writer.headers = ["Header", "Header"]
writer.type_hints = [pytablewriter.String, pytablewriter.String]
writer.value_matrix = [
  [Wrapper(rst_tooltip), "N/A"]
]
with open("test.rst", "w") as fout:
  writer.stream = fout
  writer.write_table()

Which gives you:

.. table::

    +----------------------------------------------------+------+
    |                       Header                       |Header|
    +====================================================+======+
    |.. raw:: html

   <span title="Tooltip">Hello</span>|N/A   |
    +----------------------------------------------------+------+

Which doesn't strip the newlines, but also doesn't format the cells correctly either and isn't valid reST.

I expected these to produce the same result.

movermeyer commented 1 year ago

One (incomplete) idea would be to: