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

Support line break handling for RST output #32

Open
wohali opened this issue Nov 29, 2020 · 3 comments
Open

Support line break handling for RST output #32

wohali opened this issue Nov 29, 2020 · 3 comments

Comments

@wohali
Copy link

wohali commented Nov 29, 2020

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

@movermeyer
Copy link
Contributor

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
Copy link
Contributor

movermeyer commented Oct 16, 2023

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
Copy link
Contributor

One (incomplete) idea would be to:

  • Split the cells in a row by newline, before the _preprocess so that DataPropertyExtractor computes the correct column widths to use
  • Somehow know whether each row is a result of the split in the previous step or not, so you can know whether to call self._write_value_row_separator(), since you wouldn't do that for those that were a result of the split.

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

No branches or pull requests

3 participants