Skip to content

Commit

Permalink
Allow alignments in Markdown tables.
Browse files Browse the repository at this point in the history
Closes #53, superseds #260.
  • Loading branch information
kdeldycke committed Apr 4, 2023
1 parent 83fd4fb commit f4df50c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,16 @@ eggs 451
bacon 0
```

`github` follows the conventions of GitHub flavored Markdown. It
corresponds to the `pipe` format without alignment colons:
`github` follows the conventions of GitHub flavored Markdown. This
format uses colons to indicate column alignment:

```pycon
>>> print(tabulate(table, headers, tablefmt="github"))
| item | qty |
|--------|-------|
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
| item | qty |
| :------ | ----: |
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
```

`grid` is like tables formatted by Emacs'
Expand Down
40 changes: 24 additions & 16 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,29 @@ def _is_separating_line(row):
return is_sl


def _pipe_segment_with_colons(align, colwidth):
def _segment_with_colons(align, colwidth, sep="-"):
"""Return a segment of a horizontal line with optional colons which
indicate column's alignment (as in `pipe` output format)."""
w = colwidth
if align in ["right", "decimal"]:
return ("-" * (w - 1)) + ":"
return (sep * (w - 1)) + ":"
elif align == "center":
return ":" + ("-" * (w - 2)) + ":"
return ":" + (sep * (w - 2)) + ":"
elif align == "left":
return ":" + ("-" * (w - 1))
return ":" + (sep * (w - 1))
else:
return "-" * w
return sep * w


def _pipe_line_with_colons(colwidths, colaligns):
def _line_with_colons(colwidths, colaligns, begin="|", hline="|", sep="-", end="|"):
"""Return a horizontal line with optional colons to indicate column's
alignment (as in `pipe` output format)."""
alignment (as in `pipe` and `github` output format)."""
if not colaligns: # e.g. printing an empty data frame (github issue #15)
colaligns = [""] * len(colwidths)
segments = [_pipe_segment_with_colons(a, w) for a, w in zip(colaligns, colwidths)]
return "|" + "|".join(segments) + "|"
segments = [
_segment_with_colons(a, w, sep=sep) for a, w in zip(colaligns, colwidths)
]
return begin + hline.join(segments) + end


def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
Expand Down Expand Up @@ -470,18 +472,24 @@ def escape_empty(val):
with_header_hide=None,
),
"github": TableFormat(
lineabove=Line("|", "-", "|", "|"),
linebelowheader=Line("|", "-", "|", "|"),
lineabove=partial(
_line_with_colons, begin="| ", hline=" | ", sep="-", end=" |"
),
linebelowheader=partial(
_line_with_colons, begin="| ", hline=" | ", sep="-", end=" |"
),
linebetweenrows=None,
linebelow=None,
headerrow=DataRow("|", "|", "|"),
datarow=DataRow("|", "|", "|"),
padding=1,
headerrow=DataRow("| ", " | ", " |"),
datarow=DataRow("| ", " | ", " |"),
padding=0,
with_header_hide=["lineabove"],
),
"pipe": TableFormat(
lineabove=_pipe_line_with_colons,
linebelowheader=_pipe_line_with_colons,
lineabove=partial(_line_with_colons, begin="|", hline="|", sep="-", end="|"),
linebelowheader=partial(
_line_with_colons, begin="|", hline="|", sep="-", end="|"
),
linebetweenrows=None,
linebelow=None,
headerrow=DataRow("|", "|", "|"),
Expand Down
2 changes: 1 addition & 1 deletion test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def test_github():
expected = "\n".join(
[
"| strings | numbers |",
"|-----------|-----------|",
"| :-------- | --------: |",
"| spam | 41.9999 |",
"| eggs | 451 |",
]
Expand Down

0 comments on commit f4df50c

Please sign in to comment.