package XML::Atom::Feed::JavaScript; use strict; use warnings; use base qw( XML::Atom::Feed ); our $VERSION = 0.1; =head1 NAME XML::Atom::JavaScript - Atom syndication with JavaScript =head1 SYNOPSIS ## get an Atom feed from the network use XML::Atom::API; use XML::Atom::JavaScript; my $api = XML::Atom::API->new(); my $feed = $api->getFeed( 'http://example.com/atom.xml' ); print $feed->asJavascript(); ## get an atom feed from disk use XML::Atom::JavaScript; my $feed = XML::Atom::Feed->new( Stream => 'atom.xml' ); print $feed->asJavascript(); =head1 DESCRIPTION XML::Atom::Feed::JavaScript exports an additional function into the XML::Atom package for outputting Atom feeds as javascript. =head1 FUNCTIONS =head2 asJavascript() =head1 AUTHORS =over 4 =item David Jacobs =item Ed Summers =item Brian Cassidy =back =cut sub XML::Atom::Feed::asJavascript { my ( $feed, $max, $descriptions ) = @_ or die q( can't get feed ); my $items = scalar $feed->entries; if ( not $max or $max > $items ) { $max = $items; } ## open javascript section my $output = _jsPrint( '
' ); $output .= _jsPrint( '
' . $feed->title() . '
' ); ## open our list $output .= _jsPrint( '
    ' ); ## generate content for each item foreach my $item ( $feed->entries() ){ my $link = $item->link(); my $title = $item->title(); my $desc = $item->content->body(); my $data = <<"JAVASCRIPT_TEXT";
  • $title JAVASCRIPT_TEXT if ( $descriptions or not defined ( $descriptions ) ) { $data .= " $desc"; } $data .= '
  • '; $output .= _jsPrint( $data ); } ## close our item list, and return $output .= _jsPrint( '
' ); $output .= _jsPrint( '
' ); return $output; } sub _jsPrint { my $string = shift; $string =~ s/"/\\"/g; $string =~ s/'/\\'/g; $string =~ s/\n//g; return( "document.write('$string');\n" ); } 1;