Prevent empty content
[catagits/XML-Feed.git] / lib / XML / Feed / Entry.pm
1 # $Id: Entry.pm 1872 2005-08-12 04:28:42Z btrott $
2
3 package XML::Feed::Entry;
4 use strict;
5 use base qw( Class::ErrorHandler );
6
7 use Scalar::Util qw( blessed );
8
9 use Carp;
10
11 sub wrap {
12     my $class = shift;
13     my($item) = @_;
14     bless { entry => $item }, $class;
15 }
16
17 sub unwrap { $_[0]->{entry} }
18
19 sub new {
20     my $class = shift;
21     my($format) = @_;
22     $format ||= 'Atom';
23     my $format_class = 'XML::Feed::' . $format;
24     eval "use $format_class";
25     Carp::croak("Unsupported format $format: $@") if $@;
26     my $entry = bless {}, join('::', __PACKAGE__, $format);
27     $entry->init_empty or return $class->error($entry->errstr);
28     $entry;
29 }
30
31 sub init_empty { 1 }
32
33 sub convert {
34     my $entry = shift;
35     my($format) = @_;
36     my $new = __PACKAGE__->new($format);
37     for my $field (qw( title link content summary category author id issued modified )) {
38         my $val = $entry->$field();
39         next unless defined $val;
40         next if blessed $val && $val->isa('XML::Feed::Content') && ! defined $val->body;
41         $new->$field($val);
42     }
43     $new;
44 }
45
46 sub title;
47 sub link;
48 sub content;
49 sub summary;
50 sub category;
51 sub author;
52 sub id;
53 sub issued;
54 sub modified;
55
56 1;
57 __END__
58
59 =head1 NAME
60
61 XML::Feed::Entry - Entry/item in a syndication feed
62
63 =head1 SYNOPSIS
64
65     ## $feed is an XML::Feed object.
66     for my $entry ($feed->entries) {
67         print $entry->title, "\n", $entry->summary, "\n\n";
68     }
69
70 =head1 DESCRIPTION
71
72 I<XML::Feed::Entry> represents an entry/item in an I<XML::Feed> syndication
73 feed.
74
75 =head1 USAGE
76
77 =head2 XML::Feed::Entry->new($format)
78
79 Creates a new I<XML::Feed::Entry> object in the format I<$format>, which
80 should be either I<RSS> or I<Atom>.
81
82 =head2 $entry->convert($format)
83
84 Converts the I<XML::Feed::Entry> object into the I<$format> format, and
85 returns the new object.
86
87 =head2 $entry->title([ $title ])
88
89 The title of the entry.
90
91 =head2 $entry->link([ $uri ])
92
93 The permalink of the entry, in most cases, except in cases where it points
94 instead to an offsite URI referenced in the entry.
95
96 =head2 $entry->content([ $content ])
97
98 Bn I<XML::Feed::Content> object representing the full entry body, or as
99 much as is available in the feed.
100
101 In RSS feeds, this method will look first for
102 I<http://purl.org/rss/1.0/modules/content/#encoded> and
103 I<http://www.w3.org/1999/xhtml#body> elements, then fall back to a
104 I<E<lt>descriptionE<gt>> element.
105
106 =head2 $entry->summary([ $summary ])
107
108 An I<XML::Feed::Content> object representing a short summary of the entry.
109 Possibly.
110
111 Since RSS feeds do not have the idea of a summary separate from the entry
112 body, this may not always be what you want. If the entry contains both a
113 I<E<lt>descriptionE<gt>> element B<and> another element typically used for
114 the full content of the entry--either I<http://www.w3.org/1999/xhtml/body>
115 or I<http://purl.org/rss/1.0/modules/content/#encoded>--we treat that as
116 the summary. Otherwise, we assume that there isn't a summary, and return
117 an I<XML::Feed::Content> object with an empty string in the I<body>.
118
119 =head2 $entry->category([ $category ])
120
121 The category in which the entry was posted.
122
123 =head2 $entry->author([ $author ])
124
125 The name or email address of the person who posted the entry.
126
127 =head2 $entry->id([ $id ])
128
129 The unique ID of the entry.
130
131 =head2 $entry->issued([ $issued ])
132
133 A I<DateTime> object representing the date and time at which the entry
134 was posted.
135
136 If present, I<$issued> should be a I<DateTime> object.
137
138 =head2 $entry->modified([ $modified ])
139
140 A I<DateTime> object representing the last-modified date of the entry.
141
142 If present, I<$modified> should be a I<DateTime> object.
143
144 =head1 AUTHOR & COPYRIGHT
145
146 Please see the I<XML::Feed> manpage for author, copyright, and license
147 information.
148
149 =cut