| lib/Templer/Plugin/Breadcrumbs.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 16 | 32 | 50.0 |
| branch | 1 | 10 | 10.0 |
| condition | 1 | 3 | 33.3 |
| subroutine | 4 | 4 | 100.0 |
| pod | 2 | 2 | 100.0 |
| total | 24 | 51 | 47.0 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | |||||||
| 2 | =head1 NAME | ||||||
| 3 | |||||||
| 4 | Templer::Plugin::Breadcrumbs - A plugin to create breadcrumbs | ||||||
| 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 | crumbs: Home,Software | ||||||
| 14 | ---- | ||||||
| 15 | This is my page content... |
||||||
| 16 | |||||||
| 17 | |||||||
| 18 | Here the variable 'crumbs' will be converted into a loop variable called | ||||||
| 19 | 'breadcrumbs'. THe links and titls will be set automatically, but if you | ||||||
| 20 | need to override them you can do so via: | ||||||
| 21 | |||||||
| 22 | title: About my site | ||||||
| 23 | crumbs: Home,Software[Soft],Testing[Test] | ||||||
| 24 | ---- | ||||||
| 25 | This is my page content... |
||||||
| 26 | |||||||
| 27 | |||||||
| 28 | That will result in links to /, /Soft, and /Soft/Test, respectively. With | ||||||
| 29 | display names of "Home", "Software" and "Testing". | ||||||
| 30 | |||||||
| 31 | =cut | ||||||
| 32 | |||||||
| 33 | =head1 DESCRIPTION | ||||||
| 34 | |||||||
| 35 | This plugin expands the values of "crumbs", as written in a template, to | ||||||
| 36 | a loop-variable with the name "breadcrumbs". | ||||||
| 37 | |||||||
| 38 | This can be used in a template like so: | ||||||
| 39 | |||||||
| 40 | =for example begin | ||||||
| 41 | |||||||
| 42 | |||||||
| 43 | |
||||||
| 44 | |||||||
| 45 | |
||||||
| 46 | |||||||
| 47 | |||||||
| 48 | |||||||
| 49 | |||||||
| 50 | =for example end | ||||||
| 51 | |||||||
| 52 | This template is an example. | ||||||
| 53 | |||||||
| 54 | =cut | ||||||
| 55 | |||||||
| 56 | =head1 LICENSE | ||||||
| 57 | |||||||
| 58 | This module is free software; you can redistribute it and/or modify it | ||||||
| 59 | under the terms of either: | ||||||
| 60 | |||||||
| 61 | a) the GNU General Public License as published by the Free Software | ||||||
| 62 | Foundation; either version 2, or (at your option) any later version, | ||||||
| 63 | or | ||||||
| 64 | |||||||
| 65 | b) the Perl "Artistic License". | ||||||
| 66 | |||||||
| 67 | =cut | ||||||
| 68 | |||||||
| 69 | =head1 AUTHOR | ||||||
| 70 | |||||||
| 71 | Steve Kemp |
||||||
| 72 | |||||||
| 73 | =cut | ||||||
| 74 | |||||||
| 75 | =head1 COPYRIGHT AND LICENSE | ||||||
| 76 | |||||||
| 77 | Copyright (C) 2015 Steve Kemp |
||||||
| 78 | |||||||
| 79 | This library is free software. You can modify and or distribute it under | ||||||
| 80 | the same terms as Perl itself. | ||||||
| 81 | |||||||
| 82 | =cut | ||||||
| 83 | |||||||
| 84 | =head1 METHODS | ||||||
| 85 | |||||||
| 86 | =cut | ||||||
| 87 | |||||||
| 88 | |||||||
| 89 | 11 | 11 | 5251 | use strict; | |||
| 11 | 21 | ||||||
| 11 | 268 | ||||||
| 90 | 11 | 11 | 53 | use warnings; | |||
| 11 | 19 | ||||||
| 11 | 3640 | ||||||
| 91 | |||||||
| 92 | |||||||
| 93 | package Templer::Plugin::Breadcrumbs; | ||||||
| 94 | |||||||
| 95 | |||||||
| 96 | =head2 new | ||||||
| 97 | |||||||
| 98 | Constructor. No arguments are required/supported. | ||||||
| 99 | |||||||
| 100 | =cut | ||||||
| 101 | |||||||
| 102 | sub new | ||||||
| 103 | { | ||||||
| 104 | 11 | 11 | 1 | 25 | my ( $proto, %supplied ) = (@_); | ||
| 105 | 11 | 33 | 56 | my $class = ref($proto) || $proto; | |||
| 106 | |||||||
| 107 | 11 | 19 | my $self = {}; | ||||
| 108 | 11 | 20 | bless( $self, $class ); | ||||
| 109 | 11 | 89 | return $self; | ||||
| 110 | } | ||||||
| 111 | |||||||
| 112 | |||||||
| 113 | |||||||
| 114 | =head2 expand_variables | ||||||
| 115 | |||||||
| 116 | This is the method which is called by the L |
||||||
| 117 | to expand the variables contained in a L |
||||||
| 118 | |||||||
| 119 | This method will expand any variable that is called 'crumbs' into a HTML::Template | ||||||
| 120 | loop, suitable for the display of breadcrumbs. | ||||||
| 121 | |||||||
| 122 | =cut | ||||||
| 123 | |||||||
| 124 | sub expand_variables | ||||||
| 125 | { | ||||||
| 126 | 9 | 9 | 1 | 41 | my ( $self, $site, $page, $data ) = (@_); | ||
| 127 | |||||||
| 128 | # | ||||||
| 129 | # Get the page-variables in the template. | ||||||
| 130 | # | ||||||
| 131 | 9 | 49 | my %hash = %$data; | ||||
| 132 | |||||||
| 133 | # | ||||||
| 134 | # Look for a value of "read_file" in each key. | ||||||
| 135 | # | ||||||
| 136 | 9 | 46 | foreach my $key ( keys %hash ) | ||||
| 137 | { | ||||||
| 138 | 66 | 50 | 128 | if ( $key =~ /^crumbs$/i ) | |||
| 139 | { | ||||||
| 140 | 0 | 0 | my $loop; | ||||
| 141 | |||||||
| 142 | 0 | 0 | my $val = $hash{ $key }; | ||||
| 143 | 0 | 0 | my $link = "/"; | ||||
| 144 | |||||||
| 145 | 0 | 0 | foreach my $path ( split( /,/, $val ) ) | ||||
| 146 | { | ||||||
| 147 | 0 | 0 | $path =~ s/^\s+|\s+$//g; | ||||
| 148 | |||||||
| 149 | 0 | 0 | 0 | if ( $path =~ /Home/i ) | |||
| 150 | { | ||||||
| 151 | } | ||||||
| 152 | else | ||||||
| 153 | { | ||||||
| 154 | 0 | 0 | 0 | if ( $path =~ /(.*)\[(.*)\]/ ) | |||
| 155 | { | ||||||
| 156 | 0 | 0 | $link .= "/" . $1; | ||||
| 157 | } | ||||||
| 158 | else | ||||||
| 159 | { | ||||||
| 160 | 0 | 0 | $link .= "/" . $path; | ||||
| 161 | } | ||||||
| 162 | } | ||||||
| 163 | |||||||
| 164 | 0 | 0 | $link =~ s/\/\//\//g; | ||||
| 165 | |||||||
| 166 | 0 | 0 | 0 | if ( $path =~ /\[(.*)\]/ ) | |||
| 167 | { | ||||||
| 168 | 0 | 0 | $path = $1; | ||||
| 169 | } | ||||||
| 170 | else | ||||||
| 171 | { | ||||||
| 172 | 0 | 0 | $path = ucfirst($path); | ||||
| 173 | } | ||||||
| 174 | 0 | 0 | push( @$loop, | ||||
| 175 | { title => $path, | ||||||
| 176 | link => $link | ||||||
| 177 | } ); | ||||||
| 178 | } | ||||||
| 179 | |||||||
| 180 | |||||||
| 181 | 0 | 0 | 0 | $hash{ 'breadcrumbs' } = $loop if ($loop); | |||
| 182 | 0 | 0 | delete $hash{ $key }; | ||||
| 183 | } | ||||||
| 184 | } | ||||||
| 185 | |||||||
| 186 | 9 | 29 | return ( \%hash ); | ||||
| 187 | } | ||||||
| 188 | |||||||
| 189 | |||||||
| 190 | # | ||||||
| 191 | # Register the plugin. | ||||||
| 192 | # | ||||||
| 193 | Templer::Plugin::Factory->new() | ||||||
| 194 | ->register_plugin("Templer::Plugin::Breadcrumbs"); |