X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-Catalyst-Plugin-Sitemap;a=blobdiff_plain;f=lib%2FCatalyst%2FPlugin%2FSitemap.pm;h=e408855fc55dec89b45772b8a170bbfa32a9d8c4;hp=c88be0f8a8754bbd59fc1ad47fecb42701467cff;hb=HEAD;hpb=0c20c738b4de2a58b62ca68a6ffa67c5dfb35186 diff --git a/lib/Catalyst/Plugin/Sitemap.pm b/lib/Catalyst/Plugin/Sitemap.pm index c88be0f..e408855 100644 --- a/lib/Catalyst/Plugin/Sitemap.pm +++ b/lib/Catalyst/Plugin/Sitemap.pm @@ -1,6 +1,102 @@ package Catalyst::Plugin::Sitemap; +BEGIN { + $Catalyst::Plugin::Sitemap::VERSION = '1.0.0'; +} + # ABSTRACT: Sitemap support for Catalyst. + +use strict; +use warnings; + +use Moose::Role; + +no warnings qw/uninitialized/; + +use WWW::Sitemap::XML; +use List::Util qw/ first /; + +has sitemap => ( + is => 'ro', + builder => '_build_sitemap', + lazy => 1, +); + +sub sitemap_as_xml { + my $self = shift; + return $self->sitemap->as_xml->toString; +} + +sub _build_sitemap { + my $self = shift; + + my $sitemap = WWW::Sitemap::XML->new; + + if ( $self->can('uri_disposition') ) { + $self->uri_disposition('absolute'); + } + + for my $controller ( map { $self->controller($_) } $self->controllers ) { + 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); + } + + } + + return $sitemap; +} + +1; + + + +=pod + +=head1 NAME + +Catalyst::Plugin::Sitemap - Sitemap support for Catalyst. + +=head1 VERSION + +version 1.0.0 + =head1 SYNOPSIS # in MyApp.pm @@ -35,7 +131,7 @@ package Catalyst::Plugin::Sitemap; sub sitemap : Path('/sitemap') { my ( $self, $c ) = @_; - $c->res->body( $c->sitemap->as_xml->sprint ); + $c->res->body( $c->sitemap_as_xml ); } =head1 DESCRIPTION @@ -47,10 +143,14 @@ of the sitemap of a Catalyst application. =head2 sitemap() -Returns a L object. The sitemap object is populated by +Returns a L object. The sitemap object is populated by inspecting the controllers of the application for actions with the sub attribute C<:Sitemap>. +=head2 sitemap_as_xml() + +Returns the sitemap as a string containing its XML representation. + =head1 C<:Sitemap> Subroutine Attribute The sitemap is populated by actions ear-marked with the <:Sitemap> sub @@ -71,7 +171,6 @@ resolves in a single url, this will results in an error. =item C<:Sitemap($priority)> - sub with_priority :Local :Sitemap(0.9) { ... } @@ -90,7 +189,7 @@ resolves in a single url, this will results in an error. } Adds the url with the given entry attributes (as defined by -L). +L). If the action does not resolves in a single url, this will results in an error. @@ -113,90 +212,41 @@ resolving to many urls. =back - -=head1 BUGS AND LIMITATIONS - -Currently, each invocation of the method C will -re-generate the C object. A future version -of this module might offer a way to only compute the sitemap -once. - - =head1 SEE ALSO -=over +=over =item L -=item http://babyl.dyndns.org/techblog/entry/catalyst-plugin-sitemap +Module that C currently uses under the hood. -=back +=item L -=cut +Original module that this plugin was using under the hood. -use strict; -use warnings; - -use Moose::Role; - -no warnings qw/uninitialized/; - -use WWW::Sitemap::XML; -use List::Util qw/ first /; +=item L -sub sitemap { - my $self = shift; - - my $sitemap = WWW::Sitemap::XML->new; - - for my $controller ( $self->controller(qr//) ) { - ACTION: - for my $a ( $controller->get_action_methods ) { - - my $action = $controller->action_for( $a->name ); +Similar plugin for the L framework, which inspired +C. - 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"; +=item http://babyl.dyndns.org/techblog/entry/catalyst-plugin-sitemap - if ( $controller->can($sitemap_method) ) { - $controller->$sitemap_method( $self, $sitemap ); - next ACTION; - } - } +Blog article introducing C. - if ( $attr[0] + 0 > 0 ) { - # it's a number - $uri_params{priority} = $attr[0]; - } +=back - } - elsif ( @attr > 0 ) { - %uri_params = @attr; - } +=head1 AUTHOR - $uri_params{loc} = $self->uri_for_action( $action->private_path ); +Yanick Champoux - $sitemap->add( \%uri_params ); +=head1 COPYRIGHT AND LICENSE - next ACTION; - } +This software is copyright (c) 2010 by Yanick Champoux. - } +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. - return $sitemap; -} +=cut -1; __END__