XML-Grammar-Fortune

 view release on metacpan or  search on metacpan

lib/XML/Grammar/Fortune/ToText.pm  view on Meta::CPAN

    continue
    {
        $continue_cb->();

        if ( $list->size() )
        {
            $self->$if_remainaing_meth();
        }
    }

    return;
}


sub run
{
    my $self = shift;

    my $xml = XML::LibXML->new->parse_file( $self->_input() );

    $self->_iterate_on_child_elems(
        $xml,
        "//fortune",
        {
            process => '_render_single_fortune_cookie',
            if_more => '_output_next_fortune_delim',
        }
    );

    my $buf = ${ $self->_buf() };

    $buf =~ s/[ \t]+$//gms;

    print { $self->_output() } $buf;

    return;
}

sub _render_info_if_exists
{
    my ( $self, $fortune_node ) = @_;

    if ( my ($info_node) = $fortune_node->findnodes("descendant::info") )
    {
        $self->_render_info_node($info_node);
    }

    return;
}

sub _process_raw_node
{
    my ( $self, $raw_node ) = @_;

    my ($text_node) = $raw_node->findnodes("body/text");

    my @text_childs = $text_node->childNodes();

    if ( @text_childs != 1 )
    {
        Carp::confess('@cdata is not 1');
    }

    my $cdata = $text_childs[0];

    if ( $cdata->nodeType() != XML_CDATA_SECTION_NODE() )
    {
        Carp::confess("Not a cdata");
    }

    my $value = $cdata->nodeValue();

    $value =~ s{\n+\z}{}g;
    $self->_out("$value\n");

    return;
}

sub _process_irc_node
{
    my ( $self, $irc_node ) = @_;

    my ($body_node) = $irc_node->findnodes("body");

    my @lines_list = $body_node->findnodes("saying|me_is|joins|leaves");

    use List::Util qw(max);

    my $longest_nick_len = 0;
    my @messages;
    foreach my $line (@lines_list)
    {
        if ( $line->localname() eq "saying" )
        {
            my $nick = $line->getAttribute("who");
            push @messages,
                {
                type => "say",
                nick => $nick,
                msg  => $line->textContent(),
                };

            $longest_nick_len = max( $longest_nick_len, length($nick) );
        }
        elsif ( $line->localname() eq "me_is" )
        {
            my $nick = $line->getAttribute("who");
            push @messages,
                {
                type => "me_is",
                nick => $nick,
                msg  => $line->textContent(),
                };

            $longest_nick_len = max( $longest_nick_len, length("*") );
        }
        elsif ( $line->localname() eq "joins" )
        {
            my $nick = $line->getAttribute("who");
            push @messages,
                {
                type => "joins",
                nick => $nick,
                msg  => $line->textContent(),
                };

            $longest_nick_len = max( $longest_nick_len, length("<--") );
        }
        elsif ( $line->localname() eq "leaves" )
        {
            my $nick = $line->getAttribute("who");



( run in 0.851 second using v1.01-cache-2.11-cpan-39bf76dae61 )