blib/lib/Acme/AsciiArt2HtmlTable.pm

filter: Statementstmt 100% 135 / 135 Branchbran 95.8% 46 / 48 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 97.5% 198 / 203 CC 42 SCAR 31.2
CC
42
Cov
98%
aa2ht
33.0
_clone_hash
3.0
_count_in_the_beginning
3.0
CRAP 22.6
↑ Summary
1 package Acme::AsciiArt2HtmlTable;
2  
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 require Exporter;
7  
8 14 our @ISA = qw(Exporter);
9  
10 14 our %EXPORT_TAGS = (
11 'all' => [ qw(aa2ht) ],
12 );
13  
14 14 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
15  
16 14 our @EXPORT = qw(aa2ht);
17  
18 =head1 NAME
19  
20 Acme::AsciiArt2HtmlTable - Converts Ascii art to an HTML table
21  
22 =head1 VERSION
23  
24 Version 0.01
25  
26 =cut
27  
28 14 our $VERSION = '0.01';
29  
30 =head1 SYNOPSIS
31  
32 use Acme::AsciiArt2HtmlTable;
33  
34 my $table = "ggggggggrrrrrrrrrrrrrr\n" .
35 "ggggggggrrrrrrrrrrrrrr\n" .
36 "ggggggggrrrrrrrrrrrrrr\n" .
37 "ggggggggrrrrrrrrrrrrrr\n" .
38 "ggggggyyyyrrrrrrrrrrrr\n" .
39 "ggggggyyyyrrrrrrrrrrrr\n" .
40 "gggggyyyyyyrrrrrrrrrrr\n" .
41 "gggggyyyyyyrrrrrrrrrrr\n" .
42 "ggggggyyyyrrrrrrrrrrrr\n" .
43 "ggggggyyyyrrrrrrrrrrrr\n" .
44 "ggggggggrrrrrrrrrrrrrr\n" .
45 "ggggggggrrrrrrrrrrrrrr\n" .
46 "ggggggggrrrrrrrrrrrrrr\n" .
47 "ggggggggrrrrrrrrrrrrrr\n" ;
48  
49 my $html = aa2ht( { td => { width => 3 , height => 3 } } , $table);
50  
51 # $html now holds a table with a color representation of your
52 # ascii art. In this case, the Portuguese flag.
53  
54 =cut
55  
56 14 our %default_configuration;
57  
58 =head1 FUNCTIONS
59  
60 =head2 aa2ht
61  
62 Gets ascii text and converts it to an HTML table. This is how it works:
63  
64 =over 4
65  
66 =item * each line is a C<tr> element
67  
68 =item * each letter is a C<td> element
69  
70 =item * each C<td> has background of a specific color, which is
71 defined by the letter that created it
72  
73 =back
74  
75 =cut
76  
77 36 ▶sub aa2ht {
Subroutine aa2ht: called CC=33 SCAR=35.0
Pod documented
78  
79 # default configuration
80 36 my %config = _clone_hash( \%default_configuration );
81  
82 =head3 OPTIONS
83  
84 You can pass a reference to a hash before the text you want to
85 convert.
86  
87 =cut
88  
89 36 ▶ if ( ref($_[0]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 26 10 36
90 26 my $new_config = shift;
91  
92 =head4 id
93  
94 In order to save space in the output, C<td> and C<tr> elements'
95 attributes are not in each element, but rather in a C<style> element.
96  
97 This causes a problem if you want to put two different outputs with
98 different attributes on the same page.
99  
100 To solve this problem: C<id>.
101  
102 When creating a table, use the parameter C<id> to make sure it doesn't
103 end up mixed up with something else.
104  
105 my $html = aa2ht( { 'id' => 'special' } $ascii );
106  
107 The result will be something like this:
108  
109 <style>
110 .special td { width:1; height:1; }
111 .special tr { }
112 </style>
113 <table class="special" cellspacing="0" cellpadding="0" border="0">
114  
115 =cut
116  
117 26 ▶ if (defined $new_config->{'id'}) { $config{'id'} = $new_config->{'id'} }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_config->{"id"}) 1 25 26
118  
119 =head4 use-default-colors
120  
121 If set to a false value, no default mappings are used.
122  
123 my $html = aa2ht( { 'use-default-colors' => 0 }, $ascii);
124  
125 Behind the curtains, there is still a mapping: the default mapping to
126 white.
127  
128 =cut
129  
130 26 ▶ if ( defined $new_config->{'use-default-colors'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_config->{"use-default-colors"}) 2 24 26
131 2 ▶ if ( not $new_config->{'use-default-colors'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_config->{"use-default-colors"}) 2 0 2
132 2 $config{'colors'} = { 'default' => 'ffffff' } # everything is now white
133 }
134 }
135  
136 =head4 colors
137  
138 You can override color definitions or specify your own.
139  
140 my $html = aa2ht( { 'colors' => { '@' => 'ffddee',
141 'g' => '00ffff' } }, $ascii);
142  
143 =cut
144  
145 26 ▶ if ( ref($new_config->{'colors'}) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_config->{"colors"} eq "HASH") 10 16 26
146 10 for (keys %{$new_config->{'colors'}}) {
147 12 $config{'colors'}{$_} = $new_config->{'colors'}{$_};
148 }
149 }
150  
151 =head4 randomize-new-colors
152  
153 If set to a true value, letters with no mappings are assigned a
154 random one.
155  
156 my $html = aa2ht( { 'randomize-new-colors' => 1 }, $ascii);
157  
158 You might want to remove the default mappings if you're really
159 interested in a completely random effect:
160  
161 my $html = aa2ht( { 'use-default-colors' => 0,
162 'randomize-new-colors' => 1 }, $ascii);
163  
164 You might also want to keep the white space as a white block:
165  
166 my $html = aa2ht( { 'use-default-colors' => 0,
167 'colors' => { ' ' => 'ffffff'},
168 'randomize-new-colors' => 1 }, $ascii);
169  
170 =cut
171  
172 26 ▶ if ( defined $new_config->{'randomize-new-colors'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_config->{"randomize-new-colors"}) 1 25 26
173 1 $config{'randomize-new-colors'} = $new_config->{'randomize-new-colors'}
174 }
175  
176 =head4 table
177  
178 With the parameter C<table> you can specify specific values for fields
179 like C<border>, C<cellpadding> and C<cellspacing> (all these have
180 value "0" by default).
181  
182 my $html = aa2ht( { 'table' => { 'border' => '1' } }, $ascii );
183  
184 These attributes go directly into the C<table> tag.
185  
186 =head4 tr
187  
188 With the C<tr> parameter you can specify specific values for C<tr>'s
189 attributes.
190  
191 These attributes go into a C<style> tag. The table class uses that
192 style.
193  
194 =head4 td
195  
196 With the C<td> parameter you can specify specific values for C<td>'s
197 attributes, like C<width> or C<height>.
198  
199 my $html = aa2ht( { 'td' => { 'width' => '2px',
200 'height' => '2px' } }, $ascii);
201  
202 These attributes go into a C<style> tag. The table class uses that
203 style.
204  
205 =cut
206  
207 26 for my $elem ( qw/table tr td/ ) {
208 78 ▶ defined $new_config->{$elem} or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $new_config->{$elem} or next 13 65 78
209 13 ▶ ref ($new_config->{$elem}) eq 'HASH' or next;
Branch: 1 branch
BranchTrueFalseTotal
ref $new_config->{$elem} eq "HASH" or next 13 0 13
210  
211 13 for ( keys %{$new_config->{$elem}} ) {
212 12 $config{$elem}{$_} = $new_config->{$elem}{$_};
213 }
214 }
215  
216 26 ▶ if (defined $new_config->{'optimization'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_config->{"optimization"}) 1 25 26
217 1 $config{'optimization'} = $new_config->{'optimization'};
218 }
219  
220 }
221  
222 ##############
223  
224 # prepare the table, tr and td attributes
225 36 my $table = join ' ', map { "$_=\"$config{'table'}{$_}\"" } sort keys %{$config{'table'}};
226  
227 36 my $tr = join ' ', map { "$_:$config{'tr'}{$_};" } sort keys %{$config{'tr' }};
228 36 my $td = join ' ', map { "$_:$config{'td'}{$_};" } sort keys %{$config{'td' }};
229  
230 # our ascii text
231 36 my $text = shift;
232  
233 # where we'll store our html
234 36 my $html = '';
235  
236 # style (td and tr elements' attributes)
237 36 $html .= "<style>\n" .
238 ".$config{'id'} td { $td }\n.$config{'id'} tr { $tr }" .
239 "\n</style>\n";
240  
241 # table header
242 36 $html .= "<table class=\"$config{'id'}\" $table>\n";
243  
244 # prepare the cells
245 36 my @lines = map { [ split //, $_ ] } split /\n/, $text;
246  
247 # just to make sure an optimized table has the same width as the normal one
248 36 my $opt_fix = '';
249 36 ▶ if ( $config{'optimization'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"optimization"}) 1 35 36
250 1 my $width = 0;
251 1 for my $l ( 0 .. $#lines ) {
252 3 ▶ if ( $width < $#{$lines[$l]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width < $#{$lines[$l];}) 1 2 3
253 1 $width = $#{$lines[$l]};
254 }
255 }
256 1 $opt_fix = '<tr>' . ( '<td></td>' x $width ) . '</tr>';
257 }
258  
259 36 for my $line ( 0 .. $#lines ) {
260 61 for my $cell ( 0 .. $#{$lines[$line]} ) {
261 1393 ▶ next if $lines[$line]->[$cell] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$line][$cell] eq "" 4 1389 1393
262  
263 # randomizing new colors
264 1389 ▶ if ( $config{'randomize-new-colors'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"randomize-new-colors"}) 4 1385 1389
265 4 ▶ if ( not defined $config{'colors'}{ $lines[$line]->[$cell] } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config{"colors"}{$lines[$line][$cell]}) 1 3 4
266 1 $config{'colors'}{ $lines[$line]->[$cell] } = _random_color();
267 }
268 }
269  
270 # optimization
271 1389 my $optimization = '';
272  
273 # debugging messages were kept for future reference
274  
275 # remember that lines and cells are not the exact values, as
276 # arrays start at index 0 and both lines and cells start at
277 # position 1
278  
279 #my $debug = "line $line, cell $cell, ";
280  
281 1389 ▶ if ( $config{'optimization'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"optimization"}) 5 1384 1389
282  
283 #$debug .= "\nthis is line $line, cell $cell";
284 # check how many cells we could have on each line from the line we're
285 # in to the last one
286 5 my %we_could_have;
287 5 for ( $line .. $#lines ) {
288 $we_could_have{$_} = _count_in_the_beginning(
289 $lines[$line]->[$cell],
290 11 @{$lines[$_]}[$cell .. $#{$lines[$_]}]
291 );
292 #$debug .= "\nwe could have $we_could_have{$_} on line $_";
293 }
294  
295 # check, for each line, how many cells an area up to that line would have
296 5 my %area;
297 my %area_width;
298 5 for ( $line .. $#lines ) {
299 11 my $min = _min( @we_could_have{$line .. $_} );
300 11 $area{$_} = (1 + $_ - $line) * $min;
301 11 $area_width{$_} = $min;
302 #$debug .="\nwe could make an area of $area{$_} up to line $_, with a maximum of $area_width{$_} cells per line";
303 }
304  
305 # check which is the line that maximizes optimization
306 5 my $max_area = _max(values %area);
307 5 my $best_line = _max(grep { $area{$_} == $max_area } keys %area);
308 #$debug .= "\nour best choice seem to be using line $best_line";
309  
310 # check the are width
311 5 my $width = $cell + $area_width{$best_line} - 1;
312  
313 # clean everything in the area we're about to optimize
314 #$debug .= "\nwe want to clean everything from lines $line to $best_line and cells $cell to $width";
315 5 for my $l ( $line .. $best_line ) {
316 8 for my $c ( $cell .. $width ) {
317 9 ▶ next if ( $l == $line and $c == $cell );
Branch: 1 branch
BranchTrueFalseTotal
if $l == $line and $c == $cell 5 4 9
Condition100%3/3
$l == $line and $c == $cell
!ll&&!rl&&r
315
MC/DC100%2/2
$l == $line and $c == $cell
$l == $line $c == $cell
Truth table100%3/3
A and B
A= $l == $lineB= $c == $cell
ABresult
0X0
100
111
318 4 $lines[$l]->[$c] = '';
319 }
320 }
321  
322 # optimize
323 5 my $rowspan = $best_line - $line + 1;
324 5 my $colspan = $area_width{$best_line};
325  
326 5 ▶ if ( $rowspan > 1 ) { $optimization .= " rowspan=\"$rowspan\"" }
Branch: 1 branch
BranchTrueFalseTotal
if ($rowspan > 1) 2 3 5
327 5 ▶ if ( $colspan > 1 ) { $optimization .= " colspan=\"$colspan\"" }
Branch: 1 branch
BranchTrueFalseTotal
if ($colspan > 1) 1 4 5
328  
329 #$debug .= "\n";
330 }
331  
332 $lines[$line]->[$cell] = "<td$optimization bgcolor=\"" .
333 ( $config{'colors'}{ $lines[$line]->[$cell] } ||
334 1389 ▶ $config{'colors'}{'default'} ) .
Condition66%2/3
$config{'colors'}{$lines[$line][$cell]} || $config{'colors'}{'default'}
l!l&&r!l&&!r
1376130
MC/DC0%0/2
$config{'colors'}{$lines[$line][$cell]} || $config{'colors'}{'default'}
$config{'colors'}{$lines[$line][$cell]} $config{'colors'}{'default'}
Truth table66%2/3
A || B
A= $config{'colors'}{$lines[$line][$cell]}B= $config{'colors'}{'default'}
ABresult
1X1
011
000
335 "\"></td>";
336  
337 }
338  
339 61 $lines[$line] = join "\n", grep /./, @{$lines[$line]};
340  
341 61 ▶ if ($config{'optimization'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"optimization"}) 3 58 61
342 # this is so empty rows aren't ignored by the browser
343 3 $lines[$line] .= "\n<td></td>";
344 }
345  
346 }
347  
348 # the table
349 36 $html .= join "\n", map { "<tr>\n$_\n</tr>" } @lines;
350  
351 36 ▶ if ($config{'optimization'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"optimization"}) 1 35 36
352 # this is so empty columns aren't ignored by the browser
353 1 $html .= "$opt_fix";
354 }
355  
356 # table footer
357 36 $html .= "\n</table>\n";
358  
359 # return the table
360 36 return $html;
361 }
362  
363 =head3 SPECIALS
364  
365 =head4 optimization
366  
367 Table optimization, which is disabled by default, uses the C<rowspan>
368 and C<colspan> C<td> attributes to save up space.
369  
370 my $html = aa2ht( { 'optimization' => 1 }, $ascii );
371  
372 When the optimization algorithm sees a chance of turning some cells
373 into a big one, it does so. It always chooses the biggest area
374 possible for optimizing.
375  
376 If two different areas suitable for optimization starting from a given
377 cell are available and both present the same area size, the algorithm
378 picks the one that maximizes width.
379  
380 =head4 default color
381  
382 By default, an unmapped character is mapped to the default color,
383 which is black.
384  
385 You can override this color by assigning a different mapping to
386 "default" with the C<colors> option.
387  
388 my $html = aa2ht( { 'colors' => { 'default' => 'ffffff' } }, $ascii);
389  
390 This, for instance, makes the default color be white, thus making only
391 the recognized characters show up colored on the table.
392  
393 =head1 MAPPINGS ( LETTER -> COLOR )
394  
395 The following letters are mapped to colors in the following way:
396  
397 l 000000 # black
398 b 0000ff # blue
399 o a52a2a # brown
400 g 00ff00 # green
401 a bebebe # gray
402 e bebebe # grey
403 m ff00ff # magenta
404 o ffa500 # orange
405 p ffc0cb # pink
406 u a020f0 # purple
407 r ff0000 # red
408 w ffffff # white
409 y ffff00 # yellow
410  
411 L 000000 # light black
412 B add8e6 # lighe blue
413 O a52a2a # light brown
414 G 90ee90 # light green
415 A d3d3d3 # light gray
416 E d3d3d3 # light grey
417 M ff00ff # light magenta
418 O ffa500 # light orange
419 P ffb6c1 # light pink
420 U 9370db # light purple
421 R cd5c5c # light red
422 W ffffff # light white
423 Y ffffe0 # light yellow
424  
425 Spaces are mapped to white:
426  
427 ffffff # white
428  
429 By default, everything else is mapped to black
430  
431 default 000000 # black
432  
433 =cut
434  
435 14 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
436  
437 # default configuration
438 14 %default_configuration = (
439 id => 'default',
440 table => {
441 'border' => 0,
442 'cellpadding' => 0,
443 'cellspacing' => 0,
444 },
445 tr => {
446 },
447 td => {
448 'width' => '1px',
449 'height' => '1px',
450 },
451 colors=> {
452 ' ' => 'ffffff', # white
453  
454 'l' => '000000', # black
455 'b' => '0000ff', # blue
456 'o' => 'a52a2a', # brown
457 'g' => '00ff00', # green
458 'a' => 'bebebe', # gray
459 'e' => 'bebebe', # grey
460 'm' => 'ff00ff', # magenta
461 'o' => 'ffa500', # orange
462 'p' => 'ffc0cb', # pink
463 'u' => 'a020f0', # purple
464 'r' => 'ff0000', # red
465 'w' => 'ffffff', # white
466 'y' => 'ffff00', # yellow
467  
468 'L' => '000000', # light black
469 'B' => 'add8e6', # light blue
470 'O' => 'a52a2a', # light brown
471 'G' => '90ee90', # light green
472 'A' => 'd3d3d3', # light gray
473 'E' => 'd3d3d3', # light grey
474 'M' => 'ff00ff', # light magenta
475 'O' => 'ffa500', # light orange
476 'P' => 'ffb6c1', # light pink
477 'U' => '9370db', # light purple
478 'R' => 'cd5c5c', # light red
479 'W' => 'ffffff', # light white
480 'Y' => 'ffffe0', # light yellow
481  
482 default => '000000', # black
483 },
484 'randomize-new-colors' => 0,
485 'optimization' => 0,
486 );
487  
488 }
489  
490 # subroutines
491  
492 1 ▶sub _random_color {
Subroutine _random_color: called CC=2 SCAR=6.9
493 1 my $color = '';
494  
495 1 for (1 .. 6) {
496 6 $color .= qw/1 2 3 4 5 6 7 8 9 0 a b c d e f/[int rand 16];
497 }
498  
499 1 return $color;
500 }
501  
502 180 ▶sub _clone_hash {
Subroutine _clone_hash: called CC=3 SCAR=11.0
503 180 my %hash = %{+shift};
504  
505 180 my %new_hash;
506  
507 180 for (keys %hash) {
508 1368 ▶ if (ref($hash{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash{$_}) { } 144 1224 1368
509 144 $new_hash{$_} = { _clone_hash ( $hash{$_} ) };
510 }
511 else {
512 1224 $new_hash{$_} = $hash{$_};
513 }
514 }
515  
516 180 return %new_hash;
517 }
518  
519 11 ▶sub _count_in_the_beginning {
Subroutine _count_in_the_beginning: called CC=3 SCAR=11.0
520 11 my ($cell, @elems) = @_;
521 11 my $t = 0;
522 11 for (@elems) {
523 20 ▶ if ($cell eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell eq $_) { } 15 5 20
524 15 $t++;
525 }
526 else {
527 5 last;
528 }
529 }
530 11 return $t;
531 }
532  
533 11 ▶sub _min {
Subroutine _min: called CC=3 SCAR=11.0
534 11 my $min = shift;
535  
536 11 for (@_) {
537 8 ▶ if ( $min > $_ ) { $min = $_ }
Branch: 1 branch
BranchTrueFalseTotal
if ($min > $_) 5 3 8
538 }
539  
540 11 return $min;
541 }
542  
543 10 ▶sub _max {
Subroutine _max: called CC=3 SCAR=11.0
544 10 my $max = shift;
545  
546 10 for (@_) {
547 7 ▶ if ( $max < $_ ) { $max = $_ }
Branch: 1 branch
BranchTrueFalseTotal
if ($max < $_) 2 5 7
548 }
549  
550 10 return $max;
551 }
552  
553 =head1 SEE ALSO
554  
555 The examples/ directory.
556  
557 =head1 AUTHOR
558  
559 Jose Castro, C<< <cog@cpan.org> >>
560  
561 =head1 CAVEATS
562  
563 If you specify the C<rowspan> or C<colspan> for C<td> elements and you
564 also ask for optimization... I don't even want to imagine what will
565 happen...
566  
567 =head1 BUGS
568  
569 Please report any bugs or feature requests to
570 C<bug-acme-tablethis@rt.cpan.org>, or through the web interface at
571 L<http://rt.cpan.org>. I will be notified, and then you'll automatically
572 be notified of progress on your bug as I make changes.
573  
574 =head1 COPYRIGHT & LICENSE
575  
576 Copyright 2005 Jose Castro, All Rights Reserved.
577  
578 This program is free software; you can redistribute it and/or modify it
579 under the same terms as Perl itself.
580  
581 =cut
582  
583 14 1; # End of Acme::AsciiArt2HtmlTable
↑ Summary