diff --git a/README.md b/README.md index b6bb8eb..b421029 100644 --- a/README.md +++ b/README.md @@ -233,12 +233,12 @@ bacon 0 ``` `github` follows the conventions of GitHub flavored Markdown. It -corresponds to the `pipe` format without alignment colons: +corresponds to the `pipe` format with the same alignment colons: ```pycon >>> print(tabulate(table, headers, tablefmt="github")) | item | qty | -|--------|-------| +|:-------|------:| | spam | 42 | | eggs | 451 | | bacon | 0 | diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 249b3ff..1ada2ee 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -523,16 +523,6 @@ def escape_empty(val): padding=1, with_header_hide=None, ), - "github": TableFormat( - lineabove=Line("|", "-", "|", "|"), - linebelowheader=Line("|", "-", "|", "|"), - linebetweenrows=None, - linebelow=None, - headerrow=DataRow("|", "|", "|"), - datarow=DataRow("|", "|", "|"), - padding=1, - with_header_hide=["lineabove"], - ), "pipe": TableFormat( lineabove=_pipe_line_with_colons, linebelowheader=_pipe_line_with_colons, @@ -720,6 +710,10 @@ def escape_empty(val): ), } +# "github" is an alias for "pipe": both produce GitHub-flavored Markdown with +# alignment colons in the separator row. +_table_formats["github"] = _table_formats["pipe"] + tabulate_formats = sorted(_table_formats.keys()) diff --git a/test/test_output.py b/test/test_output.py index 93de112..32a3619 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -367,9 +367,9 @@ def test_simple_headerless_with_sep_line_with_padding_in_tablefmt(): "Output: simple without headers with sep line with padding in tablefmt" expected = "\n".join( [ - "|------|----------|", + "|:-----|---------:|", "| spam | 41.9999 |", - "|------|----------|", + "|:-----|---------:|", "| eggs | 451 |", ] ) @@ -487,7 +487,7 @@ def test_github(): expected = "\n".join( [ "| strings | numbers |", - "|-----------|-----------|", + "|:----------|----------:|", "| spam | 41.9999 |", "| eggs | 451 |", ] @@ -504,7 +504,7 @@ def test_github_multiline(): [ "| more | more spam |", "| spam eggs | & eggs |", - "|-------------|-------------|", + "|------------:|:------------|", "| 2 | foo |", "| | bar |", ] @@ -513,6 +513,45 @@ def test_github_multiline(): assert_equal(expected, result) +def test_github_with_colalign(): + "Output: github with explicit column alignment" + expected = "\n".join( + [ + "| Name | Age |", + "|:-------|------:|", + "| Alice | 24 |", + "| Bob | 19 |", + ] + ) + result = tabulate( + [["Alice", 24], ["Bob", 19]], + ["Name", "Age"], + tablefmt="github", + colalign=("left", "right"), + ) + assert_equal(expected, result) + + +def test_github_no_alignment(): + "Output: github without alignment hints when numalign/stralign are disabled" + expected = "\n".join( + [ + "| strings | numbers |", + "|-----------|-----------|", + "| spam | 41.9999 |", + "| eggs | 451 |", + ] + ) + result = tabulate( + _test_table, + _test_table_headers, + tablefmt="github", + numalign=None, + stralign=None, + ) + assert_equal(expected, result) + + def test_grid(): "Output: grid with headers" expected = "\n".join(