Skip to content

Commit 704a392

Browse files
committed
change TableFormat structure (some formats are broken: pipe, mediawiki, latex)
1 parent a20d8dc commit 704a392

File tree

1 file changed

+70
-63
lines changed

1 file changed

+70
-63
lines changed

tabulate.py

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,53 @@
3636
DataRow = namedtuple("DataRow", ["begin", "sep", "end"])
3737

3838

39+
# with_header_hide (a list of strings) is a list of elements to be not
40+
# displayed if a table has column headers.
41+
#
42+
# without_header_hide (a list of strings) is a list of elements to be
43+
# not displayed if a table doesn't have column headers.
44+
#
45+
HeaderRules = namedtuple("HeaderRules", ["with_header_hide", "without_header_hide"])
46+
47+
# A table structure is suppposed to be:
48+
#
49+
# --- lineabove ---------
50+
# headerrow
51+
# --- linebelowheader ---
52+
# datarow
53+
# --- linebewteenrows ---
54+
# ... (more datarows) ...
55+
# --- linebewteenrows ---
56+
# last datarow
57+
# --- linebelow ---------
58+
#
59+
# TableFormat's line* elements can be
60+
#
61+
# - either None, if the element is not used,
62+
# - or a Line tuple,
63+
# - or a function: [col_widths], [col_alignments] -> string.
64+
#
65+
# TableFormat's *row elements can be
66+
#
67+
# - either None, if the element is not used,
68+
# - or a DataRow tuple,
69+
# - or a function: [cell_values], [col_widths], [col_alignments] -> string.
70+
#
71+
# padding (an integer) is the amount of white space around data values.
72+
#
73+
# hiding can be
74+
#
75+
# - either None, to display all table elements unconditionally,
76+
# - or a HeaderRules tuple.
77+
#
3978
TableFormat = namedtuple("TableFormat", ["lineabove", "linebelowheader",
4079
"linebetweenrows", "linebelow",
4180
"headerrow", "datarow",
42-
"padding", "usecolons", "usehtmlattrs",
43-
"with_header_hide",
44-
"without_header_hide"])
81+
"padding", "hiding"])
4582

4683

47-
if python_version_tuple() >= ('2', '6', '5'):
48-
_format_defaults = {"padding": 0,
49-
"usecolons": False,
50-
"usehtmlattrs": False,
51-
"with_header_hide": [],
52-
"without_header_hide": []}
53-
else:
54-
# workaround for Python 2.6.4 and earlier
55-
_format_defaults = {b"padding": 0,
56-
b"usecolons": False,
57-
b"usehtmlattrs": False,
58-
b"with_header_hide": [],
59-
b"without_header_hide": []}
84+
_headerless_without_linebelowheader = HeaderRules(with_header_hide=[],
85+
without_header_hide=["linebelowheader"])
6086

6187

