Barney Carroll wrote:
David Dorward wrote:
To create an RSS feed? I think I'm using XML::RSS for that, although
I'm pushing ATOM as my primary feed format.
David, would you like to elaborate on this (I think it's the heart of
the matter)?
It would probably be easiest to answer this by dropping a chunk of code
from my (under development) CMS.
It is a bit on the rough and ready side. Please consider it unfinished.
(Among the things that I really need to do to it are getting the hard
coded data out of the module and into the database).
(The API for the CMS allows modules to have an output subroutine to
generate the content for the resource. Below are said routines for the
RSS and Atom modules).
sub output {
my $self = shift;
# This is a fork of (not the latest version)
# http://search.cpan.org/src/KELLAN/XML-RSS-1.05/lib/RSS.pm
# with a minor change to the entity handling code.
my $rss = new Dorward::XML::RSS (version => '1.0');
$rss->channel(
title => "David Dorward's Blog",
link => "http://blog.dorward.me.uk",
description => "David Dorward on Life, Technology, and
Everything",
syn => {
updatePeriod => "hourly",
updateFrequency => "1",
updateBase => "1901-01-01T00:00+00:00",
},
);
$rss->add_module(prefix=>"content",
uri=>"http://purl.org/rss/1.0/modules/content/");
# This is a DBIx::Class based abstraction of a database table
my @entries = SBuilder::DataSource::Blog->get_most_recent(5);
# This copies stuff from a couple of other tables, sucks the main
# content out of a file and returns hashrefs.
my @blobs = SBuilder::DataSource::Blog->entries_to_blobs(@entries);
foreach my $entry (@blobs) {
$rss->add_item(
title => $entry->{title},
link => $entry->{url},
description => $entry->{description},
content => {
encoded => $entry->{content},
},
dc => {
date => $entry->{iso_date},
}
);
}
return $rss->as_string;
}
The Atom version is pretty similar.
sub output {
my $self = shift;
use XML::Atom::Feed;
use XML::Atom::Entry;
use XML::Atom;
$XML::Atom::DefaultVersion = "1.0";
my $feed = XML::Atom::Feed->new;
$feed->title("David Dorward's Blog");
$feed->id("http://blog.dorward.me.uk/index.atom");
# http://www.w3.org/2005/Atom
my $link = XML::Atom::Link->new;
$link->type('text/html');
$link->rel('alternate');
$link->href('http://blog.dorward.me.uk/');
$feed->add_link($link);
my $selfLink = XML::Atom::Link->new;
$selfLink->type('application/atom+xml');
$selfLink->rel('self');
$selfLink->href('http://blog.dorward.me.uk/index.atom');
$feed->add_link($selfLink);
my $author = XML::Atom::Person->new;
$author->name('David Dorward');
$author->email('[EMAIL PROTECTED]');
$feed->author($author);
my @entries = SBuilder::DataSource::Blog->get_most_recent(5);
my @blobs = SBuilder::DataSource::Blog->entries_to_blobs(@entries);
my ($updateTime) = sort { $b cmp $a } map { $_->{iso_date} } @blobs;
$feed->updated($updateTime);
foreach my $blob (@blobs) {
use XML::Atom::Entry;
my $entry = XML::Atom::Entry->new;
$entry->title($blob->{title});
# Hack to fixup ... since the XML parser doesn't check the DTD
# (it could, but this is for speed, at least until I set up a
# local catalog) so it doesn't recognise this as OK in XHTML.
# Still works without it but it would make writing XHTML
# pointless if I didn't use it for its XML features.
$blob->{content} =~ s/…/…/g;
$entry->content($blob->{content});
$entry->author($author);
$entry->updated($blob->{iso_date});
$entry->id($blob->{url});
my $link = XML::Atom::Link->new;
$link->type('text/html');
$link->rel('alternate');
$link->href($blob->{url});
$entry->add_link($link);
my $dc = XML::Atom::Namespace->new(dc =>
'http://purl.org/dc/elements/1.1/');
$entry->set($dc, 'date', $blob->{iso_date});
$feed->add_entry($entry);
}
my $xml = $feed->as_xml;
return $xml;
}
--
David Dorward
*******************************************************************
List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm
Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm
Help: [EMAIL PROTECTED]
*******************************************************************