]> Dogcows Code - chaz/p5-Catalyst-Plugin-Sitemap/commitdiff
initial commit
authorYanick Champoux <yanick@babyl.dyndns.org>
Tue, 28 Sep 2010 00:15:02 +0000 (20:15 -0400)
committerYanick Champoux <yanick@babyl.dyndns.org>
Tue, 28 Sep 2010 00:15:02 +0000 (20:15 -0400)
dist.ini [new file with mode: 0644]
lib/Catalyst/Plugin/Sitemap.pm [new file with mode: 0644]
t/lib/TestApp.pm [new file with mode: 0644]
t/lib/TestApp/Controller/Root.pm [new file with mode: 0644]
t/sitemap.t [new file with mode: 0644]

diff --git a/dist.ini b/dist.ini
new file mode 100644 (file)
index 0000000..acbdfe9
--- /dev/null
+++ b/dist.ini
@@ -0,0 +1,6 @@
+name    = Catalyst-Plugin-Sitemap
+author  = Yanick Champoux <yanick@babyl.dyndns.org>
+license = Perl_5
+copyright_holder = Yanick Champoux
+copyright_year   = 2010
+
diff --git a/lib/Catalyst/Plugin/Sitemap.pm b/lib/Catalyst/Plugin/Sitemap.pm
new file mode 100644 (file)
index 0000000..eb6e92b
--- /dev/null
@@ -0,0 +1,89 @@
+package Catalyst::Plugin::Sitemap;
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+no warnings qw/uninitialized/;
+
+use Search::Sitemap;
+use List::Util qw/ first /;
+
+sub sitemap {
+    my $self = shift;
+
+    my $sitemap = Search::Sitemap->new;
+    $sitemap->pretty(1);
+
+    for my $controller ( $self->controller(qr//) ) {
+      ACTION:
+        for my $a ( $controller->get_action_methods ) {
+
+            my $action = $controller->action_for( $a->name );
+
+            my $attr = $action->attributes->{Sitemap} or next ACTION;
+
+            die "more than one attribute 'Sitemap' for sub ",
+              $a->fully_qualified_name
+              if @$attr > 1;
+
+            my @attr = split /\s*(?:,|=>)\s*/, $attr->[0];
+
+            my %uri_params;
+
+            if ( @attr == 1 ) {
+                if ( $attr[0] eq '*' ) {
+                    my $sitemap_method = $action->name . "_sitemap";
+
+                    if ( $controller->can($sitemap_method) ) {
+                        $controller->$sitemap_method( $self, $sitemap );
+                        next ACTION;
+                    }
+                }
+
+                if ( $attr[0] + 0 > 0 ) {
+                    # it's a number
+                    $uri_params{priority} = $attr[0];
+                }
+
+            }
+            elsif ( @attr > 0 ) {
+                %uri_params = @attr;
+            }
+
+            $uri_params{loc} = $self->uri_for_action( $action->private_path );
+
+            $sitemap->add( \%uri_params );
+
+            next ACTION;
+        }
+
+    }
+
+    return $sitemap->xml;
+}
+
+1;
+
+__END__
+
+ways I can do it
+
+is Sitemap( <yadah> ) ? => use it as is
+else => has <action>_sitemap? => use it
+else => use the uri directly
+
+
+sitemap => sub {
+    my ( $self, $c, $sitemap ) = @_;
+
+    ...
+};
+
+sub action_sitemap {
+    my ( $self, $c, $sitemap ) = @_;
+}
+
+sub do_stuff :Local :Sitemap {
+}
diff --git a/t/lib/TestApp.pm b/t/lib/TestApp.pm
new file mode 100644 (file)
index 0000000..5350115
--- /dev/null
@@ -0,0 +1,10 @@
+package TestApp;
+
+use strict;
+use warnings;
+
+use Catalyst qw/ Sitemap /;
+
+__PACKAGE__->setup;
+
+1;
diff --git a/t/lib/TestApp/Controller/Root.pm b/t/lib/TestApp/Controller/Root.pm
new file mode 100644 (file)
index 0000000..01a131d
--- /dev/null
@@ -0,0 +1,35 @@
+package TestApp::Controller::Root;
+
+use strict;
+use warnings;
+
+use parent 'Catalyst::Controller';
+
+sub sitemap : Path('/sitemap') {
+    my ( $self, $c ) = @_;
+
+    $c->res->body( $c->sitemap );
+}
+
+sub alone :Path('/alone') :Sitemap { }
+
+sub with_priority :Path('/with_priority') :Sitemap(0.75) { }
+
+sub with_function :Path('/with_function') :Sitemap(*) { }
+
+sub with_function_sitemap {
+    $_[2]->add( 'http://localhost/with_function' );
+}
+
+sub with_args :Path('/with_args') 
+    :Sitemap( lastmod => 2010-09-27, changefreq => daily ) 
+    {}
+
+
+1;
+
+
+
+
+
+
diff --git a/t/sitemap.t b/t/sitemap.t
new file mode 100644 (file)
index 0000000..26f91ab
--- /dev/null
@@ -0,0 +1,25 @@
+
+use strict;
+use warnings;
+
+use Test::More tests => 4;    # last test to print
+
+use lib 't/lib';
+
+use Catalyst::Test 'TestApp';
+
+my $xml = request('/sitemap')->content;
+
+$xml =~ s/\s+//g;
+
+like $xml, qr{<url><loc>http://localhost/alone</loc></url>}, ':Sitemap';
+like $xml, qr{<url><loc>http://localhost/with_function</loc></url>},
+  ':Sitemap(*)';
+like $xml,
+  qr{<url><loc>http://localhost/with_priority</loc><priority>0.75</priority></url>},
+  ':Sitemap(0.75)';
+
+like $xml,
+  qr{<url><loc>http://localhost/with_args</loc><lastmod>2010-09-27</lastmod><changefreq>daily</changefreq></url>},
+  ':Sitemap(lotsa stuff)';
+
This page took 0.025684 seconds and 4 git commands to generate.