| 1 | package XML::FeedWriter::RSS20; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use Carp; |
|---|
| 6 | use base qw( XML::FeedWriter::Base ); |
|---|
| 7 | |
|---|
| 8 | __PACKAGE__->_alias({ |
|---|
| 9 | creator => 'dc:creator', |
|---|
| 10 | pubdate => 'pubDate', |
|---|
| 11 | published => 'pubDate', |
|---|
| 12 | lastbuilddate => 'lastBuildDate', |
|---|
| 13 | updated => 'lastBuildDate', |
|---|
| 14 | webmaster => 'webMaster', |
|---|
| 15 | managingeditor => 'managingEditor', |
|---|
| 16 | editor => 'managingEditor', |
|---|
| 17 | content => 'content:encoded', |
|---|
| 18 | summary => 'description', |
|---|
| 19 | }); |
|---|
| 20 | |
|---|
| 21 | __PACKAGE__->_requires({ |
|---|
| 22 | channel => [qw( description link title )], |
|---|
| 23 | cloud => [qw( domain path port protocol registerProcedure )], |
|---|
| 24 | image => [qw( link title url )], |
|---|
| 25 | textinput => [qw( description link name title )], |
|---|
| 26 | enclosure => [qw( length type url )], |
|---|
| 27 | }); |
|---|
| 28 | |
|---|
| 29 | __PACKAGE__->_sort_order({ |
|---|
| 30 | title => 10, |
|---|
| 31 | link => 9, |
|---|
| 32 | description => 8, |
|---|
| 33 | creator => 7, |
|---|
| 34 | author => 7, |
|---|
| 35 | pubDate => 6, |
|---|
| 36 | guid => 5, |
|---|
| 37 | }); |
|---|
| 38 | |
|---|
| 39 | sub _extra_options { |
|---|
| 40 | my ($self, $options) = @_; |
|---|
| 41 | |
|---|
| 42 | my $no_cdata = delete $options->{no_cdata}; |
|---|
| 43 | |
|---|
| 44 | $self->{_use_cdata} = !$no_cdata; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | sub _root_element { |
|---|
| 48 | my ($self, $modules) = @_; |
|---|
| 49 | |
|---|
| 50 | $self->xml->startTag( rss => |
|---|
| 51 | version => '2.0', |
|---|
| 52 | 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', |
|---|
| 53 | 'xmlns:content' => 'http://purl.org/rss/1.0/modules/content/', |
|---|
| 54 | %{ $modules }, |
|---|
| 55 | ); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | sub _channel { |
|---|
| 59 | my ($self, $channel) = @_; |
|---|
| 60 | |
|---|
| 61 | $channel->{lastBuildDate} ||= $self->dtx->for_rss20; |
|---|
| 62 | |
|---|
| 63 | $self->xml->startTag('channel'); |
|---|
| 64 | |
|---|
| 65 | foreach my $key ( $self->_sort_keys( $channel ) ) { |
|---|
| 66 | |
|---|
| 67 | if ( $key eq 'category' ) { |
|---|
| 68 | $self->_duplicable_elements( $key => $channel->{$key} ); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | elsif ( $key eq 'cloud' ) { |
|---|
| 72 | $self->_empty_element( $key => $channel->{$key} ); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | elsif ( $key =~ /image|textinput/ ) { |
|---|
| 76 | $self->_element_with_children( $key => $channel->{$key} ); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | elsif ( $key =~ /lastBuildDate|pubDate/ ) { |
|---|
| 80 | $self->_datetime_element( $key => $channel->{$key} ); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | elsif ( my ($type) = $key =~ /skip(Day|Hour)s/ ) { |
|---|
| 84 | $self->_element_with_duplicable_children( |
|---|
| 85 | $key => $channel->{$key}, lc $type |
|---|
| 86 | ); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | else { |
|---|
| 90 | $self->_data_element( $key => $channel->{$key} ); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | sub add_items { |
|---|
| 96 | my ($self, @items) = @_; |
|---|
| 97 | |
|---|
| 98 | croak "can't add items any longer" if $self->_closed; |
|---|
| 99 | |
|---|
| 100 | foreach my $i ( @items ) { |
|---|
| 101 | my %item = $self->_canonize( $i ); |
|---|
| 102 | |
|---|
| 103 | $self->xml->startTag('item'); |
|---|
| 104 | foreach my $key ( $self->_sort_keys( \%item ) ) { |
|---|
| 105 | |
|---|
| 106 | if ( $key eq 'pubDate' ) { |
|---|
| 107 | $self->_datetime_element( $key => $item{$key} ); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | elsif ( $key eq 'description' ) { |
|---|
| 111 | $self->_cdata_element( $key => $item{$key} ); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | elsif ( $key eq 'enclosure' ) { |
|---|
| 115 | $self->_empty_element( $key => $item{$key} ); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | elsif ( $key eq 'category' ) { |
|---|
| 119 | $self->_duplicable_elements( $key => $item{$key} ); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | else { |
|---|
| 123 | $self->_data_element( $key => $item{$key} ); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | $self->xml->endTag('item'); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | sub close { |
|---|
| 131 | my $self = shift; |
|---|
| 132 | |
|---|
| 133 | return if $self->_closed; |
|---|
| 134 | |
|---|
| 135 | $self->xml->endTag('channel'); |
|---|
| 136 | $self->xml->endTag('rss'); |
|---|
| 137 | $self->xml->end; |
|---|
| 138 | |
|---|
| 139 | $self->_closed(1); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | 1; |
|---|
| 143 | |
|---|
| 144 | __END__ |
|---|
| 145 | |
|---|
| 146 | =head1 NAME |
|---|
| 147 | |
|---|
| 148 | XML::FeedWriter::RSS20 |
|---|
| 149 | |
|---|
| 150 | =head1 SYNOPSIS |
|---|
| 151 | |
|---|
| 152 | use XML::FeedWriter; |
|---|
| 153 | |
|---|
| 154 | # let's create a writer. |
|---|
| 155 | |
|---|
| 156 | my $writer = XML::FeedWriter->new( |
|---|
| 157 | |
|---|
| 158 | # specify type/version; RSS 2.0 by default |
|---|
| 159 | version => '2.0', |
|---|
| 160 | |
|---|
| 161 | # and channel info |
|---|
| 162 | title => 'feed title', |
|---|
| 163 | link => 'http://example.com', |
|---|
| 164 | description => 'blah blah blah', |
|---|
| 165 | ); |
|---|
| 166 | |
|---|
| 167 | # add as many items as you wish (and spec permits). |
|---|
| 168 | |
|---|
| 169 | $writer->add_items( |
|---|
| 170 | # each item should be a hash reference |
|---|
| 171 | { |
|---|
| 172 | title => 'first post', |
|---|
| 173 | description => 'plain text of the first post', |
|---|
| 174 | link => 'http://example.com/first_post', |
|---|
| 175 | updated => time(), # will be converted to a pubDate |
|---|
| 176 | creator => 'me', # alias for "dc:creator" |
|---|
| 177 | }, |
|---|
| 178 | { |
|---|
| 179 | title => 'second post', |
|---|
| 180 | description => '<p>html of the second post</p>', |
|---|
| 181 | link => 'http://example.com/second_post', |
|---|
| 182 | pubdate => DateTime->now, # will be formatted properly |
|---|
| 183 | creator => 'someone', |
|---|
| 184 | }, |
|---|
| 185 | ); |
|---|
| 186 | |
|---|
| 187 | # this will close several tags such as root 'rss'. |
|---|
| 188 | |
|---|
| 189 | $writer->close; |
|---|
| 190 | |
|---|
| 191 | # then, if you want to save the feed to a file |
|---|
| 192 | |
|---|
| 193 | $writer->save('path_to_file.xml'); |
|---|
| 194 | |
|---|
| 195 | # or just use it as an xml string. |
|---|
| 196 | |
|---|
| 197 | my $string = $writer->as_string; |
|---|
| 198 | |
|---|
| 199 | =head1 DESCRIPTION |
|---|
| 200 | |
|---|
| 201 | This is an RSS 2.0 feed writer. You usually don't need to use this directly, but if you insist, replace XML::FeedWriter with XML::FeedWriter::RSS20 and it works fine. See L<XML::FeedWriter> for basic usage. |
|---|
| 202 | |
|---|
| 203 | =head1 METHODS |
|---|
| 204 | |
|---|
| 205 | =head2 new |
|---|
| 206 | =head2 add_items |
|---|
| 207 | =head2 close |
|---|
| 208 | =head2 save |
|---|
| 209 | =head2 as_string |
|---|
| 210 | |
|---|
| 211 | =head1 SEE ALSO |
|---|
| 212 | |
|---|
| 213 | L<http://www.rssboard.org/rss-profile> |
|---|
| 214 | |
|---|
| 215 | L<XML::FeedWriter> |
|---|
| 216 | |
|---|
| 217 | =head1 AUTHOR |
|---|
| 218 | |
|---|
| 219 | Kenichi Ishigaki, E<lt>ishigaki@cpan.orgE<gt> |
|---|
| 220 | |
|---|
| 221 | =head1 COPYRIGHT AND LICENSE |
|---|
| 222 | |
|---|
| 223 | Copyright (C) 2008 by Kenichi Ishigaki. |
|---|
| 224 | |
|---|
| 225 | This program is free software; you can redistribute it and/or |
|---|
| 226 | modify it under the same terms as Perl itself. |
|---|
| 227 | |
|---|
| 228 | =cut |
|---|