79
79
"padding" , "hiding" ])
80
80
81
81
82
+ def _pipe_segment_with_colons (align , colwidth ):
83
+ """Return a segment of a horizontal line with optional colons which
84
+ indicate column's alignment (as in `pipe` output format)."""
85
+ w = colwidth
86
+ if align in ["right" , "decimal" ]:
87
+ return ('-' * (w - 1 )) + ":"
88
+ elif align == "center" :
89
+ return ":" + ('-' * (w - 2 )) + ":"
90
+ elif align == "left" :
91
+ return ":" + ('-' * (w - 1 ))
92
+ else :
93
+ return '-' * w
94
+
95
+
96
+ def _pipe_line_with_colons (colwidths , colaligns ):
97
+ """Return a horizontal line with optional colons to indicate column's
98
+ alignment (as in `pipe` output format)."""
99
+ segments = [_pipe_segment_with_colons (a , w ) for a , w in zip (colaligns , colwidths )]
100
+ return u"|" + u"|" .join (segments ) + u"|"
101
+
102
+
82
103
_table_formats = {"simple" :
83
104
TableFormat (lineabove = Line ("" , "-" , " " , "" ),
84
105
linebelowheader = Line ("" , "-" , " " , "" ),
102
123
headerrow = DataRow ("|" , "|" , "|" ),
103
124
datarow = DataRow ("|" , "|" , "|" ),
104
125
padding = 1 , hiding = None ),
105
- "pipe" : # TODO: colons in linebelowheader
106
- TableFormat (lineabove = None ,
107
- linebelowheader = Line ( "|" , "-" , "|" , "|" ) ,
126
+ "pipe" :
127
+ TableFormat (lineabove = _pipe_line_with_colons ,
128
+ linebelowheader = _pipe_line_with_colons ,
108
129
linebetweenrows = None ,
109
130
linebelow = None ,
110
131
headerrow = DataRow ("|" , "|" , "|" ),
111
132
datarow = DataRow ("|" , "|" , "|" ),
112
- padding = 1 , hiding = None ),
133
+ padding = 1 ,
134
+ hiding = HidingRules (with_header_hide = ["lineabove" ])),
113
135
"orgtbl" :
114
136
TableFormat (lineabove = None ,
115
137
linebelowheader = Line ("|" , "-" , "+" , "|" ),
@@ -732,12 +754,13 @@ def _build_row(cells, padding, rowfmt):
732
754
return (begin + sep .join (padded_cells ) + end ).rstrip ()
733
755
734
756
735
- def _build_line (colwidths , padding , linefmt ):
757
+ def _build_line (colwidths , colaligns , padding , linefmt ):
736
758
"Return a string which represents a horizontal line."
737
759
if not linefmt :
738
760
return None
739
761
if isfunction (linefmt ):
740
- raise NotImplementedError ("line format as a function" )
762
+ padded_widths = [w + 2 * padding for w in colwidths ]
763
+ return linefmt (padded_widths , colaligns )
741
764
else :
742
765
begin , fill , sep , end = linefmt
743
766
cells = [fill * (w + 2 * padding ) for w in colwidths ]
@@ -754,21 +777,6 @@ def _mediawiki_cell_attrs(row, colaligns):
754
777
return row2
755
778
756
779
757
- def _line_segment_with_colons (linefmt , align , colwidth ):
758
- """Return a segment of a horizontal line with optional colons which
759
- indicate column's alignment (as in `pipe` output format)."""
760
- fill = linefmt .hline
761
- w = colwidth
762
- if align in ["right" , "decimal" ]:
763
- return (fill [0 ] * (w - 1 )) + ":"
764
- elif align == "center" :
765
- return ":" + (fill [0 ] * (w - 2 )) + ":"
766
- elif align == "left" :
767
- return ":" + (fill [0 ] * (w - 1 ))
768
- else :
769
- return fill [0 ] * w
770
-
771
-
772
780
def _format_table (fmt , headers , rows , colwidths , colaligns ):
773
781
"""Produce a plain-text representation of the table."""
774
782
lines = []
@@ -777,25 +785,25 @@ def _format_table(fmt, headers, rows, colwidths, colaligns):
777
785
headerrow = fmt .headerrow
778
786
779
787
if fmt .lineabove and "lineabove" not in hidden :
780
- lines .append (_build_line (colwidths , pad , fmt .lineabove ))
788
+ lines .append (_build_line (colwidths , colaligns , pad , fmt .lineabove ))
781
789
782
790
if headers :
783
791
lines .append (_build_row (headers , pad , headerrow ))
784
792
if fmt .linebelowheader and "linebelowheader" not in hidden :
785
- lines .append (_build_line (colwidths , pad , fmt .linebelowheader ))
793
+ lines .append (_build_line (colwidths , colaligns , pad , fmt .linebelowheader ))
786
794
787
795
if rows and fmt .linebetweenrows and "linebetweenrows" not in hidden :
788
796
# initial rows with a line below
789
797
for row in rows [:- 1 ]:
790
798
lines .append (_build_row (row , pad , fmt .datarow ))
791
- lines .append (_build_line (colwidths , pad , fmt .linebetweenrows ))
799
+ lines .append (_build_line (colwidths , colaligns , pad , fmt .linebetweenrows ))
792
800
# the last row without a line below
793
801
lines .append (_build_row (rows [- 1 ], pad , fmt .datarow ))
794
802
else :
795
803
for row in rows :
796
804
lines .append (_build_row (row , pad , fmt .datarow ))
797
805
798
806
if fmt .linebelow and "linebelow" not in hidden :
799
- lines .append (_build_line (colwidths , pad , fmt .linebelow ))
807
+ lines .append (_build_line (colwidths , colaligns , pad , fmt .linebelow ))
800
808
801
809
return "\n " .join (lines )
0 commit comments