Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
#!/usr/bin/perl -w

use strict;

# I want to make it into a module, someday
package Text::Delirium;

$Text::Delirium::default_amplitude = 2;
$Text::Delirium::default_palette   = [
    '#FF9900',
    '#FFFF00',
    '#009900',
    '#0099FF',
    '#0000FF',
    '#6600FF',
    '#FF00FF',
]

# constructor 
#   amplitude   ((absolute(magnitude - 1)) of font size variations)
#   palette     (array ref of colors to be used)
# ______________________________________
sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = { };

    if (@_) {
        (@_ & 1) || die "Odd number of parameters\n";
        my %opt = @_;
        $self->{palette}   = (defined ($opt{palette}))
            ? $opt{palette}
            : $Text::Delirium::default_palette;
        $self->{amplitude} = (defined ($opt{amplitude}))
            ? $opt{amplitude}
            : $Text::Delirium::default_amplitude;
    } else {
        $self->{palette}   = $Text::Delirium::default_palette;
        $self->{amplitude} = $Text::Delirium::default_amplitude;
    }
    bless($self => $class);
}

# ______________________________________
sub __random_color {
    my $self = shift;
    my $palette_len = scalar @{$self->{palette}};
    return $self->{palette}int rand $palette_len;
}

# ______________________________________
sub __random_size {
    my $self  = shift;
    my $delta = int rand($self->{amplitude});
    my $sign  = (int rand(2)) ? "+" : "-";
    if ($delta) {
        return sprintf(' size="%s%d"', $sign, $delta);
    } else {
        return "";
    }
}

# ______________________________________
sub convert_char {
    my $self = shift;
    my $c    = shift;
    my ($r, $rc, $rs);

    if ($c =~ /\s/) {
        $r = $c;
    } else {
        $rc = $self->__random_color();
        $rs = $self->__random_size();
        $r = sprintf(qq{<font%s color="%s">%s</font>}, $rs, $rc, $c);
    }
    return $r;
}

# ______________________________________
sub convert_string {
    my $self = shift;
    my $s    = shift;
    return join("", map { $self->convert_char($_) } split("", $s));
}

# ______________________________________
sub convert_file {
    my $self = shift;
    my $in   = shift || *STDIN;
    my $out  = shift || *STDOUT;

    while (<$in>) {
        print $out $self->convert_string($_);
    }
}

#
#
# "http://ccwf.cc.utexas.edu/~brenna/delirium/index.html";
#
#

package main;
# use Text::Delirium
use HTML::Parser;
use Getopt::Std;

#______________________________________
sub text {
    my $p    = shift;
    my $text = shift;
    print $text;
}

# arg1 -> filter
# arg2 -> file handle
#______________________________________
sub filter_factory {
    my $filter = shift;
    my $stream = shift;
    return sub {
        my $p    = shift;
        my $text = shift;
        print $stream $filter->convert_string($text);
    };
}

my $delirium = filter_factory (
    Text::Delirium->new(),
    *STDOUT,
);

my $p = HTML::Parser->new (
    api_version => 3,
    start_h     => [ \&text,    "self, text" ],
    end_h       => [ \&text,    "self, text" ],
    text_h      => [ $delirium, "self, text" ],
    comment_h   => [ \&text,    "self, text" ],
);
$p->unbroken_text(1);

$p->parse_file(*STDIN);

__END__

=head1 NAME

delirium - filter to make your HTML delirious

=head1 SYNOPSIS

delirium < STDIN > STDOUT

=head1 DESCRIPTION

Each character in the stream is given a pseudo-random size and color.
Basically, it turns:

    blah

into:

    b
    l
    a
    h

It really bloats your HTML by a large factor, but it looks interesting
enough to justify its use on special occasions.

I was inspired to write this script when I saw someone post a very
colourful message on the AfterY2K Forum.

    http://www.geekculture.com/ultimatebb/Forum9/HTML/000018.html

Apparently, he used emacs lisp to do this.  Of course, I used Perl.

=head1 OPTIONS

none, yet.

=head1 SEE ALSO

http://ccwf.cc.utexas.edu/~brenna/delirium/index.html

=head1 AUTHOR

John BEPPU <beppu@lineo.com>

=cut

# $Id: delirium,v 1.6 2000/07/04 00:12:19 beppu Exp $

In reply to delirium by beppu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (9)
    As of 2014-12-27 20:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (177 votes), past polls