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(@_) }
51 my $person = XML::Atom::Person->new(Version => 1.0);
53 $feed->{atom}->author($person);
55 $feed->{atom}->author ? $feed->{atom}->author->name : undef;
62 $feed->{atom}->modified(DateTime::Format::W3CDTF->format_datetime($_[0]));
64 $feed->{atom}->modified ? iso2dt($feed->{atom}->modified) : undef;
70 for my $entry ($_[0]->{atom}->entries) {
71 push @entries, XML::Feed::Entry::Atom->wrap($entry);
80 $feed->{atom}->add_entry($entry->unwrap);
83 sub as_xml { $_[0]->{atom}->as_xml }
85 package XML::Feed::Entry::Atom;
88 use base qw( XML::Feed::Entry );
89 use XML::Atom::Util qw( iso2dt );
90 use XML::Feed::Content;
92 use List::Util qw( first );
96 $entry->{entry} = XML::Atom::Entry->new(Version => 1.0);
100 sub title { shift->{entry}->title(@_) }
104 $entry->{entry}->add_link({ rel => 'alternate', href => $_[0],
105 type => 'text/html', });
107 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link;
108 $l ? $l->href : undef;
115 $entry->{entry}->summary(ref($_[0]) eq 'XML::Feed::Content' ?
116 $_[0]->body : $_[0]);
118 XML::Feed::Content->wrap({ type => 'html',
119 body => $entry->{entry}->summary });
127 if (ref($_[0]) eq 'XML::Feed::Content') {
128 %param = (Body => $_[0]->body);
130 %param = (Body => $_[0]);
132 $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
134 my $c = $entry->{entry}->content;
136 # map Atom types to MIME types
137 my $type = $c ? $c->type : undef;
139 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
140 $type = 'text/plain' if $type eq 'text';
143 XML::Feed::Content->wrap({ type => $type,
144 body => $c ? $c->body : undef });
150 my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
152 $entry->{entry}->add_category({ term => $_[0] });
154 my $category = $entry->{entry}->category;
155 $category ? ($category->label || $category->term) : $entry->{entry}->get($ns, 'subject');
162 my $person = XML::Atom::Person->new(Version => 1.0);
163 $person->name($_[0]);
164 $entry->{entry}->author($person);
166 $entry->{entry}->author ? $entry->{entry}->author->name : undef;
170 sub id { shift->{entry}->id(@_) }
175 $entry->{entry}->issued(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
177 $entry->{entry}->issued ? iso2dt($entry->{entry}->issued) : undef;
184 $entry->{entry}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
186 $entry->{entry}->modified ? iso2dt($entry->{entry}->modified) : undef;