| 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 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 65 | 11 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 11 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 79 | 11 | my ( $proto, %supplied ) = (@_); | |||||||||||||||||||
| 80 | 11 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 11 | my $self = {}; | |||||||||||||||||||
| 83 | 11 | bless( $self, $class ); | |||||||||||||||||||
| 84 | 11 | 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 | 9 | ▶ | { | ||||||||||||||||||
|
Subroutine
expand_variables: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 103 | 9 | my ( $self, $site, $page, $data ) = (@_); | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | # | ||||||||||||||||||||
| 106 | # Get the page-variables in the template. | ||||||||||||||||||||
| 107 | # | ||||||||||||||||||||
| 108 | 9 | my %hash = %$data; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # | ||||||||||||||||||||
| 111 | # Compute the path to the web-root | ||||||||||||||||||||
| 112 | # | ||||||||||||||||||||
| 113 | 9 | ▶ | return ( \%hash ) if ( !$page ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 8 | ▶ | my $root_path = $page->source() || ""; | ||||||||||||||||||
|
Condition100%2/2
$page->source || ''
MC/DC100%1/1
$page->source || ''
$page->source
Truth table100%2/2
A
A= $page->source
| |||||||||||||||||||||
| 116 | 8 | ▶ | my $input = $site->get("input") || ""; | ||||||||||||||||||
|
Condition100%2/2
$site->get('input') || ''
MC/DC100%1/1
$site->get('input') || ''
$site->get('input')
Truth table100%2/2
A
A= $site->get('input')
| |||||||||||||||||||||
| 117 | 8 | $root_path =~ s{^$input}{./}; # Change leading input path | |||||||||||||||||||
| 118 | 8 | $root_path =~ s{[^/]+$}{}; # Remove trailing pagename | |||||||||||||||||||
| 119 | 8 | $root_path =~ s{/[^/]+}{/..}g; # Replace directories by .. | |||||||||||||||||||
| 120 | 8 | $root_path =~ s{/$}{}; # Remove trailing / | |||||||||||||||||||
| 121 | 8 | $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 | foreach my $key ( keys %hash ) | |||||||||||||||||||
| 128 | { | ||||||||||||||||||||
| 129 | 63 | ▶ | if ( $hash{ $key } =~ /^path_to\((.*)\)/ ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | { | ||||||||||||||||||||
| 131 | 4 | my $path = $1; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | # | ||||||||||||||||||||
| 134 | # Strip leading/trailing whitespace. | ||||||||||||||||||||
| 135 | # | ||||||||||||||||||||
| 136 | 4 | $path =~ s/^\s+|\s+$//g; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | # | ||||||||||||||||||||
| 139 | # Ensure path is absolute | ||||||||||||||||||||
| 140 | # | ||||||||||||||||||||
| 141 | 4 | ▶ | if ($path) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | { | ||||||||||||||||||||
| 143 | 2 | $path = "/$path"; | |||||||||||||||||||
| 144 | 2 | $path =~ s{^/+}{/}; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | # | ||||||||||||||||||||
| 148 | # Store specified path starting from web-root. | ||||||||||||||||||||
| 149 | # | ||||||||||||||||||||
| 150 | 4 | $hash{ $key } = "$root_path$path"; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | # | ||||||||||||||||||||
| 155 | # Return. | ||||||||||||||||||||
| 156 | # | ||||||||||||||||||||
| 157 | 8 | return ( \%hash ); | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | # | ||||||||||||||||||||
| 162 | # Register the plugin. | ||||||||||||||||||||
| 163 | # | ||||||||||||||||||||
| 164 | 11 | Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::RootPath"); | |||||||||||||||||||