Mangle RSS and Atom feeds
Tomas Doran [Tue, 26 Jan 2010 01:34:58 +0000 (01:34 +0000)]
Thus doing away with XML::RSS and XML::Atom

ISSUES
Makefile.PL
lib/Gitalist/Controller/Repository.pm
root/repository/atom.tt2 [new file with mode: 0644]
root/repository/rss.tt2 [new file with mode: 0644]
t/atom.t
t/rss.t

diff --git a/ISSUES b/ISSUES
index 448f798..d0b2d00 100644 (file)
--- a/ISSUES
+++ b/ISSUES
@@ -1,6 +1,7 @@
 * Root /search page doesn't do anything
 * Atom feed sucks shit (see t/atom.t)
-* OPML Feed dates incorrect format
+* OPML & RSS & Atom Feed dates incorrect format
+* Feeds don't do XML entity escaping on the crap they print.
 * Sort out commitdiff so conflicted merges have an equivalent display to gitweb (d7f39bebabeb31ce9a7b4f1b6f3db9f391b78c3e as a reference)
 * Need to sanitise the input ...
 * The snapshot action does not properly support tgz - requests for this format will receive a tar.  tbz2 is not supported at all (yet).
index 6c86077..5c29c76 100644 (file)
@@ -91,8 +91,6 @@ requires 'Path::Class' => '0.17';
 requires 'Sub::Exporter';
 requires 'Syntax::Highlight::Engine::Kate';
 requires 'Sys::Hostname';
-requires 'XML::Atom';
-requires 'XML::RSS';
 
 test_requires 'Test::More' => '0.88';
 test_requires 'Test::utf8' => '0.02';
index c80f177..fd27563 100644 (file)
@@ -63,32 +63,35 @@ Provides an atom feed for a given repository.
 =cut
 
 sub atom : Chained('find') Args(0) {
-  my($self, $c) = @_;
-
-  my $feed = XML::Atom::Feed->new;
-
-  my $host = lc hostname();
-  $feed->title($host . ' - ' . Gitalist->config->{name});
-  $feed->updated(~~DateTime->now);
+    my ($self, $c) = @_;
 
-  my $repository = $c->stash->{Repository};
-  my %logargs = (
-      sha1   => $repository->head_hash,
-      count  => Gitalist->config->{paging}{log} || 25,
-  );
+    my $host = lc hostname();
+    $c->stash(
+        title => $host . ' - ' . Gitalist->config->{name},
+        updated => DateTime->now
+    );
 
-  my $mk_title = $c->stash->{short_cmt};
-  for my $commit ($repository->list_revs(%logargs)) {
-    my $entry = XML::Atom::Entry->new;
-    $entry->title( $mk_title->($commit->comment) );
-    $entry->id($c->uri_for_action('/ref/commit', [$repository->name, $commit->sha1]));
-    # XXX FIXME Needs work ...
-    $entry->content($commit->comment);
-    $feed->add_entry($entry);
-  }
+    my $repository = $c->stash->{Repository};
+    my %logargs = (
+        sha1   => $repository->head_hash,
+        count  => Gitalist->config->{paging}{log} || 25,
+    );
 
-  $c->response->body($feed->as_xml);
-  $c->response->content_type('application/atom+xml');
+    my @revs;
+    my $mk_title = $c->stash->{short_cmt};
+    for my $commit ($repository->list_revs(%logargs)) {
+        my $entry = {};
+        $entry->{title} = $mk_title->($commit->comment);
+        $entry->{id} = $c->uri_for_action('/ref/commit', [$repository->name, $commit->sha1]);
+        # XXX FIXME Needs work ...
+        $entry->{content} = $commit->comment;
+        push(@revs, $entry);
+    }
+    $c->stash(
+        Commits => \@revs,
+        no_wrapper => 1,
+    );
+    $c->response->content_type('application/atom+xml');
 }
 
 =head2 rss
@@ -102,14 +105,12 @@ sub rss : Chained('find') Args(0) {
 
   my $repository = $c->stash->{Repository};
 
-  my $rss = XML::RSS->new(version => '2.0');
-  $rss->channel(
+  $c->stash(
     title          => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
-    link           => $c->uri_for_action('/repository/summary', [$repository->name]),
     language       => 'en',
-    description    => $repository->description,
     pubDate        => DateTime->now,
     lastBuildDate  => DateTime->now,
+    no_wrapper     => 1,
   );
 
   my %logargs = (
@@ -117,17 +118,17 @@ sub rss : Chained('find') Args(0) {
       count  => Gitalist->config->{paging}{log} || 25,
 #      ($c->req->param('f') ? (file => $c->req->param('f')) : ())
   );
+  my @revs;
   my $mk_title = $c->stash->{short_cmt};
   for my $commit ($repository->list_revs(%logargs)) {
     # XXX FIXME Needs work ....
-    $rss->add_item(
+    push(@revs, {
         title       => $mk_title->($commit->comment),
         permaLink   => $c->uri_for_action('/ref/commit', [$repository->name, $commit->sha1]),
         description => $commit->comment,
-    );
+    });
   }
-
-  $c->response->body($rss->as_string);
+  $c->stash(Commits => \@revs);
   $c->response->content_type('application/rss+xml');
 }
 
diff --git a/root/repository/atom.tt2 b/root/repository/atom.tt2
new file mode 100644 (file)
index 0000000..1d52779
--- /dev/null
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://purl.org/atom/ns#">
+  <title>[% title %]</title>
+  <modified>[% updated %]</modified>
+  [% FOREACH Commit = Commits %]
+  <entry>
+    <title>[% Commit.title %]</title>
+    <id>[% Commit.id %]</id>
+    <content mode="xml">
+      <div xmlns="http://www.w3.org/1999/xhtml">[% Commit.content %]</div>
+    </content>
+  </entry>
+  [% END %]
+</feed>
diff --git a/root/repository/rss.tt2 b/root/repository/rss.tt2
new file mode 100644 (file)
index 0000000..d1a47cc
--- /dev/null
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">
+
+<channel>
+<title>[% title %]</title>
+<link>[% c.uri_for_action('/repository/summary', [Repository.name]) %]</link>
+<description>[% Repository.description %]</description>
+<language>[% lang %]</language>
+<pubDate>[% pubDate %]</pubDate>
+<lastBuildDate>[% lastBuildDate %]</lastBuildDate>
+[% FOREACH Commit = Commits %]
+<item>
+<title>[% Commit.title %]</title>
+<description>[% Commit.description %]</description>
+<guid isPermaLink="true">[% Commit.permaLink %]</guid>
+</item>
+[% END %]
+</channel>
+</rss>
index 726a9c5..13c835f 100644 (file)
--- a/t/atom.t
+++ b/t/atom.t
@@ -5,7 +5,7 @@ use TestGitalist;
 
 my $res = request('/repo1/atom');
 ok $res->is_success;
-
+is $res->content_type, 'application/atom+xml';
 TODO: {
     local $TODO = "Does not work yet. Need similar info to RSS feed";
     like $res->content, qr{link>http://localhost/repo1</link};
diff --git a/t/rss.t b/t/rss.t
index 47f4c8d..2d6a143 100644 (file)
--- a/t/rss.t
+++ b/t/rss.t
@@ -5,7 +5,7 @@ use TestGitalist;
 
 my $res = request('/repo1/rss');
 ok $res->is_success;
-
+is $res->content_type, 'application/rss+xml';
 like $res->content, qr{link>http://localhost/repo1</link};
 like $res->content, qr{description>some test repository</description};
 like $res->content, qr{title>add dir1/file2</title};