36
36
DataRow = namedtuple ("DataRow" , ["begin" , "sep" , "end" ])
37
37
38
38
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
+ #
39
78
TableFormat = namedtuple ("TableFormat" , ["lineabove" , "linebelowheader" ,
40
79
"linebetweenrows" , "linebelow" ,
41
80
"headerrow" , "datarow" ,
42
- "padding" , "usecolons" , "usehtmlattrs" ,
43
- "with_header_hide" ,
44
- "without_header_hide" ])
81
+ "padding" , "hiding" ])
45
82
46
83
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" ])
60
86
61
87
62
88
_table_formats = {"simple" :
67
93
headerrow = DataRow ("" , " " , "" ),
68
94
datarow = DataRow ("" , " " , "" ),
69
95
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 = [])),
74
98
"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 ),
78
105
"grid" :
79
106
TableFormat (lineabove = Line ("+" , "-" , "+" , "+" ),
80
107
linebelowheader = Line ("+" , "=" , "+" , "+" ),
83
110
headerrow = DataRow ("|" , "|" , "|" ),
84
111
datarow = DataRow ("|" , "|" , "|" ),
85
112
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
91
115
TableFormat (lineabove = None ,
92
116
linebelowheader = Line ("|" , "-" , "|" , "|" ),
93
117
linebetweenrows = None ,
94
118
linebelow = None ,
95
119
headerrow = DataRow ("|" , "|" , "|" ),
96
120
datarow = DataRow ("|" , "|" , "|" ),
97
121
padding = 1 ,
98
- usecolons = True ,
99
- usehtmlattrs = False ,
100
- with_header_hide = [],
101
- without_header_hide = []),
122
+ hiding = None ),
102
123
"orgtbl" :
103
124
TableFormat (lineabove = None ,
104
125
linebelowheader = Line ("|" , "-" , "+" , "|" ),
107
128
headerrow = DataRow ("|" , "|" , "|" ),
108
129
datarow = DataRow ("|" , "|" , "|" ),
109
130
padding = 1 ,
110
- usecolons = False ,
111
- usehtmlattrs = False ,
112
- with_header_hide = [],
113
- without_header_hide = ["linebelowheader" ]),
131
+ hiding = _headerless_without_linebelowheader ),
114
132
"rst" :
115
133
TableFormat (lineabove = Line ("" , "=" , " " , "" ),
116
134
linebelowheader = Line ("" , "=" , " " , "" ),
119
137
headerrow = DataRow ("" , " " , "" ),
120
138
datarow = DataRow ("" , " " , "" ),
121
139
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
127
142
TableFormat (lineabove = Line ("{| class=\" wikitable\" style=\" text-align: left;\" " ,
128
143
"" , "" , "\n |+ <!-- caption -->\n |-" ),
129
144
linebelowheader = Line ("|-" , "" , "" , "" ),
132
147
headerrow = DataRow ("!" , "!!" , "" ),
133
148
datarow = DataRow ("|" , "||" , "" ),
134
149
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
140
152
TableFormat (lineabove = Line ("\\ begin{tabular}{r" , "" , "r" , "}\n \hline" ),
141
153
linebelowheader = Line ("\\ hline" , "" , "" , "" ),
142
154
linebetweenrows = None ,
143
155
linebelow = Line ("\\ hline\n \\ end{tabular}" , "" , "" , "" ),
144
156
headerrow = DataRow ("" , "&" , "\\ \\ " ),
145
157
datarow = DataRow ("" , "&" , "\\ \\ " ),
146
158
padding = 1 ,
147
- usecolons = False ,
148
- usehtmlattrs = False ,
149
- with_header_hide = [],
150
- without_header_hide = ["linebelowheader" ])}
159
+ hiding = _headerless_without_linebelowheader )}
151
160
152
161
153
162
tabulate_formats = list (sorted (_table_formats .keys ()))
@@ -165,7 +174,8 @@ def simple_separated_format(separator):
165
174
166
175
"""
167
176
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 )
169
179
170
180
171
181
def _isconvertible (conv , string ):
@@ -764,13 +774,15 @@ def _line_segment_with_colons(linefmt, align, colwidth):
764
774
def _format_table (fmt , headers , rows , colwidths , colaligns ):
765
775
"""Produce a plain-text representation of the table."""
766
776
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 = []
768
784
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
774
786
775
787
if fmt .lineabove and "lineabove" not in hidden :
776
788
lines .append (_build_line (colwidths , pad , * fmt .lineabove ))
@@ -780,12 +792,7 @@ def _format_table(fmt, headers, rows, colwidths, colaligns):
780
792
781
793
if fmt .linebelowheader and "linebelowheader" not in hidden :
782
794
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 ))
789
796
790
797
if rows and fmt .linebetweenrows and "linebetweenrows" not in hidden :
791
798
# initial rows with a line below
0 commit comments