package Yuki::RSS; use strict; use vars qw($VERSION); $VERSION = '0.2'; =head1 NAME Yuki::RSS - The smallest module to generate RSS 1.0. It is downward compatible to XML::RSS. =head1 SYNOPSIS use strict; use Yuki::RSS; my $rss = new Yuki::RSS( version => '1.0', encoding => 'Shift_JIS' ); $rss->channel( title => "Site Title", link => "http://url.of.your.site/", description => "The description of your site", ); $rss->add_item( title => "Item Title", link => "http://url.of.your/item.html", description => "Yoo, hoo, hoo", ); print $rss->as_string; =head1 DESCRIPTION Yuki::RSS is the smallest RSS 1.0 generator. This module helps you to create the minimum document of RSS 1.0. If you need more than that, use XML::RSS. =head1 METHODS =over 4 =item new Yuki::RSS (version => $version, encoding => $encoding) Constructor for Yuki::RSS. It returns a reference to a Yuki::RSS object. B must be 1.0. B will be inserted output document as a XML encoding. This module does not convert to this encoding. =item add_item (title => $title, link => $link, description => $description) Adds an item to the Yuki::RSS object. =item as_string Returns the RSS string. =item channel (title => $title, link => $link, description => $desc) Channel information of RSS. =head1 SEE ALSO =over 4 =item L =back =head1 AUTHOR Hiroshi Yuki http://www.hyuki.com/ =head1 LICENSE Copyright (C) 2001 by Hiroshi Yuki. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut # The constructor. sub new { my ($class, %hash) = @_; my $self = { version => $hash{version}, encoding => $hash{encoding}, channel => { }, items => [], }; return bless $self, $class; } # Setting channel. sub channel { my ($self, %hash) = @_; foreach (keys %hash) { $self->{channel}->{$_} = $hash{$_}; } return $self->{channel}; } # Adding item. sub add_item { my ($self, %hash) = @_; push(@{$self->{items}}, \%hash); return $self->{items}; } sub stylesheet ($%) { my $self = shift; $self->{stylesheet} = {@_}; } # sub as_string { my ($self) = @_; unless ($self->{channel}->{'dc:date'}) { $self->{channel}->{'dc:date'} = _rfc3339_date (time); } else { $self->{channel}->{'dc:date'} = _rfc3339_date ($self->{channel}->{'dc:date'}); } my $doc = <<"EOD"; {encoding}" ?> @{[ ref ($self->{stylesheet}) ? '{stylesheet}->{$_})]}")} keys %{$self->{stylesheet}})).'?>':'' ]} @{[&_escape($self->{channel}->{title})]} @{[&_escape($self->{channel}->{link})]} @{[&_escape($self->{channel}->{description})]} @{[&_escape($self->{channel}->{'dc:language'})]} $self->{channel}->{'dc:date'} @{[ map { qq{} } @{$self->{items}} ]} @{[ map { do { my $r = qq{\n}; for my $element (keys %{$_}) { my $c = $_->{$element}; $c = _rfc3339_date ($c) if $element eq 'dc:date'; $r .= qq{ <$element>@{[&_escape($c)]}\n}; } $r . "\n"; }} @{$self->{items}} ]} EOD } sub _escape { my $s = shift; $s =~ s|&|&|g; $s =~ s|<|<|g; $s =~ s|>|>|g; $s =~ s|"|"|g; return $s; } sub _rfc3339_date ($) { my @time = gmtime (shift); sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0]; } 1; # $Date: 2003/01/01 11:48:28 $