1 package XML::Feed::Entry::Format::Atom;
4 use base qw( XML::Feed::Entry );
5 use XML::Atom::Util qw( iso2dt );
6 use XML::Feed::Content;
8 use List::Util qw( first );
12 $entry->{entry} = XML::Atom::Entry->new(Version => 1.0);
18 sub title { shift->{entry}->title(@_) }
19 sub source { shift->{entry}->source(@_) }
20 sub updated { shift->{entry}->updated(@_) }
21 sub base { shift->{entry}->base(@_) }
26 $entry->{entry}->add_link({ rel => 'alternate', href => $_[0],
27 type => 'text/html', });
29 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link;
30 $l ? $l->href : undef;
38 if (ref($_[0]) eq 'XML::Feed::Content') {
39 %param = (Body => $_[0]->body);
41 %param = (Body => $_[0]);
43 $entry->{entry}->summary(XML::Atom::Content->new(%param, Version => 1.0));
45 my $s = $entry->{entry}->summary;
46 # map Atom types to MIME types
47 my $type = ($s && ref($s) eq 'XML::Feed::Content') ? $s->type : undef;
49 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
50 $type = 'text/plain' if $type eq 'text';
53 if (defined $s && ref($s) eq 'XML::Feed::Content') {
56 XML::Feed::Content->wrap({ type => $type,
62 'text/xhtml' => 'xhtml',
63 'text/html' => 'html',
64 'text/plain' => 'text',
73 if (ref($_[0]) eq 'XML::Feed::Content') {
74 $orig_body = $_[0]->body;
75 if (defined $_[0]->type && defined $types{$_[0]->type}) {
76 %param = (Body => $orig_body, Type => $types{$_[0]->type});
78 if ($param{'Type'} eq "html") {
79 $param{'Body'} = HTML::Entities::encode_entities($param{'Body'});
83 $base = $_[0]->base if defined $_[0]->base;
87 if (!exists($param{Body}))
89 $param{Body} = $orig_body;
91 $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
92 # Assigning again so the type will be normalized. This seems to be
93 # an XML-Atom do-what-I-don't-meannery.
94 $entry->{entry}->content->body($orig_body);
95 $entry->{entry}->content->base($base) if defined $base;
97 my $c = $entry->{entry}->content;
99 # map Atom types to MIME types
100 my $type = $c ? $c->type : undef;
102 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
103 $type = 'text/plain' if $type eq 'text';
106 XML::Feed::Content->wrap({ type => $type,
107 base => $c ? $c->base : undef,
108 body => $c ? $c->body : undef });
114 my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
116 $entry->{entry}->add_category({ term => $_ }) for @_;
121 my @category = ($entry->{entry}->can('categories')) ? $entry->{entry}->categories : $entry->{entry}->category;
122 my @return = @category
123 ? (map { $_->label || $_->term } @category)
124 : $entry->{entry}->getlist($ns, 'subject');
126 return wantarray? @return : $return[0];
133 my $person = XML::Atom::Person->new(Version => 1.0);
134 $person->name($_[0]);
135 $entry->{entry}->author($person);
137 $entry->{entry}->author ? $entry->{entry}->author->name : undef;
141 sub id { shift->{entry}->id(@_) }
146 $entry->{entry}->issued(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
148 return iso2dt($entry->{entry}->issued)
149 if $entry->{entry}->issued;
150 return iso2dt($entry->{entry}->published)
151 if $entry->{entry}->published;
159 $entry->{entry}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
161 return iso2dt($entry->{entry}->modified) if $entry->{entry}->modified;
162 return iso2dt($entry->{entry}->updated) if $entry->{entry}->updated;
170 $entry->{entry}->lat($_[0]) if $_[0];
172 $entry->{entry}->lat;
179 $entry->{entry}->long($_[0]) if $_[0];
181 $entry->{entry}->long;
190 my $enclosure = shift;
191 my $method = ($XML::Feed::MULTIPLE_ENCLOSURES)? 'add_link' : 'link';
192 $entry->{entry}->$method({ rel => 'enclosure', href => $enclosure->{url},
193 length => $enclosure->{length},
194 type => $enclosure->{type} });
197 my @links = grep { defined $_->rel && $_->rel eq 'enclosure' } $entry->{entry}->link;
198 return unless @links;
199 my @encs = map { XML::Feed::Enclosure->new({ url => $_->href, length => $_->length, type => $_->type }) } @links ;
200 return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1];