Open wohali opened 3 years ago
Providing an example:
.. table::
+---------------------------------------+--------------------------------------------------+
| Header |Header |
+=======================================+==================================================+
|.. raw:: html | |
| |N/A |
| <span title="Tooltip">Hello</span> | |
+---------------------------------------+--------------------------------------------------+
Which renders as:
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:
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.
One (incomplete) idea would be to:
_preprocess
so that DataPropertyExtractor
computes the correct column widths to useself._write_value_row_separator()
, since you wouldn't do that for those that were a result of the split.
RST tables can handle cells with line breaks in them. It would be nice if pytablewriter supported them, rather than discarding them all.