...
119
|
|
|
|
|
|
|
# we grab all it's | inner text (and/or parsed html), rearrange it into a |
120
|
|
|
|
|
|
|
# single string of formatted text, and put a token into it's first | |
121
|
|
|
|
|
|
|
# once we have processed the html with _parse(), we replace the tokens with the |
122
|
|
|
|
|
|
|
# corresponding formatted text |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my @tables = $tree->look_down(_tag=>'table'); |
125
|
|
|
|
|
|
|
my $table_count = 0; |
126
|
|
|
|
|
|
|
for my $table (@tables) { |
127
|
|
|
|
|
|
|
$formatted_tables->[$table_count] = []; |
128
|
|
|
|
|
|
|
my @trs = $table->look_down(_tag=>'tr'); |
129
|
|
|
|
|
|
|
my @max_col_width; # max column widths by index |
130
|
|
|
|
|
|
|
my @max_col_heights; # max column heights (for multi-line text) by index |
131
|
|
|
|
|
|
|
my @col_lines; # a stack for our redesigned rows of column ( | ) text |
132
|
|
|
|
|
|
|
FIRST_PASS: { |
133
|
|
|
|
|
|
|
my $row_count = 0; # obviously a counter... |
134
|
|
|
|
|
|
|
for my $tr (@trs) { # *** 1st pass over rows |
135
|
|
|
|
|
|
|
$max_col_heights[$row_count] = 0; |
136
|
|
|
|
|
|
|
$col_lines[$row_count] = []; |
137
|
|
|
|
|
|
|
my @cols = $tr->look_down(_tag=>'td'); # no support for | . sorry.
|
138
|
|
|
|
|
|
|
for (my $i = 0; $i < scalar @cols; $i++) { |
139
|
|
|
|
|
|
|
my $td = $cols[$i]->clone; |
140
|
|
|
|
|
|
|
my $new_tree = HTML::TreeBuilder->new; |
141
|
|
|
|
|
|
|
$new_tree->{_content} = [ $td ]; |
142
|
|
|
|
|
|
|
# parse the contents of the td into text |
143
|
|
|
|
|
|
|
# this doesn't work well with nested tables... |
144
|
|
|
|
|
|
|
my $text = __PACKAGE__->new->_parse($new_tree); |
145
|
|
|
|
|
|
|
# we don't want leading or tailing whitespace |
146
|
|
|
|
|
|
|
$text =~ s/^\s+//s; |
147
|
|
|
|
|
|
|
$text =~ s/\s+\z//s; |
148
|
|
|
|
|
|
|
# now we figure out the maximum widths and heights needed for each column |
149
|
|
|
|
|
|
|
my $max_line_width = 0; |
150
|
|
|
|
|
|
|
my @lines = split "\n", $text; # take the parsed text and break it into virtual rows |
151
|
|
|
|
|
|
|
$max_col_heights[$row_count] = scalar @lines if scalar @lines > $max_col_heights[$row_count]; |
152
|
|
|
|
|
|
|
for my $line (@lines) { |
153
|
|
|
|
|
|
|
my $line_width = length $line; |
154
|
|
|
|
|
|
|
$max_line_width = $line_width if $line_width > $max_line_width; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
$cols[$i]->{_content} = [ $text ]; |
157
|
|
|
|
|
|
|
$max_col_width[$i] ||= 0; |
158
|
|
|
|
|
|
|
$max_col_width[$i] = $max_line_width if $max_line_width > $max_col_width[$i]; |
159
|
|
|
|
|
|
|
# now put the accumulated lines onto our stack |
160
|
|
|
|
|
|
|
$col_lines[$row_count]->[$i] = \@lines; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
$tr->{_content} = \@cols; |
163
|
|
|
|
|
|
|
$row_count++; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
SECOND_PASS: { |
168
|
|
|
|
|
|
|
my $row_count = 0; # obviously, another counter... |
169
|
|
|
|
|
|
|
for my $tr (@trs) { # *** 2nd pass over rows |
170
|
|
|
|
|
|
|
my @cols = $tr->look_down(_tag=>'td'); # no support for | . sorry.
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
my $row_text; # the final string representing each row of reformatted text |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
my @col_rows; # a stack for each virtual $new_line spliced together from a group of | 's |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# iterate over each column of the maximum rows of parsed multiline text per | |
177
|
|
|
|
|
|
|
# for each virtual row of each virtual column, concat the text with alignment spacings |
178
|
|
|
|
|
|
|
# the final concatinated string value will be placed in column 0 |
179
|
|
|
|
|
|
|
for (my $j = 0; $j < $max_col_heights[$row_count]; $j++) { |
180
|
|
|
|
|
|
|
my $new_line; |
181
|
|
|
|
|
|
|
for (my $i = 0; $i < scalar @cols; $i++) { # here are the actual | elements we're iterating over... |
182
|
|
|
|
|
|
|
my $width = $max_col_width[$i] + $cellpadding; # how wide is this column of text |
183
|
|
|
|
|
|
|
my $line = $col_lines[$row_count]->[$i]->[$j]; # get the text to fit into it |
184
|
|
|
|
|
|
|
$line = defined $line ? $line : ''; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# strip the whitespace from beginning and end of each line |
187
|
|
|
|
|
|
|
$line =~ s/^\s+//gs; |
188
|
|
|
|
|
|
|
$line =~ s/\s+\z//gs; |
189
|
|
|
|
|
|
|
my $n_space = $width - length $line; # the difference between the column and text widths |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# we are creating virtual rows of text within a single | |
192
|
|
|
|
|
|
|
# so we need to add an indent to all but the first row to |
193
|
|
|
|
|
|
|
# match the indent added by _parse() for presenting table contents |
194
|
|
|
|
|
|
|
$line = ((' ')x$parser_indent). $line if $j != 0 and $i == 0; |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
# here we adjust the text alignment by wrapping the text in occulted whitespace |
197
|
|
|
|
|
|
|
my $justify = $cols[$i]->tag eq 'td' ? ( $cols[$i]->attr('align') || 'left' ) : 'center'; |
198
|
|
|
|
|
|
|
if ($justify eq 'center') { |
199
|
|
|
|
|
|
|
my $pre = int( ($n_space + $cellpadding) / 2 ); # divide remaining space in half |
200
|
|
|
|
|
|
|
my $post = $n_space - $pre; # assign any uneven remainder to the end |
201
|
|
|
|
|
|
|
$new_line .= ((' ')x$pre). $line .((' ')x$post); # wrap the text in spaces |
202
|
|
|
|
|
|
|
} elsif ($justify eq 'left') { |
203
|
|
|
|
|
|
|
$new_line .= ((' ')x$cellpadding). $line .((' ')x$n_space); |
204
|
|
|
|
|
|
|
} else { |
205
|
|
|
|
|
|
|
$new_line .= ((' ')x$n_space). $line .((' ')x$cellpadding); |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
$new_line .= "\n" if $j != $max_col_heights[$row_count] - 1; # add a newline to all but the last text row |
209
|
|
|
|
|
|
|
$col_rows[$j] = $new_line; # put the line into the stack for this row |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
$row_text .= $_ for @col_rows; |
212
|
|
|
|
|
|
|
for (my $i = 1; $i < scalar @cols; $i++) { |
213
|
|
|
|
|
|
|
$cols[$i]->delete; # get rid of unneeded | 's |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
# put the fully formatted text into our accumulator |
216
|
|
|
|
|
|
|
$formatted_tables->[$table_count]->[$row_count] = $row_text; |
217
|
|
|
|
|
|
|
$cols[0]->content->[0] = "__TOKEN__${table_count}__${row_count}__"; # place a token into the row at col 0 |
218
|
|
|
|
|
|
|
$row_count++; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
$table_count++; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
# now replace our tokens |
225
|
|
|
|
|
|
|
my $text = $self->_parse( $tree ); |
226
|
|
|
|
|
|
|
for (my $i = 0; $i < scalar @$formatted_tables; $i++) { |
227
|
|
|
|
|
|
|
for (my $j = 0; $j < scalar @{ $$formatted_tables[$i] }; $j++) { |
228
|
|
|
|
|
|
|
my $token = "__TOKEN__${i}__${j}__"; |
229
|
|
|
|
|
|
|
$token .= "\n?" if $no_rowspacing; |
230
|
|
|
|
|
|
|
my $new_text = $$formatted_tables[$i][$j]; |
231
|
|
|
|
|
|
|
$text =~ s/$token/$new_text/; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
return $text; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
1; |
239
|
|
|
|
|
|
|
__END__ |