]> Dogcows Code - chaz/p5-Catalyst-Plugin-Sitemap/blob - lib/Catalyst/Plugin/Sitemap.pm
preparing for a first release
[chaz/p5-Catalyst-Plugin-Sitemap] / lib / Catalyst / Plugin / Sitemap.pm
1 package Catalyst::Plugin::Sitemap;
2 # ABSTRACT: Sitemap support for Catalyst.
3
4 =head1 SYNOPSIS
5
6 # in MyApp.pm
7
8 use Catalyst qw/ Sitemap /;
9
10 # in the controller
11
12 sub alone :Local :Sitemap {
13 ...
14 }
15
16 sub with_priority :Local :Sitemap(0.75) {
17 ...
18 }
19
20 sub with_args :Local
21 :Sitemap( lastmod => 2010-09-27, changefreq => daily ) {
22 ...
23 }
24
25 sub with_function :Local :Sitemap(*) {
26 ...
27 }
28
29 sub with_function_sitemap {
30 $_[2]->add( 'http://localhost/with_function' );
31 }
32
33 # and then...
34
35 sub sitemap : Path('/sitemap') {
36 my ( $self, $c ) = @_;
37
38 $c->res->body( $c->sitemap->xml );
39 }
40
41 =head1 DESCRIPTION
42
43 L<Catalyst::Plugin::Sitemap> provides a way to semi-automate the creation
44 of the sitemap of a Catalyst application.
45
46 =head1 CONTEXT METHOD
47
48 =head2 sitemap()
49
50 Returns a L<Search::Sitemap> object. The sitemap object is populated by
51 inspecting the controllers of the application for actions with the
52 sub attribute C<:Sitemap>.
53
54 =head1 C<:Sitemap> Subroutine Attribute
55
56 The sitemap is populated by actions ear-marked with the <:Sitemap> sub
57 attribute. It can be invoked in different ways:
58
59 =over
60
61 =item C<:Sitemap>
62
63 sub alone :Local :Sitemap {
64 ...
65 }
66
67 Adds the url of the action to the sitemap.
68
69 If the action does not
70 resolves in a single url, this will results in an error.
71
72 =item C<:Sitemap($priority)>
73
74
75 sub with_priority :Local :Sitemap(0.9) {
76 ...
77 }
78
79 Adds the url, with the given number, which has to be between 1 (inclusive)
80 and 0 (exclusive), as its priority.
81
82 If the action does not
83 resolves in a single url, this will results in an error.
84
85 =item C<:Sitemap( %attributes )>
86
87 sub with_args :Local
88 :Sitemap( lastmod => 2010-09-27, changefreq => daily ) {
89 ...
90 }
91
92 Adds the url with the given entry attributes (as defined by C<Search::Sitemap>).
93
94 If the action does not
95 resolves in a single url, this will results in an error.
96
97 =item C<:Sitemap(*)>
98
99 sub with_function :Local :Sitemap(*) { }
100
101 sub with_function_sitemap {
102 my ( $self, $c, $sitemap ) = @_;
103
104 $sitemap->add( 'http://localhost/with_function' );
105 }
106
107 Calls the function 'I<action>_sitemap', if it exists, and passes it the
108 controller, context and sitemap objects.
109
110 This is currently the only way to invoke C<:Sitemap> on an action
111 resolving to many urls.
112
113 =back
114
115
116 =head1 BUGS AND LIMITATIONS
117
118 Currently, each invocation of the method C<sitemap()> will
119 re-generate the C<Search::Sitemap> object. A future version
120 of this module might offer a way to only compute the sitemap
121 once.
122
123
124 =head1 SEE ALSO
125
126 =over
127
128 =item L<Search::Sitemap>
129
130 =item http://babyl.dyndns.org/techblog/entry/catalyst-plugin-sitemap
131
132 =back
133
134 =cut
135
136 use strict;
137 use warnings;
138
139 use Moose::Role;
140
141 no warnings qw/uninitialized/;
142
143 use Search::Sitemap;
144 use List::Util qw/ first /;
145
146 sub sitemap {
147 my $self = shift;
148
149 my $sitemap = Search::Sitemap->new;
150 $sitemap->pretty(1);
151
152 for my $controller ( $self->controller(qr//) ) {
153 ACTION:
154 for my $a ( $controller->get_action_methods ) {
155
156 my $action = $controller->action_for( $a->name );
157
158 my $attr = $action->attributes->{Sitemap} or next ACTION;
159
160 die "more than one attribute 'Sitemap' for sub ",
161 $a->fully_qualified_name
162 if @$attr > 1;
163
164 my @attr = split /\s*(?:,|=>)\s*/, $attr->[0];
165
166 my %uri_params;
167
168 if ( @attr == 1 ) {
169 if ( $attr[0] eq '*' ) {
170 my $sitemap_method = $action->name . "_sitemap";
171
172 if ( $controller->can($sitemap_method) ) {
173 $controller->$sitemap_method( $self, $sitemap );
174 next ACTION;
175 }
176 }
177
178 if ( $attr[0] + 0 > 0 ) {
179 # it's a number
180 $uri_params{priority} = $attr[0];
181 }
182
183 }
184 elsif ( @attr > 0 ) {
185 %uri_params = @attr;
186 }
187
188 $uri_params{loc} = $self->uri_for_action( $action->private_path );
189
190 $sitemap->add( \%uri_params );
191
192 next ACTION;
193 }
194
195 }
196
197 return $sitemap;
198 }
199
200 1;
201
202 __END__
This page took 0.044785 seconds and 4 git commands to generate.