lib/Templer/Plugin/SiteMap.pm

filter: Statementstmt 73.3% 22 / 30 Branchbran 50.0% 1 / 2 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 60.0% 36 / 60 CC 7 SCAR 17.1
CC
7
Cov
61%
cleanup
10.9
init
2.3
new
2.1
CRAP 5.5
« lib/Templer/Plugin/Redis.pm ↑ Summary lib/Templer/Plugin/FileContents.pm »
1  
2 =head1 NAME
3  
4 Templer::Plugin::SiteMap - Generate a SiteMap automatically.
5  
6 =cut
7  
8 =head1 SYNOPSIS
9  
10 This plugin must be enabled by adding two settings in the
11 global configuration file; the name of the file to generate
12 and the prefix of the output URLs.
13  
14 The following is a good example:
15  
16 =for example begin
17  
18 sitemap_file = /sitemap.xml
19 sitemap_base = http://example.com/
20  
21 =for example end
22  
23 =cut
24  
25 =head1 DESCRIPTION
26  
27 This plugin will generate a simple C<sitemap.xml> file including
28 references to all pages which C<templer> knows about.
29  
30 =cut
31  
32 =head1 LICENSE
33  
34 This module is free software; you can redistribute it and/or modify it
35 under the terms of either:
36  
37 a) the GNU General Public License as published by the Free Software
38 Foundation; either version 2, or (at your option) any later version,
39 or
40  
41 b) the Perl "Artistic License".
42  
43 =cut
44  
45 =head1 AUTHOR
46  
47 Steve Kemp <steve@steve.org.uk>
48  
49 =cut
50  
51 =head1 COPYRIGHT AND LICENSE
52  
53 Copyright (C) 2016 Steve Kemp <steve@steve.org.uk>.
54  
55 This library is free software. You can modify and or distribute it under
56 the same terms as Perl itself.
57  
58 =cut
59  
60 =head1 METHODS
61  
62 =cut
63  
64  
65  
66 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 11 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 package Templer::Plugin::SiteMap;
72  
73  
74 =head2 new
75  
76 Constructor. No arguments are required/supported.
77  
78 =cut
79  
80 sub new
81 11 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod documented
82 11 my ( $proto, %supplied ) = (@_);
83 11 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
84  
85 11 my $self = {};
86 11 bless( $self, $class );
87 11 return $self;
88 }
89  
90  
91 =head2 init
92  
93 Initialisation function, which merely saves a reference to the
94 L<Templer::Site> object.
95  
96 =cut
97  
98  
99 sub init
100 2 ▶{
Subroutine init: called CC=2 SCAR=8.1
Pod documented
101 2 my( $self, $site ) = ( @_);
102  
103 2 ▶ $self->{ 'site' } ||= $site;
Condition33%1/3
$self->{"site"} ||= $site
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"site"} ||= $site
$self->{"site"} $site
Truth table33%1/3
A ||= B
A= $self->{"site"}B= $site
ABresult
1X1
011
000
104 }
105  
106  
107 =head2 cleanup
108  
109 This method is invoked when site-generation is complete, and this
110 is where we generate the sitemap, if our two required configuration
111 values are present in the configuration file.
112  
113 If configuration-variables are not setup then we do nothing.
114  
115 =cut
116  
117 sub cleanup
118 2 ▶{
Subroutine cleanup: called CC=5 SCAR=23.9
Pod documented
119 2 my ($self) = (@_);
120  
121 #
122 # Gain access to our expected configuration values.
123 #
124 2 my $file = $self->{'site'}->{'sitemap_file'};
125 2 my $base = $self->{'site'}->{'sitemap_base'};
126 2 my $path = $self->{'site'}->{'output'};
127  
128 2 ▶ return unless ($file && $base && $path );
Branch: 1 branch
BranchTrueFalseTotal
unless $file and $base and $path 2 0 2
Condition33%2/6
$file and $base and $path
!ll&&!rl&&r
200
$file and $base
!ll&&!rl&&r
200
MC/DC0%0/3
$file and $base and $path
$file $base $path
Truth table25%1/4
A and B and C
A= $fileB= $baseC= $path
ABCresult
0XX0
10X0
1100
1111
129  
130 #
131 # The pages we know about.
132 #
133 0 my $pages = $self->{ 'site' }->{ 'output-files' };
134  
135 #
136 # Open the sitemap file
137 #
138 0 open( my $map, ">", $path . $file );
139 0 print $map <<EOF;
140 <?xml version="1.0" encoding="UTF-8"?>
141 <urlset
142 xmlns="http://www.google.com/schemas/sitemap/0.84"
143 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
144 xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
145 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
146 <url>
147 <loc>$base</loc>
148 <priority>0.75</priority>
149 <changefreq>daily</changefreq>
150 </url>
151 EOF
152  
153  
154 #
155 # For each page
156 #
157 0 foreach my $page (@$pages)
158 {
159 0 my $url = substr( $page, length($path) );
160 0 print $map <<EOF;
161 <url>
162 <loc>$base$url</loc>
163 <priority>0.50</priority>
164 <changefreq>weekly</changefreq>
165 </url>
166 EOF
167 }
168  
169 0 print $map <<EOF;
170 </urlset>
171 EOF
172 0 close($map);
173 }
174  
175 #
176 # Register the plugin.
177 #
178 11 Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::SiteMap");
« lib/Templer/Plugin/Redis.pm ↑ Summary lib/Templer/Plugin/FileContents.pm »