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;
14 $feed->{atom} = XML::Atom::Feed->new(Version => 1.0);
22 $feed->{atom} = XML::Atom::Feed->new(Stream => $str)
23 or return $feed->error(XML::Atom::Feed->errstr);
30 sub title { shift->{atom}->title(@_) }
34 $feed->{atom}->add_link({ rel => 'alternate', href => $_[0],
35 type => 'text/html', });
37 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $feed->{atom}->link;
38 $l ? $l->href : undef;
41 sub description { shift->{atom}->tagline(@_) }
42 sub copyright { shift->{atom}->copyright(@_) }
43 sub language { shift->{atom}->language(@_) }
44 sub generator { shift->{atom}->generator(@_) }
49 my $person = XML::Atom::Person->new(Version => 1.0);
51 $feed->{atom}->author($person);
53 $feed->{atom}->author ? $feed->{atom}->author->name : undef;
60 $feed->{atom}->modified(DateTime::Format::W3CDTF->format_datetime($_[0]));
62 $feed->{atom}->modified ? iso2dt($feed->{atom}->modified) : undef;
68 for my $entry ($_[0]->{atom}->entries) {
69 push @entries, XML::Feed::Entry::Atom->wrap($entry);
78 $feed->{atom}->add_entry($entry->unwrap);
81 sub as_xml { $_[0]->{atom}->as_xml }
83 package XML::Feed::Entry::Atom;
86 use base qw( XML::Feed::Entry );
87 use XML::Atom::Util qw( iso2dt );
88 use XML::Feed::Content;
90 use List::Util qw( first );
94 $entry->{entry} = XML::Atom::Entry->new(Version => 1.0);
98 sub title { shift->{entry}->title(@_) }
102 $entry->{entry}->add_link({ rel => 'alternate', href => $_[0],
103 type => 'text/html', });
105 my $l = first { !defined $_->rel || $_->rel eq 'alternate' } $entry->{entry}->link;
106 $l ? $l->href : undef;
113 $entry->{entry}->summary(ref($_[0]) eq 'XML::Feed::Content' ?
114 $_[0]->body : $_[0]);
116 XML::Feed::Content->wrap({ type => 'html',
117 body => $entry->{entry}->summary });
125 if (ref($_[0]) eq 'XML::Feed::Content') {
126 %param = (Body => $_[0]->body);
128 %param = (Body => $_[0]);
130 $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
132 my $c = $entry->{entry}->content;
134 # map Atom types to MIME types
135 my $type = $c ? $c->type : undef;
137 $type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
138 $type = 'text/plain' if $type eq 'text';
141 XML::Feed::Content->wrap({ type => $type,
142 body => $c ? $c->body : undef });
148 my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
150 $entry->{entry}->add_category({ term => $_[0] });
152 my $category = $entry->{entry}->category;
153 $category ? ($category->label || $category->term) : $entry->{entry}->get($ns, 'subject');
160 my $person = XML::Atom::Person->new(Version => 1.0);
161 $person->name($_[0]);
162 $entry->{entry}->author($person);
164 $entry->{entry}->author ? $entry->{entry}->author->name : undef;
168 sub id { shift->{entry}->id(@_) }
173 $entry->{entry}->issued(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
175 $entry->{entry}->issued ? iso2dt($entry->{entry}->issued) : undef;
182 $entry->{entry}->modified(DateTime::Format::W3CDTF->format_datetime($_[0])) if $_[0];
184 $entry->{entry}->modified ? iso2dt($entry->{entry}->modified) : undef;