File Coverage

lib/Templer/Plugin/SiteMap.pm
Criterion Covered Total %
statement 21 29 72.4
branch 0 2 0.0
condition 4 12 33.3
subroutine 6 6 100.0
pod 3 3 100.0
total 34 52 65.3


line stmt bran cond sub pod time code
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 file including
28             references to all pages which C 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
48              
49             =cut
50              
51             =head1 COPYRIGHT AND LICENSE
52              
53             Copyright (C) 2016 Steve Kemp .
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     11   6698 use strict;
  11         17  
  11         1468  
67 11     11   47 use warnings;
  11         18  
  11         276  
68              
69 11     11   46 use POSIX qw(strftime);
  11         91  
  11         59  
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             {
82 11     11 1 19 my ( $proto, %supplied ) = (@_);
83 11   33     54 my $class = ref($proto) || $proto;
84              
85 11         20 my $self = {};
86 11         18 bless( $self, $class );
87 11         103 return $self;
88             }
89              
90              
91             =head2 init
92              
93             Initialisation function, which merely saves a reference to the
94             L object.
95              
96             =cut
97              
98              
99             sub init
100             {
101 2     2 1 5 my( $self, $site ) = ( @_);
102              
103 2   33     18 $self->{ 'site' } ||= $site;
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             {
119 2     2 1 4 my ($self) = (@_);
120              
121             #
122             # Gain access to our expected configuration values.
123             #
124 2         6 my $file = $self->{'site'}->{'sitemap_file'};
125 2         2 my $base = $self->{'site'}->{'sitemap_base'};
126 2         4 my $path = $self->{'site'}->{'output'};
127              
128 2 0 33     8 return unless ($file && $base && $path );
      33        
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 <
140            
141            
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            
147             $base
148             0.75
149             daily
150            
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 <
161            
162             $base$url
163             0.50
164             weekly
165            
166             EOF
167             }
168              
169 0           print $map <
170            
171             EOF
172 0           close($map);
173             }
174              
175             #
176             # Register the plugin.
177             #
178             Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::SiteMap");