Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ format:

`orgtbl` follows the conventions of Emacs
[org-mode](http://orgmode.org/manual/Tables.html), and is editable also
in the minor orgtbl-mode. Hence its name:
in the minor orgtbl-mode. Vertical bars inside cell values and headers are
escaped as `\vert{}` so that they do not create extra columns. For example:

```pycon
>>> print(tabulate(table, headers, tablefmt="orgtbl"))
Expand Down
5 changes: 3 additions & 2 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ def make_header_line(is_header, colwidths, colaligns):


GITHUB_ESCAPE_RULES = {r"|": r"\|"}
ORGTBL_ESCAPE_RULES = {"|": r"\vert{}"}


def _rst_escape_first_column(rows, headers):
Expand Down Expand Up @@ -535,8 +536,8 @@ def escape_empty(val):
linebelowheader=Line("|", "-", "+", "|"),
linebetweenrows=None,
linebelow=None,
headerrow=DataRow("|", "|", "|"),
datarow=DataRow("|", "|", "|"),
headerrow=DataRow("|", "|", "|", ORGTBL_ESCAPE_RULES),
datarow=DataRow("|", "|", "|", ORGTBL_ESCAPE_RULES),
padding=1,
with_header_hide=None,
),
Expand Down
14 changes: 14 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,20 @@ def test_orgtbl():
assert_equal(expected, result)


@mark.parametrize("headers", [[], ["left|right", "value"]])
@mark.parametrize("value", ["a|b", "a|b\nc|d"])
def test_orgtbl_escapes_cell_delimiters(headers, value):
result = tabulate([[value, "*bold*"]], headers=headers, tablefmt="orgtbl")
data_lines = [line for line in result.splitlines() if "+" not in line]
assert all(line.count("|") == 3 for line in data_lines)
assert r"a\vert{}b" in result
assert "*bold*" in result
if headers:
assert r"left\vert{}right" in result
if "\n" in value:
assert r"c\vert{}d" in result


def test_orgtbl_headerless():
"Output: orgtbl without headers"
expected = "\n".join(["| spam | 41.9999 |", "| eggs | 451 |"])
Expand Down