-# $Id: Atom.pm 1958 2006-08-14 05:31:27Z btrott $
+# $Id$
package XML::Feed::Format::Atom;
use strict;
use XML::Atom::Util qw( iso2dt );
use List::Util qw( first );
use DateTime::Format::W3CDTF;
+use HTML::Entities;
use XML::Atom::Entry;
XML::Atom::Entry->mk_elem_accessors(qw( lat long ), ['http://www.w3.org/2003/01/geo/wgs84_pos#']);
sub summary {
my $entry = shift;
if (@_) {
- my %param;
- if (ref($_[0]) eq 'XML::Feed::Content') {
- %param = (Body => $_[0]->body);
- } else {
- %param = (Body => $_[0]);
- }
- $entry->{entry}->summary(XML::Atom::Content->new(%param, Version => 1.0));
+ my %param;
+ if (ref($_[0]) eq 'XML::Feed::Content') {
+ %param = (Body => $_[0]->body);
+ } else {
+ %param = (Body => $_[0]);
+ }
+ $entry->{entry}->summary(XML::Atom::Content->new(%param, Version => 1.0));
} else {
- my $s = $entry->{entry}->summary;
+ my $s = $entry->{entry}->summary;
# map Atom types to MIME types
my $type = ($s && ref($s) eq 'XML::Feed::Content') ? $s->type : undef;
if ($type) {
$type = 'text/html' if $type eq 'xhtml' || $type eq 'html';
$type = 'text/plain' if $type eq 'text';
}
- my $body = $s;
- if (defined $s && ref($s) eq 'XML::Feed::Content') {
- $body = $s->body;
- }
+ my $body = $s;
+ if (defined $s && ref($s) eq 'XML::Feed::Content') {
+ $body = $s->body;
+ }
XML::Feed::Content->wrap({ type => $type,
body => $body });
}
}
my %types = (
- 'text/xhtml' => 'xhtml',
- 'text/html' => 'html',
- 'text/plain' => 'text',
+ 'text/xhtml' => 'xhtml',
+ 'text/html' => 'html',
+ 'text/plain' => 'text',
);
sub content {
if (@_) {
my %param;
my $base;
+ my $orig_body;
if (ref($_[0]) eq 'XML::Feed::Content') {
- if (defined $_[0]->type && defined $types{$_[0]->type}) {
- %param = (Body => $_[0]->body, Type => $types{$_[0]->type});
- } else {
- %param = (Body => $_[0]->body);
- }
+ $orig_body = $_[0]->body;
+ if (defined $_[0]->type && defined $types{$_[0]->type}) {
+ %param = (Body => $orig_body, Type => $types{$_[0]->type});
+
+ if ($param{'Type'} eq "html") {
+ $param{'Body'} = HTML::Entities::encode_entities($param{'Body'});
+ }
+ } else {
+ }
$base = $_[0]->base if defined $_[0]->base;
} else {
- %param = (Body => $_[0]);
+ $orig_body = $_[0];
+ }
+ if (!exists($param{Body}))
+ {
+ $param{Body} = $orig_body;
}
$entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
+ # Assigning again so the type will be normalized. This seems to be
+ # an XML-Atom do-what-I-don't-meannery.
+ $entry->{entry}->content->body($orig_body);
$entry->{entry}->content->base($base) if defined $base;
} else {
my $c = $entry->{entry}->content;
my $entry = shift;
my $ns = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
if (@_) {
- $entry->{entry}->add_category({ term => $_[0] });
+ $entry->{entry}->add_category({ term => $_ }) for @_;
+ return 1
} else {
- my $category = $entry->{entry}->category;
- my @return = $category ? ($category->label || $category->term) : $entry->{entry}->getlist($ns, 'subject');
+
+
+ my @category = ($entry->{entry}->can('categories')) ? $entry->{entry}->categories : $entry->{entry}->category;
+ my @return = @category
+ ? (map { $_->label || $_->term } @category)
+ : $entry->{entry}->getlist($ns, 'subject');
+
return wantarray? @return : $return[0];
}
}
}
-sub add_enclosure {
- my($entry, $enclosure) = @_;
- my $link = XML::Atom::Link->new;
- $link->rel('enclosure');
- $link->type($enclosure->type);
- $link->href($enclosure->url);
- $link->length($enclosure->length);
- $entry->{entry}->add_link($link);
-};
+sub enclosure {
+ my $entry = shift;
+
+ if (@_) {
+ my $enclosure = shift;
+ my $method = ($XML::Feed::MULTIPLE_ENCLOSURES)? 'add_link' : 'link';
+ $entry->{entry}->$method({ rel => 'enclosure', href => $enclosure->{url},
+ length => $enclosure->{length},
+ type => $enclosure->{type} });
+ return 1;
+ } else {
+ my @links = grep { defined $_->rel && $_->rel eq 'enclosure' } $entry->{entry}->link;
+ return unless @links;
+ my @encs = map { XML::Feed::Enclosure->new({ url => $_->href, length => $_->length, type => $_->type }) } @links ;
+ return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1];
+ }
+}
1;