Skip to content

Commit 8014ec6

Browse files
kdeldyckeastanin
authored andcommitted
Fix alignments in github tables
Closes #53, follow-up on #261
1 parent 7c70bf4 commit 8014ec6

3 files changed

Lines changed: 49 additions & 16 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,12 @@ bacon 0
233233
```
234234

235235
`github` follows the conventions of GitHub flavored Markdown. It
236-
corresponds to the `pipe` format without alignment colons:
236+
corresponds to the `pipe` format with the same alignment colons:
237237

238238
```pycon
239239
>>> print(tabulate(table, headers, tablefmt="github"))
240240
| item | qty |
241-
|--------|-------|
241+
|:-------|------:|
242242
| spam | 42 |
243243
| eggs | 451 |
244244
| bacon | 0 |

tabulate/__init__.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -522,16 +522,6 @@ def escape_empty(val):
522522
padding=1,
523523
with_header_hide=None,
524524
),
525-
"github": TableFormat(
526-
lineabove=Line("|", "-", "|", "|"),
527-
linebelowheader=Line("|", "-", "|", "|"),
528-
linebetweenrows=None,
529-
linebelow=None,
530-
headerrow=DataRow("|", "|", "|"),
531-
datarow=DataRow("|", "|", "|"),
532-
padding=1,
533-
with_header_hide=["lineabove"],
534-
),
535525
"pipe": TableFormat(
536526
lineabove=_pipe_line_with_colons,
537527
linebelowheader=_pipe_line_with_colons,
@@ -719,6 +709,10 @@ def escape_empty(val):
719709
),
720710
}
721711

712+
# "github" is an alias for "pipe": both produce GitHub-flavored Markdown with
713+
# alignment colons in the separator row.
714+
_table_formats["github"] = _table_formats["pipe"]
715+
722716

723717
tabulate_formats = sorted(_table_formats.keys())
724718

test/test_output.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ def test_simple_headerless_with_sep_line_with_padding_in_tablefmt():
369369
"Output: simple without headers with sep line with padding in tablefmt"
370370
expected = "\n".join(
371371
[
372-
"|------|----------|",
372+
"|:-----|---------:|",
373373
"| spam | 41.9999 |",
374-
"|------|----------|",
374+
"|:-----|---------:|",
375375
"| eggs | 451 |",
376376
]
377377
)
@@ -489,7 +489,7 @@ def test_github():
489489
expected = "\n".join(
490490
[
491491
"| strings | numbers |",
492-
"|-----------|-----------|",
492+
"|:----------|----------:|",
493493
"| spam | 41.9999 |",
494494
"| eggs | 451 |",
495495
]
@@ -506,7 +506,7 @@ def test_github_multiline():
506506
[
507507
"| more | more spam |",
508508
"| spam eggs | & eggs |",
509-
"|-------------|-------------|",
509+
"|------------:|:------------|",
510510
"| 2 | foo |",
511511
"| | bar |",
512512
]
@@ -515,6 +515,45 @@ def test_github_multiline():
515515
assert_equal(expected, result)
516516

517517

518+
def test_github_with_colalign():
519+
"Output: github with explicit column alignment"
520+
expected = "\n".join(
521+
[
522+
"| Name | Age |",
523+
"|:-------|------:|",
524+
"| Alice | 24 |",
525+
"| Bob | 19 |",
526+
]
527+
)
528+
result = tabulate(
529+
[["Alice", 24], ["Bob", 19]],
530+
["Name", "Age"],
531+
tablefmt="github",
532+
colalign=("left", "right"),
533+
)
534+
assert_equal(expected, result)
535+
536+
537+
def test_github_no_alignment():
538+
"Output: github without alignment hints when numalign/stralign are disabled"
539+
expected = "\n".join(
540+
[
541+
"| strings | numbers |",
542+
"|-----------|-----------|",
543+
"| spam | 41.9999 |",
544+
"| eggs | 451 |",
545+
]
546+
)
547+
result = tabulate(
548+
_test_table,
549+
_test_table_headers,
550+
tablefmt="github",
551+
numalign=None,
552+
stralign=None,
553+
)
554+
assert_equal(expected, result)
555+
556+
518557
def test_grid():
519558
"Output: grid with headers"
520559
expected = "\n".join(

0 commit comments

Comments
 (0)