| 1 |
package XML::Feed::Entry::Format::Atom; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use v5.10; |
| 5 |
|
| 6 |
our $VERSION = '1.0.1'; |
| 7 |
|
| 8 |
use base qw( XML::Feed::Entry ); |
| 9 |
use XML::Feed::Content; |
| 10 |
use XML::Feed::Util qw( format_w3cdtf parse_datetime ); |
| 11 |
use XML::Atom::Entry; |
| 12 |
use List::Util qw( first ); |
| 13 |
|
| 14 |
sub init_empty { |
| 15 |
my $entry = shift; |
| 16 |
$entry->{entry} = XML::Atom::Entry->new(Version => 1.0); |
| 17 |
1; |
| 18 |
} |
| 19 |
|
| 20 |
sub format { 'Atom' } |
| 21 |
|
| 22 |
sub title { shift->{entry}->title(@_) } |
| 23 |
sub source { shift->{entry}->source(@_) } |
| 24 |
sub updated { shift->modified(@_) } |
| 25 |
sub base { shift->{entry}->base(@_) } |
| 26 |
|
| 27 |
sub link { |
| 28 |
my $entry = shift; |
| 29 |
if (@_) { |
| 30 |
$entry->{entry}->add_link({ rel => 'alternate', href => $_[0], |
| 31 |
type => 'text/html', }); |
| 32 |
} else { |
| 33 |
my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link; |
| 34 |
$l ? $l->href : undef; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
sub summary { |
| 39 |
my $entry = shift; |
| 40 |
|
| 41 |
my %type_correction = ( |
| 42 |
html => 'text/html', |
| 43 |
text => 'text/plain', |
| 44 |
xhtml => 'text/html', |
| 45 |
); |
| 46 |
|
| 47 |
if (@_) { |
| 48 |
my %param; |
| 49 |
if (ref($_[0]) eq 'XML::Feed::Content') { |
| 50 |
%param = (Body => $_[0]->body); |
| 51 |
} else { |
| 52 |
%param = (Body => $_[0]); |
| 53 |
} |
| 54 |
$entry->{entry}->summary(XML::Atom::Content->new(%param, Version => 1.0)); |
| 55 |
} else { |
| 56 |
my $s = $entry->{entry}->summary; |
| 57 |
# map Atom types to MIME types |
| 58 |
my $type = ($s && ref($s) eq 'XML::Feed::Content') ? $s->type : undef; |
| 59 |
if ($type and exists $type_correction{$type}) { |
| 60 |
$type = $type_correction{$type}; |
| 61 |
} |
| 62 |
my $body = $s; |
| 63 |
if (defined $s && ref($s) eq 'XML::Feed::Content') { |
| 64 |
$body = $s->body; |
| 65 |
} |
| 66 |
XML::Feed::Content->wrap({ type => $type, |
| 67 |
body => $body }); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
my %types = ( |
| 72 |
'text/xhtml' => 'xhtml', |
| 73 |
'text/html' => 'html', |
| 74 |
'text/plain' => 'text', |
| 75 |
); |
| 76 |
|
| 77 |
sub content { |
| 78 |
my $entry = shift; |
| 79 |
if (@_) { |
| 80 |
return $entry->set_content(@_); |
| 81 |
} else { |
| 82 |
return $entry->get_content; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
sub set_content { |
| 87 |
my $entry = shift; |
| 88 |
|
| 89 |
my %param; |
| 90 |
my $base; |
| 91 |
my $orig_body; |
| 92 |
if (ref($_[0]) eq 'XML::Feed::Content') { |
| 93 |
$orig_body = $_[0]->body; |
| 94 |
if (defined $_[0]->type && defined $types{$_[0]->type}) { |
| 95 |
%param = (Body => $orig_body, Type => $types{$_[0]->type}); |
| 96 |
|
| 97 |
if ($param{'Type'} eq "html") { |
| 98 |
$param{'Body'} = HTML::Entities::encode_entities($param{'Body'}); |
| 99 |
} |
| 100 |
} else { |
| 101 |
} |
| 102 |
$base = $_[0]->base if defined $_[0]->base; |
| 103 |
} else { |
| 104 |
$orig_body = $_[0]; |
| 105 |
} |
| 106 |
if (!exists($param{Body})) |
| 107 |
{ |
| 108 |
$param{Body} = $orig_body; |
| 109 |
} |
| 110 |
$entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0)); |
| 111 |
# Assigning again so the type will be normalized. This seems to be |
| 112 |
# an XML-Atom do-what-I-don't-meannery. |
| 113 |
$entry->{entry}->content->body($orig_body); |
| 114 |
$entry->{entry}->content->base($base) if defined $base; |
| 115 |
} |
| 116 |
|
| 117 |
sub get_content { |
| 118 |
my $entry = shift; |
| 119 |
|
| 120 |
my $c = $entry->{entry}->content; |
| 121 |
|
| 122 |
# map Atom types to MIME types |
| 123 |
my $type = $c ? $c->type : undef; |
| 124 |
if ($type) { |
| 125 |
$type = 'text/html' if $type eq 'xhtml' || $type eq 'html'; |
| 126 |
$type = 'text/plain' if $type eq 'text'; |
| 127 |
} |
| 128 |
|
| 129 |
XML::Feed::Content->wrap({ type => $type, |
| 130 |
base => $c ? $c->base : undef, |
| 131 |
body => $c ? $c->body : undef }); |
| 132 |
} |
| 133 |
|
| 134 |
sub category { |
| 135 |
my $entry = shift; |
| 136 |
my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/'); |
| 137 |
if (@_) { |
| 138 |
$entry->{entry}->add_category({ term => $_ }) for @_; |
| 139 |
return 1; |
| 140 |
} else { |
| 141 |
|
| 142 |
|
| 143 |
my @category = ($entry->{entry}->can('categories')) ? $entry->{entry}->categories : $entry->{entry}->category; |
| 144 |
my @return = @category |
| 145 |
? (map { $_->label // $_->term } @category) |
| 146 |
: $entry->{entry}->getlist($ns, 'subject'); |
| 147 |
|
| 148 |
return wantarray? @return : $return[0]; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
sub author { |
| 153 |
my $entry = shift; |
| 154 |
if (@_ && $_[0]) { |
| 155 |
my $person = XML::Atom::Person->new(Version => 1.0); |
| 156 |
$person->name($_[0]); |
| 157 |
$entry->{entry}->author($person); |
| 158 |
} else { |
| 159 |
$entry->{entry}->author ? $entry->{entry}->author->name : undef; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
sub id { shift->{entry}->id(@_) } |
| 164 |
|
| 165 |
sub issued { |
| 166 |
my $entry = shift; |
| 167 |
if (@_) { |
| 168 |
$entry->{entry}->issued(format_w3cdtf($_[0])) if $_[0]; |
| 169 |
} else { |
| 170 |
parse_datetime($entry->{entry}->issued || $entry->{entry}->published); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
sub modified { |
| 175 |
my $entry = shift; |
| 176 |
if (@_) { |
| 177 |
$entry->{entry}->modified(format_w3cdtf($_[0])) if $_[0]; |
| 178 |
} else { |
| 179 |
parse_datetime($entry->{entry}->modified || $entry->{entry}->updated); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
sub lat { |
| 184 |
my $entry = shift; |
| 185 |
if (@_) { |
| 186 |
$entry->{entry}->lat($_[0]) if $_[0]; |
| 187 |
} else { |
| 188 |
$entry->{entry}->lat; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
sub long { |
| 193 |
my $entry = shift; |
| 194 |
if (@_) { |
| 195 |
$entry->{entry}->long($_[0]) if $_[0]; |
| 196 |
} else { |
| 197 |
$entry->{entry}->long; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
sub enclosure { |
| 203 |
my $entry = shift; |
| 204 |
|
| 205 |
if (@_) { |
| 206 |
my $enclosure = shift; |
| 207 |
my $method = ($XML::Feed::MULTIPLE_ENCLOSURES)? 'add_link' : 'link'; |
| 208 |
$entry->{entry}->$method({ rel => 'enclosure', href => $enclosure->{url}, |
| 209 |
length => $enclosure->{length}, |
| 210 |
type => $enclosure->{type} }); |
| 211 |
return 1; |
| 212 |
} else { |
| 213 |
my @links = grep { defined $_->rel && $_->rel eq 'enclosure' } $entry->{entry}->link; |
| 214 |
return unless @links; |
| 215 |
my @encs = map { |
| 216 |
XML::Feed::Enclosure->new({ |
| 217 |
url => $_->href, length => $_->length, type => $_->type |
| 218 |
}) |
| 219 |
} @links ; |
| 220 |
return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1]; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
1; |
| 226 |
|