6288
_table_formats = {"simple":
@@ -67,14 +93,15 @@
6793
headerrow=DataRow("", " ", ""),
6894
datarow=DataRow("", " ", ""),
6995
padding=0,
70-
usecolons=False,
71-
usehtmlattrs=False,
72-
with_header_hide=["linebelow"],
73-
without_header_hide=[]),
96+
hiding=HeaderRules(with_header_hide=["linebelow"],
97+
without_header_hide=[])),
7498
"plain":
75-
TableFormat(None, None, None, None,
76-
DataRow("", " ", ""), DataRow("", " ", ""),
77-
**_format_defaults),
99+
TableFormat(lineabove=None, linebelowheader=None,
100+
linebetweenrows=None, linebelow=None,
101+
headerrow=DataRow("", " ", ""),
102+
datarow=DataRow("", " ", ""),
103+
padding=0,
104+
hiding=None),
78105
"grid":
79106
TableFormat(lineabove=Line("+", "-", "+", "+"),
80107
linebelowheader=Line("+", "=", "+", "+"),
@@ -83,22 +110,16 @@
83110
headerrow=DataRow("|", "|", "|"),
84111
datarow=DataRow("|", "|", "|"),
85112
padding=1,
86-
usecolons=False,
87-
usehtmlattrs=False,
88-
with_header_hide=[],
89-
without_header_hide=["linebelowheader"]),
90-
"pipe":
113+
hiding=_headerless_without_linebelowheader),
114+
"pipe": # TODO: colons in linebelowheader
91115
TableFormat(lineabove=None,
92116
linebelowheader=Line("|", "-", "|", "|"),
93117
linebetweenrows=None,
94118
linebelow=None,
95119
headerrow=DataRow("|", "|", "|"),
96120
datarow=DataRow("|", "|", "|"),
97121
padding=1,
98-
usecolons=True,
99-
usehtmlattrs=False,
100-
with_header_hide=[],
101-
without_header_hide=[]),
122+
hiding=None),
102123
"orgtbl":
103124
TableFormat(lineabove=None,
104125
linebelowheader=Line("|", "-", "+", "|"),
@@ -107,10 +128,7 @@
107128
headerrow=DataRow("|", "|", "|"),
108129
datarow=DataRow("|", "|", "|"),
109130
padding=1,
110-
usecolons=False,
111-
usehtmlattrs=False,
112-
with_header_hide=[],
113-
without_header_hide=["linebelowheader"]),
131+
hiding=_headerless_without_linebelowheader),
114132
"rst":
115133
TableFormat(lineabove=Line("", "=", " ", ""),
116134
linebelowheader=Line("", "=", " ", ""),
@@ -119,11 +137,8 @@
119137
headerrow=DataRow("", " ", ""),
120138
datarow=DataRow("", " ", ""),
121139
padding=0,
122-
usecolons=False,
123-
usehtmlattrs=False,
124-
with_header_hide=[],
125-
without_header_hide=["linebelowheader"]),
126-
"mediawiki":
140+
hiding=_headerless_without_linebelowheader),
141+
"mediawiki": # TODO: row alignment
127142
TableFormat(lineabove=Line("{| class=\"wikitable\" style=\"text-align: left;\"",
128143
"", "", "\n|+ <!-- caption -->\n|-"),
129144
linebelowheader=Line("|-", "", "", ""),
@@ -132,22 +147,16 @@
132147
headerrow=DataRow("!", "!!", ""),
133148
datarow=DataRow("|", "||", ""),
134149
padding=1,
135-
usecolons=False,
136-
usehtmlattrs=True,
137-
with_header_hide=[],
138-
without_header_hide=["linebelowheader"]),
139-
"latex":
150+
hiding=_headerless_without_linebelowheader),
151+
"latex": # TODO: row alignment
140152
TableFormat(lineabove=Line("\\begin{tabular}{r", "", "r", "}\n\hline"),
141153
linebelowheader=Line("\\hline", "", "", ""),
142154
linebetweenrows=None,
143155
linebelow=Line("\\hline\n\\end{tabular}", "", "", ""),
144156
headerrow=DataRow("", "&", "\\\\"),
145157
datarow=DataRow("", "&", "\\\\"),
146158
padding=1,
147-
usecolons=False,
148-
usehtmlattrs=False,
149-
with_header_hide=[],
150-
without_header_hide=["linebelowheader"])}
159+
hiding=_headerless_without_linebelowheader)}
151160

152161

153162
tabulate_formats = list(sorted(_table_formats.keys()))
@@ -165,7 +174,8 @@ def simple_separated_format(separator):
165174
166175
"""
167176
return TableFormat(None, None, None, None,
168-
headerrow=None, datarow=DataRow('', separator, ''), **_format_defaults)
177+
headerrow=None, datarow=DataRow('', separator, ''),
178+
padding=0, hiding=None)
169179

170180

171181
def _isconvertible(conv, string):
@@ -764,13 +774,15 @@ def _line_segment_with_colons(linefmt, align, colwidth):
764774
def _format_table(fmt, headers, rows, colwidths, colaligns):
765775
"""Produce a plain-text representation of the table."""
766776
lines = []
767-
hidden = fmt.with_header_hide if headers else fmt.without_header_hide
777+
if fmt.hiding:
778+
if headers:
779+
hidden = fmt.hiding.with_header_hide
780+
else:
781+
hidden = fmt.hiding.without_header_hide
782+
else:
783+
hidden = []
768784
pad = fmt.padding
769-
headerrow = fmt.headerrow if fmt.headerrow else fmt.datarow
770-
771-
if fmt.usehtmlattrs:
772-
headers = _mediawiki_cell_attrs(headers, colaligns)
773-
rows = [_mediawiki_cell_attrs(row, colaligns) for row in rows]
785+
headerrow = fmt.headerrow
774786

775787
if fmt.lineabove and "lineabove" not in hidden:
776788
lines.append(_build_line(colwidths, pad, *fmt.lineabove))
@@ -780,12 +792,7 @@ def _format_table(fmt, headers, rows, colwidths, colaligns):
780792

781793
if fmt.linebelowheader and "linebelowheader" not in hidden:
782794
begin, fill, sep, end = fmt.linebelowheader
783-
if fmt.usecolons:
784-
segs = [_line_segment_with_colons(fmt.linebelowheader, a, w + 2*pad)
785-
for w,a in zip(colwidths, colaligns)]
786-
lines.append(_build_row(segs, 0, begin, sep, end))
787-
else:
788-
lines.append(_build_line(colwidths, pad, *fmt.linebelowheader))
795+
lines.append(_build_line(colwidths, pad, *fmt.linebelowheader))
789796

790797
if rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
791798
# initial rows with a line below

0 commit comments

Comments
 (0)