| 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 |  |  |  |  |  |  | <p>CSS files are stored in <!-- tmpl_var name='css' -->. | 
| 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<path_to> 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 <bruno@boulgour.com> | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =cut | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | Copyright (C) 2015 Bruno BEAUFILS <bruno@boulgour.com>. | 
| 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 |  | 4015 | use strict; | 
|  | 11 |  |  |  |  | 13 |  | 
|  | 11 |  |  |  |  | 247 |  | 
| 65 | 11 |  |  | 11 |  | 30 | use warnings; | 
|  | 11 |  |  |  |  | 11 |  | 
|  | 11 |  |  |  |  | 3032 |  | 
| 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 | 22 | my ( $proto, %supplied ) = (@_); | 
| 80 | 11 |  | 33 |  |  | 68 | my $class = ref($proto) || $proto; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 11 |  |  |  |  | 18 | my $self = {}; | 
| 83 | 11 |  |  |  |  | 15 | bless( $self, $class ); | 
| 84 | 11 |  |  |  |  | 79 | return $self; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =head2 expand_variables | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | This is the method which is called by the L<Templer::Plugin::Factory> | 
| 91 |  |  |  |  |  |  | to expand the variables contained in a L<Templer::Site::Page> 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 | 16 | my ( $self, $site, $page, $data ) = (@_); | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 9 |  |  |  |  | 30 | my %hash = %$data; | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 9 | 100 |  |  |  | 26 | return ( \%hash ) if ( !$page ); | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 8 |  | 100 |  |  | 28 | my $root_path = $page->source()     || ""; | 
| 116 | 8 |  | 100 |  |  | 32 | my $input     = $site->get("input") || ""; | 
| 117 | 8 |  |  |  |  | 82 | $root_path =~ s{^$input}{./}; | 
| 118 | 8 |  |  |  |  | 35 | $root_path =~ s{[^/]+$}{}; | 
| 119 | 8 |  |  |  |  | 21 | $root_path =~ s{/[^/]+}{/..}g; | 
| 120 | 8 |  |  |  |  | 19 | $root_path =~ s{/$}{}; | 
| 121 | 8 |  |  |  |  | 12 | $root_path =~ s{^./}{}; | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 8 |  |  |  |  | 26 | foreach my $key ( keys %hash ) | 
| 128 |  |  |  |  |  |  | { | 
| 129 | 63 | 100 |  |  |  | 172 | if ( $hash{ $key } =~ /^path_to\((.*)\)/ ) | 
| 130 |  |  |  |  |  |  | { | 
| 131 | 4 |  |  |  |  | 7 | my $path = $1; | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 4 |  |  |  |  | 6 | $path =~ s/^\s+|\s+$//g; | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 4 | 100 |  |  |  | 5 | if ($path) | 
| 142 |  |  |  |  |  |  | { | 
| 143 | 2 |  |  |  |  | 2 | $path = "/$path"; | 
| 144 | 2 |  |  |  |  | 6 | $path =~ s{^/+}{/}; | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 4 |  |  |  |  | 8 | $hash{ $key } = "$root_path$path"; | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  | } | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  |  | 
| 157 | 8 |  |  |  |  | 24 | return ( \%hash ); | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::RootPath"); | 
| 165 |  |  |  |  |  |  |  |