Acme-AsciiArt2HtmlTable

 view release on metacpan or  search on metacpan

lib/Acme/AsciiArt2HtmlTable.pm  view on Meta::CPAN


=head1 NAME

Acme::AsciiArt2HtmlTable - Converts Ascii art to an HTML table

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

    use Acme::AsciiArt2HtmlTable;

    my $table = "ggggggggrrrrrrrrrrrrrr\n" .
                "ggggggggrrrrrrrrrrrrrr\n" .
                "ggggggggrrrrrrrrrrrrrr\n" .
                "ggggggggrrrrrrrrrrrrrr\n" .
                "ggggggyyyyrrrrrrrrrrrr\n" .
                "ggggggyyyyrrrrrrrrrrrr\n" .
                "gggggyyyyyyrrrrrrrrrrr\n" .
                "gggggyyyyyyrrrrrrrrrrr\n" .
                "ggggggyyyyrrrrrrrrrrrr\n" .
                "ggggggyyyyrrrrrrrrrrrr\n" .
                "ggggggggrrrrrrrrrrrrrr\n" .
                "ggggggggrrrrrrrrrrrrrr\n" .
                "ggggggggrrrrrrrrrrrrrr\n" .
                "ggggggggrrrrrrrrrrrrrr\n" ;

    my $html = aa2ht( { td => { width => 3 , height => 3 } } , $table);

    # $html now holds a table with a color representation of your
    # ascii art. In this case, the Portuguese flag.

=cut

our %default_configuration;

=head1 FUNCTIONS

=head2 aa2ht

Gets ascii text and converts it to an HTML table. This is how it works:

=over 4

=item * each line is a C<tr> element

=item * each letter is a C<td> element

=item * each C<td> has background of a specific color, which is
defined by the letter that created it

=back

=cut

sub aa2ht {

  # default configuration
  my %config = _clone_hash( \%default_configuration );

=head3 OPTIONS

You can pass a reference to a hash before the text you want to
convert.

=cut

  if ( ref($_[0]) eq 'HASH' ) {
    my $new_config = shift;

=head4 id

In order to save space in the output, C<td> and C<tr> elements'
attributes are not in each element, but rather in a C<style> element.

This causes a problem if you want to put two different outputs with
different attributes on the same page.

To solve this problem: C<id>.

When creating a table, use the parameter C<id> to make sure it doesn't
end up mixed up with something else.

  my $html = aa2ht( { 'id' => 'special' } $ascii );

The result will be something like this:

  <style>
  .special td { width:1; height:1; }
  .special tr {  }
  </style>
  <table class="special" cellspacing="0" cellpadding="0" border="0">

=cut

    if (defined $new_config->{'id'}) { $config{'id'} = $new_config->{'id'} }

=head4 use-default-colors

If set to a false value, no default mappings are used.

  my $html = aa2ht( { 'use-default-colors' => 0 }, $ascii);

Behind the curtains, there is still a mapping: the default mapping to
white.

=cut

    if ( defined $new_config->{'use-default-colors'} ) {
      if ( not $new_config->{'use-default-colors'}) {
        $config{'colors'} = { 'default' => 'ffffff' } # everything is now white
      }
    }

=head4 colors

lib/Acme/AsciiArt2HtmlTable.pm  view on Meta::CPAN


=cut

BEGIN {

  # default configuration
  %default_configuration = (
            id    =>    'default',
            table => {
                        'border'      => 0,
                        'cellpadding' => 0,
                        'cellspacing' => 0,
                     },
            tr    => {
                     },
            td    => {
                        'width'       => '1px',
                        'height'      => '1px',
                     },
            colors=> { 
                        ' ' => 'ffffff',     # white

                        'l' => '000000',     # black
                        'b' => '0000ff',     # blue
                        'o' => 'a52a2a',     # brown
                        'g' => '00ff00',     # green
                        'a' => 'bebebe',     # gray
                        'e' => 'bebebe',     # grey
                        'm' => 'ff00ff',     # magenta
                        'o' => 'ffa500',     # orange
                        'p' => 'ffc0cb',     # pink
                        'u' => 'a020f0',     # purple
                        'r' => 'ff0000',     # red
                        'w' => 'ffffff',     # white
                        'y' => 'ffff00',     # yellow

                        'L' => '000000',     # light black
                        'B' => 'add8e6',     # light blue
                        'O' => 'a52a2a',     # light brown
                        'G' => '90ee90',     # light green
                        'A' => 'd3d3d3',     # light gray
                        'E' => 'd3d3d3',     # light grey
                        'M' => 'ff00ff',     # light magenta
                        'O' => 'ffa500',     # light orange
                        'P' => 'ffb6c1',     # light pink
                        'U' => '9370db',     # light purple
                        'R' => 'cd5c5c',     # light red
                        'W' => 'ffffff',     # light white
                        'Y' => 'ffffe0',     # light yellow

                        default => '000000', # black
                     },
            'randomize-new-colors' => 0,
            'optimization'         => 0,
          );

}

# subroutines

sub _random_color {
  my $color = '';

  for (1 .. 6) {
    $color .= qw/1 2 3 4 5 6 7 8 9 0 a b c d e f/[int rand 16];
  }

  return $color;
}

sub _clone_hash {
  my %hash = %{+shift};

  my %new_hash;

  for (keys %hash) {
    if (ref($hash{$_})) { 
      $new_hash{$_} = { _clone_hash ( $hash{$_} ) };
    }
    else {
      $new_hash{$_} = $hash{$_};
    }
  }

  return %new_hash;
}

sub _count_in_the_beginning {
  my ($cell, @elems) = @_;
  my $t = 0;
  for (@elems) {
    if ($cell eq $_) {
      $t++;
    }
    else {
      last;
    }
  }
  return $t;
}

sub _min {
  my $min = shift;

  for (@_) {
    if ( $min > $_ ) { $min = $_ }
  }

  return $min;
}

sub _max {
  my $max = shift;

  for (@_) {
    if ( $max < $_ ) { $max = $_ }
  }

  return $max;
}

=head1 SEE ALSO

The examples/ directory.

=head1 AUTHOR

Jose Castro, C<< <cog@cpan.org> >>

=head1 CAVEATS

If you specify the C<rowspan> or C<colspan> for C<td> elements and you
also ask for optimization... I don't even want to imagine what will
happen...

=head1 BUGS

Please report any bugs or feature requests to
C<bug-acme-tablethis@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.  I will be notified, and then you'll automatically
be notified of progress on your bug as I make changes.

=head1 COPYRIGHT & LICENSE

Copyright 2005 Jose Castro, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of Acme::AsciiArt2HtmlTable



( run in 2.132 seconds using v1.01-cache-2.11-cpan-7fcb06a456a )