display | more...

A simple perl module to parse E2's chatterbox XML ticker. It's used with the XML::Parser module. POD (with a short example) is at the bottom. It converts the href's to square brackets, just like the nodelet. If you use it, remember to escape the '&' in the chatterbox XML ticker output, otherwise it dies, saying that the xml is not well-formed.

There's also a Perl E2 private message XML ticker parser.

package E2Chatterbox::ChatterParser;

use strict;

########################################################################
########################################################################
###
### XML subs
###
########################################################################
########################################################################
sub Init {
  my $expat = shift();

  $expat->{speaker} = undef;
  $expat->{message} = undef;
  $expat->{chatter} = [];
  $expat->{time} = undef;
  $expat->{room} = undef;
}


sub Char {
  my ( $expat, $string ) = @_;

  if ( defined($expat->{speaker}) ) {
    $string =~ s/\n//g;

    $expat->{message} .= $string;
  }
}

sub Start {
  my ( $expat, $element, %attr ) = @_;
  $element = lc( $element );

  if ( $element eq 'message' ) {
    $expat->{speaker} = $attr{author};
    $expat->{time}    = $attr{time};
  }
  elsif ( $element eq 'a' ) {
    $expat->{message} .= '[';
  }
  elsif ( $element eq 'info' ) {
    $expat->{room} = $attr{room};
  }
}

sub End {
  my ( $expat, $element ) = @_;
  $element = lc( $element );

  if ( $element eq 'message' ) {
    if ( $expat->{time} > $expat->{last_time} ) {
      push( @{$expat->{chatter}},
        [ $expat->{time}, $expat->{speaker}, $expat->{message} ] );
      $expat->{last_time} = $expat->{time};
    }

    $expat->{speaker} = undef;
    $expat->{message} = undef;
    $expat->{time} = undef;
  }
  elsif ( $element eq 'a' ) {
    $expat->{message} .= ']';
  }
}

sub Final {
  my $expat = shift();

  return ( $expat->{chatter}, $expat->{last_time} );
}


1;

=pod

=head1 NAME

E2ChatterboxCParser - XML parser for chatterbox XML ticker

=head1 SYNOPSIS

 use XML::Parser;

 my $parser = new XML::Parser( 
   Style => "E2Chatterbox::ChatterParser" 
  );

 my ($chatter_log, $last_time) = $parser->parse( $xml, 'last_time' => $time );

 foreach my $line ( @$chatter_log ) {
   my ( $time, $speaker, $speech ) = @$line;
   print "[$time] $speaker: $speech\n";
 }

=head1 DESCRIPTION

This is a style for the XML::Parser perl module that deals
with E2's xml chatterbox ticker (/?node=chatterbox+XML+ticker).
It requires two arguments (the xml from the ticker and the
time to start reading), and returns two values (a reference
to an array of arrays and the time of the last message read.)

=cut

Log in or register to write something here or to contact authors.