1 # $Id: Atom.pm 1958 2006-08-14 05:31:27Z btrott $
3 package XML::Feed::Atom;
6 use base qw( XML::Feed );
8 use XML::Atom::Util qw( iso2dt );
9 use List::Util qw( first );
10 use DateTime::Format::W3CDTF;
13 my ($feed, %args) = @_;
14 $args{'Version'} ||= '1.0';
16 $feed->{atom} = XML::Atom::Feed->new(%args);
24 $feed->{atom} = XML::Atom::Feed->new(Stream => $str)
25 or return $feed->error(XML::Atom::Feed->errstr);
32 sub title { shift->{atom}->title(@_) }
36 $feed->{atom}->add_link({ rel => 'alternate', href => $_[0],
37 type => 'text/html', });
39 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $feed->{atom}->link;
40 $l ? $l->href : undef;
43 sub description { shift->{atom}->tagline(@_) }
44 sub copyright { shift->{atom}->copyright(@_) }
45 sub language { shift->{atom}->language(@_) }
46 sub generator { shift->{atom}->generator(@_) }
47 sub id { shift->{atom}->id(@_) }
48 sub updated { shift->{atom}->updated(@_) }
49 sub add_link { shift->{atom}->add_link(@_) }
54 my $person = XML::Atom::Person->new(Version => 1.0);
56 $feed->{atom}->author($person);
58 $feed->{atom}->author ? $feed->{atom}->author->name : undef;
65 $feed->{atom}->modified(DateTime::Format::W3CDTF->format_datetime($_[0]));
67 return iso2dt($feed->{atom}->modified) if $feed->{atom}->modified;
68 return iso2dt($feed->{atom}->updated) if $feed->{atom}->updated;
75 for my $entry ($_[0]->{atom}->entries) {
76 push @entries, XML::Feed::Entry::Atom->wrap($entry);
85 $feed->{atom}->add_entry($entry->unwrap);
88 sub as_xml { $_[0]->{atom}->as_xml }
90 package XML::Feed::Entry::Atom;
93 use base qw( XML::Feed::Entry );
94 use XML::Atom::Util qw( iso2dt );
95 use XML::Feed::Content;
97 use List::Util qw( first );
101 $entry->{entry} = XML::Atom::Entry->new(Version => 1.0);
105 sub title { shift->{entry}->title(@_) }
106 sub source { shift->{entry}->source(@_) }
107 sub updated { shift->{entry}->updated(@_) }
112 $entry->{entry}->add_link({ rel => 'alternate', href => $_[0],
113 type => 'text/html', });
115 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link;
116 $l ? $l->href : undef;
123 $entry->{entry}->summary(ref($_[0]) eq 'XML::Feed::Content' ?
124 $_[0]->body : $_[0]);
126 XML::Feed::Content->wrap({ type => 'html',
127 body => $entry->{entry}->summary });
132 'text/xhtml' => 'xhtml',
133 'text/html' => 'html',
134 'text/plain' => 'text',
141 if (ref($_[0]) eq 'XML::Feed::Content') {
142 if (defined $_[0]->type && defined $types{$_[0]->type}) {
143 %param = (Body => $_[0]->body, Type => $types{$_[0]->type});
145 %param = (Body => $_[0]->body);
148 %param = (Body => $_[0]);
150 $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
152 my $c = $entry->{entry}->content;
154 # map Atom types to MIME types
155 my $type = $c ? $c->type : undef;
157 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
158 $type = 'text/plain' if $type eq 'text';
161 XML::Feed::Content->wrap({ type => $type,
162 body => $c ? $c->body : undef });
168 my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
170 $entry->{entry}->add_category({ term => $_[0] });
172 my $category = $entry->{entry}->category;
173 my @return = $category ? ($category->label || $category->term) : $entry->{entry}->getlist($ns, 'subject');
174 return wantarray? @return : $return[0];
181 my $person = XML::Atom::Person->new(Version => 1.0);
182 $person->name($_[0]);
183 $entry->{entry}->author($person);
185 $entry->{entry}->author ? $entry->{entry}->author->name : undef;
189 sub id { shift->{entry}->id(@_) }
194 $entry->{entry}->issued(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
196 $entry->{entry}->issued ? iso2dt($entry->{entry}->issued) : undef;
203 $entry->{entry}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
205 return iso2dt($entry->{entry}->modified) if $entry->{entry}->modified;
206 return iso2dt($entry->{entry}->updated) if $entry->{entry}->updated;