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
10 changes: 6 additions & 4 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ def _html_begin_table_without_header(colwidths_ignore, colaligns_ignore):
# this table header will be suppressed if there is a header row
return "<table>\n<tbody>"

def _html_separating_line(colwidths, colaligns):
return "<tr><td colspan=\"%d\"><hr></td></tr>" % len(colwidths)

def _html_row_with_attrs(celltag, unsafe, cell_values, colwidths, colaligns):
alignment = {
Expand Down Expand Up @@ -617,23 +619,23 @@ def escape_empty(val):
),
"html": TableFormat(
lineabove=_html_begin_table_without_header,
linebelowheader="",
linebelowheader=_html_separating_line,
linebetweenrows=None,
linebelow=Line("</tbody>\n</table>", "", "", ""),
headerrow=partial(_html_row_with_attrs, "th", False),
datarow=partial(_html_row_with_attrs, "td", False),
padding=0,
with_header_hide=["lineabove"],
with_header_hide=["lineabove","linebelowheader"],
),
"unsafehtml": TableFormat(
lineabove=_html_begin_table_without_header,
linebelowheader="",
linebelowheader=_html_separating_line,
linebetweenrows=None,
linebelow=Line("</tbody>\n</table>", "", "", ""),
headerrow=partial(_html_row_with_attrs, "th", True),
datarow=partial(_html_row_with_attrs, "td", True),
padding=0,
with_header_hide=["lineabove"],
with_header_hide=["lineabove","linebelowheader"],
),
"latex": TableFormat(
lineabove=_latex_line_begin_tabular,
Expand Down
36 changes: 36 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2586,6 +2586,7 @@ def test_moinmoin_headerless():

_test_table_html_headers = ["<strings>", "<&numbers&>"]
_test_table_html = [["spam >", 41.9999], ["eggs &", 451.0]]
_test_table_html_sepline = [["spam >", 41.9999], SEPARATING_LINE, ["eggs &", 451.0]]
_test_table_unsafehtml_headers = ["strings", "numbers"]
_test_table_unsafehtml = [
["spam", '<font color="red">41.9999</font>'],
Expand Down Expand Up @@ -2655,6 +2656,41 @@ def test_html_headerless():
assert result._repr_html_() == result.str


def test_html_with_separating_line():
expected = "\n".join(
[
"<table>",
"<tbody>",
'<tr><td style="text-align: left;">spam &gt;</td><td style="text-align: right;"> 41.9999</td></tr>',
'<tr><td colspan="2"><hr></td></tr>',
'<tr><td style="text-align: left;">eggs &amp;</td><td style="text-align: right;">451 </td></tr>',
"</tbody>",
"</table>",
]
)
result = tabulate(_test_table_html_sepline, tablefmt="html")
assert_equal(expected, result)


def test_html_headers_no_extra_line():
# ensure that the extra hr isn't accidentally triggered
expected = "\n".join(
[
"<table>",
"<thead>",
'<tr><th style="text-align: left;">&lt;strings&gt; </th><th style="text-align: right;"> &lt;&amp;numbers&amp;&gt;</th></tr>',
"</thead>",
"<tbody>",
'<tr><td style="text-align: left;">spam &gt; </td><td style="text-align: right;"> 41.9999</td></tr>',
'<tr><td colspan="2"><hr></td></tr>',
'<tr><td style="text-align: left;">eggs &amp; </td><td style="text-align: right;"> 451 </td></tr>',
"</tbody>",
"</table>",
]
)
result = tabulate(_test_table_html_sepline,_test_table_html_headers, tablefmt="html")
assert_equal(expected, result)

def test_unsafehtml_headerless():
"Output: unsafe html without headers"
expected = "\n".join(
Expand Down