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
16 changes: 13 additions & 3 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,16 @@ def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
return (separator + colsep.join(values_with_attrs)).rstrip()


def _escape_pipe(value):
return value.replace("|", "|")


def _textile_row_with_attrs(cell_values, colwidths, colaligns):
cell_values[0] += " "
alignment = {"left": "<.", "right": ">.", "center": "=.", "decimal": ">."}
values = "|".join(alignment.get(a, "") + v for a, v in zip(colaligns, cell_values))
values = "|".join(
alignment.get(a, "") + _escape_pipe(v) for a, v in zip(colaligns, cell_values)
)
return f"|{values}|"


Expand Down Expand Up @@ -236,7 +242,11 @@ def _moin_row_with_attrs(celltag, cell_values, colwidths, colaligns, header=""):
"decimal": '<style="text-align: right;">',
}
values_with_attrs = [
"{}{} {} ".format(celltag, alignment.get(a, ""), header + c + header)
"{}{} {} ".format(
celltag,
alignment.get(a, ""),
header + _escape_pipe(c) + header,
)
for c, a in zip(cell_values, colaligns)
]
return "".join(values_with_attrs) + "||"
Expand Down Expand Up @@ -690,7 +700,7 @@ def escape_empty(val):
linebelowheader=None,
linebetweenrows=None,
linebelow=None,
headerrow=DataRow("|_. ", "|_.", "|"),
headerrow=DataRow("|_. ", "|_.", "|", {r"|": r"&#124;"}),
datarow=_textile_row_with_attrs,
padding=1,
with_header_hide=None,
Expand Down
28 changes: 28 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2584,6 +2584,34 @@ def test_moinmoin_headerless():
assert_equal(expected, result)


def test_moinmoin_escapes_pipes():
"Output: moinmoin with literal pipes in cell and header content"
# https://github.com/astanin/python-tabulate/issues/501
expected = "\n".join(
[
'||<style="text-align: left;"> \'\'\' h &#124; 1 \'\'\' '
'||<style="text-align: left;"> \'\'\' h2 \'\'\' ||',
'||<style="text-align: left;"> x &#124; y '
'||<style="text-align: left;"> a &#124;&#124; b ||',
]
)
result = tabulate([["x | y", "a || b"]], headers=["h | 1", "h2"], tablefmt="moinmoin")
assert_equal(expected, result)


def test_textile_escapes_pipes():
"Output: textile with literal pipes in cell and header content"
# https://github.com/astanin/python-tabulate/issues/501
expected = "\n".join(
[
"|_. h &#124; 1 |_. h2 |",
"|<. x &#124; y |<. a &#124;&#124; b |",
]
)
result = tabulate([["x | y", "a || b"]], headers=["h | 1", "h2"], tablefmt="textile")
assert_equal(expected, result)


_test_table_html_headers = ["<strings>", "<&numbers&>"]
_test_table_html = [["spam >", 41.9999], ["eggs &", 451.0]]
_test_table_unsafehtml_headers = ["strings", "numbers"]
Expand Down