| 1 |
package XML::Feed::Entry::Format::RSS; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use v5.10; |
| 5 |
|
| 6 |
our $VERSION = '1.0.1'; |
| 7 |
|
| 8 |
sub format { 'RSS ' . $_[0]->{'_version'} } |
| 9 |
|
| 10 |
use XML::Feed::Content; |
| 11 |
use XML::Feed::Util qw( format_w3cdtf parse_mail_date parse_w3cdtf_date ); |
| 12 |
|
| 13 |
use base qw( XML::Feed::Entry ); |
| 14 |
|
| 15 |
sub init_empty { $_[0]->{entry} = { } } |
| 16 |
|
| 17 |
sub base { |
| 18 |
my $entry = shift; |
| 19 |
@_ ? $entry->{entry}->{'xml:base'} = $_[0] : $entry->{entry}->{'xml:base'}; |
| 20 |
} |
| 21 |
|
| 22 |
sub title { |
| 23 |
my $entry = shift; |
| 24 |
@_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title}; |
| 25 |
} |
| 26 |
|
| 27 |
sub link { |
| 28 |
my $entry = shift; |
| 29 |
if (@_) { |
| 30 |
$entry->{entry}{link} = $_[0]; |
| 31 |
## For RSS 2.0 output from XML::RSS. Sigh. |
| 32 |
$entry->{entry}{permaLink} = $_[0]; |
| 33 |
} else { |
| 34 |
my $link = $entry->{entry}{link} // |
| 35 |
$entry->{entry}{permaLink} // |
| 36 |
$entry->{entry}{guid}; |
| 37 |
if (defined $link) { |
| 38 |
$link =~ s/^\s+//; |
| 39 |
$link =~ s/\s+$//; |
| 40 |
} |
| 41 |
return $link; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
sub summary { |
| 46 |
my $entry = shift; |
| 47 |
my $item = $entry->{entry}; |
| 48 |
if (@_) { |
| 49 |
$item->{description} = ref($_[0]) eq 'XML::Feed::Content' ? |
| 50 |
$_[0]->body : $_[0]; |
| 51 |
$entry->{description_is_summary} = 1; |
| 52 |
} else { |
| 53 |
## Some RSS feeds use <description> for a summary, and some use it |
| 54 |
## for the full content. Pretty gross. We don't want to return the |
| 55 |
## full content if the caller expects a summary, so the heuristic is: |
| 56 |
## if the <entry> contains both a <description> and one of the elements |
| 57 |
## typically used for the full content, use <description> as summary. |
| 58 |
my $txt; |
| 59 |
if ($item->{description} && |
| 60 |
($entry->{description_is_summary} || |
| 61 |
$entry->_content // |
| 62 |
$item->{'http://www.w3.org/1999/xhtml'}{body})) { |
| 63 |
$txt = $item->{description}; |
| 64 |
## Blogspot's 'short' RSS feeds do this in the Atom namespace |
| 65 |
## for no obviously good reason. |
| 66 |
} elsif ($item->{'http://www.w3.org/2005/Atom'}{summary}) { |
| 67 |
$txt = $item->{'http://www.w3.org/2005/Atom'}{summary}; |
| 68 |
} |
| 69 |
XML::Feed::Content->wrap({ type => 'text/plain', body => $txt }); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
# Get contentfrom HASH ref or scalar. |
| 74 |
sub _content { |
| 75 |
my $entry = shift; |
| 76 |
my $content = $entry->{entry}{content}; |
| 77 |
return ref $content ? $content->{encoded} : $content; |
| 78 |
} |
| 79 |
|
| 80 |
sub content { |
| 81 |
my $entry = shift; |
| 82 |
my $item = $entry->{entry}; |
| 83 |
if (@_) { |
| 84 |
my $c; |
| 85 |
if (ref($_[0]) eq 'XML::Feed::Content') { |
| 86 |
if (defined $_[0]->base) { |
| 87 |
$c = { 'content' => $_[0]->body, 'xml:base' => $_[0]->base }; |
| 88 |
} else { |
| 89 |
$c = $_[0]->body; |
| 90 |
} |
| 91 |
} else { |
| 92 |
$c = $_[0]; |
| 93 |
} |
| 94 |
$item->{content} = {} unless ref $item->{content}; |
| 95 |
$item->{content}{encoded} = $c; |
| 96 |
} else { |
| 97 |
my $base; |
| 98 |
my $body = |
| 99 |
$entry->_content // |
| 100 |
$item->{'http://www.w3.org/1999/xhtml'}{body} // |
| 101 |
$item->{description}; |
| 102 |
if ('HASH' eq ref($body)) { |
| 103 |
$base = $body->{'xml:base'}; |
| 104 |
$body = $body->{content}; |
| 105 |
} |
| 106 |
XML::Feed::Content->wrap({ type => 'text/html', body => $body, base => $base }); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
sub category { |
| 111 |
my $entry = shift; |
| 112 |
my $item = $entry->{entry}; |
| 113 |
if (@_) { |
| 114 |
my @tmp = ($entry->category, @_); |
| 115 |
$item->{category} = [@tmp]; |
| 116 |
$item->{dc}{subject} = [@tmp]; |
| 117 |
} else { |
| 118 |
my $r = $item->{category} // $item->{dc}{subject}; |
| 119 |
my @r = ref($r) eq 'ARRAY' ? @$r : defined $r? ($r) : (); |
| 120 |
return wantarray? @r : $r[0]; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
sub author { |
| 125 |
my $item = shift->{entry}; |
| 126 |
if (@_) { |
| 127 |
$item->{author} = $item->{dc}{creator} = $_[0]; |
| 128 |
} else { |
| 129 |
$item->{author} // $item->{dc}{creator}; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
## XML::RSS doesn't give us access to the rdf:about for the <item>, |
| 134 |
## so we have to fall back to the <link> element in RSS 1.0 feeds. |
| 135 |
sub id { |
| 136 |
my $item = shift->{entry}; |
| 137 |
if (@_) { |
| 138 |
$item->{guid} = $_[0]; |
| 139 |
} else { |
| 140 |
$item->{guid} // $item->{permaLink} // $item->{link}; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
sub issued { |
| 145 |
my $item = shift->{entry}; |
| 146 |
if (@_) { |
| 147 |
$item->{dc}{date} = format_w3cdtf($_[0]); |
| 148 |
$item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]); |
| 149 |
} else { |
| 150 |
return parse_mail_date($item->{pubDate}) |
| 151 |
|| parse_w3cdtf_date($item->{dc}{date} || $item->{dcterms}{date}); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
sub modified { |
| 156 |
my $item = shift->{entry}; |
| 157 |
if (@_) { |
| 158 |
$item->{dcterms}{modified} = format_w3cdtf($_[0]); |
| 159 |
} else { |
| 160 |
return parse_w3cdtf_date( |
| 161 |
$item->{dcterms}{modified} || $item->{atom}{updated} |
| 162 |
); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
sub lat { |
| 167 |
my $item = shift->{entry}; |
| 168 |
if (@_) { |
| 169 |
$item->{geo}{lat} = $_[0]; |
| 170 |
} else { |
| 171 |
return $item->{geo}{lat}; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
sub long { |
| 176 |
my $item = shift->{entry}; |
| 177 |
if (@_) { |
| 178 |
$item->{geo}{long} = $_[0]; |
| 179 |
} else { |
| 180 |
return $item->{geo}{long}; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
sub enclosure { |
| 185 |
my $entry = shift; |
| 186 |
|
| 187 |
if (@_) { |
| 188 |
my $enclosure = shift; |
| 189 |
my $val = { |
| 190 |
url => $enclosure->{url}, |
| 191 |
type => $enclosure->{type}, |
| 192 |
length => $enclosure->{length} |
| 193 |
}; |
| 194 |
if ($XML::Feed::MULTIPLE_ENCLOSURES) { |
| 195 |
push @{$entry->{entry}->{enclosure}}, $val; |
| 196 |
} else { |
| 197 |
$entry->{entry}->{enclosure} = $val; |
| 198 |
} |
| 199 |
} else { |
| 200 |
my $tmp = $entry->{entry}->{enclosure}; |
| 201 |
if (defined $tmp) { |
| 202 |
my @encs = map { XML::Feed::Enclosure->new($_) } |
| 203 |
(ref $tmp eq 'ARRAY')? @$tmp : ($tmp); |
| 204 |
return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1]; |
| 205 |
} |
| 206 |
return; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
1; |
| 211 |
|