Skip to content

Commit e711ff9

Browse files
committed
Allow alignments in Markdown tables.
Closes astanin#53, superseds astanin#260.
1 parent 83fd4fb commit e711ff9

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,16 @@ eggs 451
213213
bacon 0
214214
```
215215

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

219219
```pycon
220220
>>> print(tabulate(table, headers, tablefmt="github"))
221-
| item | qty |
222-
|--------|-------|
223-
| spam | 42 |
224-
| eggs | 451 |
225-
| bacon | 0 |
221+
| item | qty |
222+
| :----- | ----: |
223+
| spam | 42 |
224+
| eggs | 451 |
225+
| bacon | 0 |
226226
```
227227

228228
`grid` is like tables formatted by Emacs'

tabulate/__init__.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,29 @@ def _is_separating_line(row):
110110
return is_sl
111111

112112

113-
def _pipe_segment_with_colons(align, colwidth):
113+
def _segment_with_colons(align, colwidth, sep="-"):
114114
"""Return a segment of a horizontal line with optional colons which
115115
indicate column's alignment (as in `pipe` output format)."""
116116
w = colwidth
117117
if align in ["right", "decimal"]:
118-
return ("-" * (w - 1)) + ":"
118+
return (sep * (w - 1)) + ":"
119119
elif align == "center":
120-
return ":" + ("-" * (w - 2)) + ":"
120+
return ":" + (sep * (w - 2)) + ":"
121121
elif align == "left":
122-
return ":" + ("-" * (w - 1))
122+
return ":" + (sep * (w - 1))
123123
else:
124-
return "-" * w
124+
return sep * w
125125

126126

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

135137

136138
def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
@@ -470,8 +472,12 @@ def escape_empty(val):
470472
with_header_hide=None,
471473
),
472474
"github": TableFormat(
473-
lineabove=Line("|", "-", "|", "|"),
474-
linebelowheader=Line("|", "-", "|", "|"),
475+
lineabove=partial(
476+
_line_with_colons, begin="| ", hline=" | ", sep="-", end=" |"
477+
),
478+
linebelowheader=partial(
479+
_line_with_colons, begin="| ", hline=" | ", sep="-", end=" |"
480+
),
475481
linebetweenrows=None,
476482
linebelow=None,
477483
headerrow=DataRow("|", "|", "|"),
@@ -480,8 +486,10 @@ def escape_empty(val):
480486
with_header_hide=["lineabove"],
481487
),
482488
"pipe": TableFormat(
483-
lineabove=_pipe_line_with_colons,
484-
linebelowheader=_pipe_line_with_colons,
489+
lineabove=partial(_line_with_colons, begin="|", hline="|", sep="-", end="|"),
490+
linebelowheader=partial(
491+
_line_with_colons, begin="|", hline="|", sep="-", end="|"
492+
),
485493
linebetweenrows=None,
486494
linebelow=None,
487495
headerrow=DataRow("|", "|", "|"),

test/test_output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def test_github():
429429
expected = "\n".join(
430430
[
431431
"| strings | numbers |",
432-
"|-----------|-----------|",
432+
"| :-------- | --------: |",
433433
"| spam | 41.9999 |",
434434
"| eggs | 451 |",
435435
]

0 commit comments

Comments
 (0)