/[pub]/suikawiki/script/lib/Yuki/RSS.pm
Suika

Contents of /suikawiki/script/lib/Yuki/RSS.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Sat May 10 02:45:27 2003 UTC (22 years, 8 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +1 -1 lines
FILE REMOVED
*** empty log message ***

1 package Yuki::RSS;
2 use strict;
3 use vars qw($VERSION);
4
5 $VERSION = '0.2';
6
7 =head1 NAME
8
9 Yuki::RSS - The smallest module to generate RSS 1.0.
10 It is downward compatible to XML::RSS.
11
12 =head1 SYNOPSIS
13
14 use strict;
15 use Yuki::RSS;
16
17 my $rss = new Yuki::RSS(
18 version => '1.0',
19 encoding => 'Shift_JIS'
20 );
21
22 $rss->channel(
23 title => "Site Title",
24 link => "http://url.of.your.site/",
25 description => "The description of your site",
26 );
27
28 $rss->add_item(
29 title => "Item Title",
30 link => "http://url.of.your/item.html",
31 description => "Yoo, hoo, hoo",
32 );
33
34 print $rss->as_string;
35
36 =head1 DESCRIPTION
37
38 Yuki::RSS is the smallest RSS 1.0 generator.
39 This module helps you to create the minimum document of RSS 1.0.
40 If you need more than that, use XML::RSS.
41
42 =head1 METHODS
43
44 =over 4
45
46 =item new Yuki::RSS (version => $version, encoding => $encoding)
47
48 Constructor for Yuki::RSS.
49 It returns a reference to a Yuki::RSS object.
50 B<version> must be 1.0.
51 B<encoding> will be inserted output document as a XML encoding.
52 This module does not convert to this encoding.
53
54 =item add_item (title => $title, link => $link, description => $description)
55
56 Adds an item to the Yuki::RSS object.
57
58 =item as_string
59
60 Returns the RSS string.
61
62 =item channel (title => $title, link => $link, description => $desc)
63
64 Channel information of RSS.
65
66 =head1 SEE ALSO
67
68 =over 4
69
70 =item L<XML::RSS>
71
72 =back
73
74 =head1 AUTHOR
75
76 Hiroshi Yuki <hyuki@hyuki.com> http://www.hyuki.com/
77
78 =head1 LICENSE
79
80 Copyright (C) 2001 by Hiroshi Yuki.
81
82 This program is free software; you can redistribute it and/or
83 modify it under the same terms as Perl itself.
84
85 =cut
86
87 # The constructor.
88 sub new {
89 my ($class, %hash) = @_;
90 my $self = {
91 version => $hash{version},
92 encoding => $hash{encoding},
93 channel => { },
94 items => [],
95 };
96 return bless $self, $class;
97 }
98
99 # Setting channel.
100 sub channel {
101 my ($self, %hash) = @_;
102 foreach (keys %hash) {
103 $self->{channel}->{$_} = $hash{$_};
104 }
105 return $self->{channel};
106 }
107
108 # Adding item.
109 sub add_item {
110 my ($self, %hash) = @_;
111 push(@{$self->{items}}, \%hash);
112 return $self->{items};
113 }
114
115 sub stylesheet ($%) {
116 my $self = shift;
117 $self->{stylesheet} = {@_};
118 }
119
120 #
121 sub as_string {
122 my ($self) = @_;
123 unless ($self->{channel}->{'dc:date'}) {
124 $self->{channel}->{'dc:date'} = _rfc3339_date (time);
125 } else {
126 $self->{channel}->{'dc:date'} = _rfc3339_date ($self->{channel}->{'dc:date'});
127 }
128 my $doc = '';
129 $doc = qq(<?xml version="1.0"@{[$self->{encoding}?qq( encoding="$self->{encoding}"):'']}?>)
130 if $self->{channel}->{xml_declare};
131 $doc .= <<"EOD";
132 @{[ ref ($self->{stylesheet}) ?
133 '<?xml-stylesheet '.(join (' ', map {qq($_="@{[&_escape($self->{stylesheet}->{$_})]}")} keys %{$self->{stylesheet}})).'?>':''
134 ]}
135 <rdf:RDF
136 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
137 xmlns="http://purl.org/rss/1.0/"
138 xmlns:dc="http://purl.org/dc/elements/1.1/"
139 >
140
141 <channel rdf:about="@{[&_escape($self->{channel}->{link})]}">
142 <title>@{[&_escape($self->{channel}->{title})]}</title>
143 <link>@{[&_escape($self->{channel}->{link})]}</link>
144 <description>@{[&_escape($self->{channel}->{description})]}</description>
145 <dc:language>@{[&_escape($self->{channel}->{'dc:language'})]}</dc:language>
146 <dc:date>$self->{channel}->{'dc:date'}</dc:date>
147 <items>
148 <rdf:Seq>
149 @{[
150 map {
151 qq{<rdf:li rdf:resource="@{[&_escape($_->{link})]}" />}
152 } @{$self->{items}}
153 ]}
154 </rdf:Seq>
155 </items>
156 </channel>
157 @{[
158 map { do {
159 my $r = qq{<item rdf:about="@{[&_escape($_->{link})]}">\n};
160 for my $element (keys %{$_}) {
161 my $c = $_->{$element};
162 $c = _rfc3339_date ($c) if $element eq 'dc:date';
163 $r .= qq{ <$element>@{[&_escape($c)]}</$element>\n};
164 }
165 $r . "</item>\n";
166 }} @{$self->{items}}
167 ]}
168 </rdf:RDF>
169 EOD
170 }
171
172 sub _escape {
173 my $s = shift;
174 $s =~ s|&|&amp;|g;
175 $s =~ s|<|&lt;|g;
176 $s =~ s|>|&gt;|g;
177 $s =~ s|"|&quot;|g;
178 return $s;
179 }
180
181 sub _rfc3339_date ($) {
182 my @time = gmtime (shift);
183 sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
184 }
185
186 1; # $Date: 2003/03/27 04:15:04 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24