File Coverage

lib/Templer/Plugin/RootPath.pm
Criterion Covered Total %
statement 30 30 100.0
branch 6 6 100.0
condition 5 7 71.4
subroutine 4 4 100.0
pod 1 2 50.0
total 46 49 93.8


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Templer::Plugin::RootPath - A plugin to add path to web-root to variables.
5              
6             =cut
7              
8             =head1 SYNOPSIS
9              
10             The following is a good example use of this plugin
11              
12             title: About my site
13             css: path_to(css)
14             ----
15            

CSS files are stored in .

16              
17             This is mainly useful as global variables used in the default layout.
18              
19             =cut
20              
21             =head1 DESCRIPTION
22              
23             This plugin allows template variables (considered as absolute path from the
24             web-root) to be set to a correct relative path from any page of the website.
25              
26             An empty call to C may be used to compute the relative path to the
27             web-root.
28              
29             =cut
30              
31             =head1 LICENSE
32              
33             This module is free software; you can redistribute it and/or modify it
34             under the terms of either:
35              
36             a) the GNU General Public License as published by the Free Software
37             Foundation; either version 2, or (at your option) any later version,
38             or
39              
40             b) the Perl "Artistic License".
41              
42             =cut
43              
44             =head1 AUTHOR
45              
46             Bruno BEAUFILS
47              
48             =cut
49              
50             =head1 COPYRIGHT AND LICENSE
51              
52             Copyright (C) 2015 Bruno BEAUFILS .
53              
54             This library is free software. You can modify and or distribute it under
55             the same terms as Perl itself.
56              
57             =cut
58              
59             =head1 METHODS
60              
61             =cut
62              
63              
64 11     11   17115 use strict;
  11         22  
  11         296  
65 11     11   45 use warnings;
  11         18  
  11         3840  
66              
67              
68             package Templer::Plugin::RootPath;
69              
70              
71             =head2
72              
73             Constructor. No arguments are required/supported.
74              
75             =cut
76              
77             sub new
78             {
79 11     11 0 21 my ( $proto, %supplied ) = (@_);
80 11   33     125 my $class = ref($proto) || $proto;
81              
82 11         16 my $self = {};
83 11         45 bless( $self, $class );
84 11         91 return $self;
85             }
86              
87              
88             =head2 expand_variables
89              
90             This is the method which is called by the L
91             to expand the variables contained in a L object.
92              
93             Variables are written in the file in the form "key: value", and are
94             internally stored within the Page object as a hash.
95              
96             This method iterates over each key & value and updates any that
97             seem to refer to file-paths.
98              
99             =cut
100              
101             sub expand_variables
102             {
103 9     9 1 32 my ( $self, $site, $page, $data ) = (@_);
104              
105             #
106             # Get the page-variables in the template.
107             #
108 9         45 my %hash = %$data;
109              
110             #
111             # Compute the path to the web-root
112             #
113 9 100       31 return ( \%hash ) if ( !$page );
114              
115 8   100     30 my $root_path = $page->source() || "";
116 8   100     36 my $input = $site->get("input") || "";
117 8         116 $root_path =~ s{^$input}{./}; # Change leading input path
118 8         47 $root_path =~ s{[^/]+$}{}; # Remove trailing pagename
119 8         30 $root_path =~ s{/[^/]+}{/..}g; # Replace directories by ..
120 8         27 $root_path =~ s{/$}{}; # Remove trailing /
121 8         17 $root_path =~ s{^./}{}; # Remove leading ./ if still there
122              
123              
124             #
125             # Look for a value of "path_to" in each key.
126             #
127 8         30 foreach my $key ( keys %hash )
128             {
129 63 100       140 if ( $hash{ $key } =~ /^path_to\((.*)\)/ )
130             {
131 4         9 my $path = $1;
132              
133             #
134             # Strip leading/trailing whitespace.
135             #
136 4         9 $path =~ s/^\s+|\s+$//g;
137              
138             #
139             # Ensure path is absolute
140             #
141 4 100       21 if ($path)
142             {
143 2         6 $path = "/$path";
144 2         8 $path =~ s{^/+}{/};
145             }
146              
147             #
148             # Store specified path starting from web-root.
149             #
150 4         11 $hash{ $key } = "$root_path$path";
151             }
152             }
153              
154             #
155             # Return.
156             #
157 8         29 return ( \%hash );
158             }
159              
160              
161             #
162             # Register the plugin.
163             #
164             Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::RootPath");