| 1 | |||||||||||||||||||||
| 2 | =head1 NAME | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | Templer::Plugin::TimeStamp - A plugin to get TimeStamp of source files | ||||||||||||||||||||
| 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 | mtime: timestamp(%Y-%m-%d %H:M:%S) | ||||||||||||||||||||
| 14 | ---- | ||||||||||||||||||||
| 15 | <p>This file has been last modified on <!-- tmpl_var name='mtime' -->. | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | This plugin allows template variables to be set to the source file | ||||||||||||||||||||
| 22 | modification time. The parameter passed to the function are passed as is to | ||||||||||||||||||||
| 23 | strftime with leading and trailing spaces removed. | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 LICENSE | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 30 | under the terms of either: | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | a) the GNU General Public License as published by the Free Software | ||||||||||||||||||||
| 33 | Foundation; either version 2, or (at your option) any later version, | ||||||||||||||||||||
| 34 | or | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | b) the Perl "Artistic License". | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =head1 AUTHOR | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | Bruno BEAUFILS <bruno@boulgour.com> | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | Copyright (C) 2015 Bruno BEAUFILS <bruno@boulgour.com>. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | This library is free software. You can modify and or distribute it under | ||||||||||||||||||||
| 51 | the same terms as Perl itself. | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head1 METHODS | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 11 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 61 | 11 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 11 | ▶ | use POSIX qw{strftime}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | package Templer::Plugin::TimeStamp; | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head2 | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | Constructor. No arguments are required/supported. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | sub new | ||||||||||||||||||||
| 76 | 11 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 77 | 11 | my ( $proto, %supplied ) = (@_); | |||||||||||||||||||
| 78 | 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
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 11 | my $self = {}; | |||||||||||||||||||
| 81 | 11 | bless( $self, $class ); | |||||||||||||||||||
| 82 | 11 | return $self; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =head2 expand_variables | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | This is the method which is called by the L<Templer::Plugin::Factory> | ||||||||||||||||||||
| 89 | to expand the variables contained in a L<Templer::Site::Page> object. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Variables are written in the file in the form "key: value", and are | ||||||||||||||||||||
| 92 | internally stored within the Page object as a hash. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | This method iterates over each key & value and updates any that | ||||||||||||||||||||
| 95 | seem to refer to timestamps. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | sub expand_variables | ||||||||||||||||||||
| 100 | 9 | ▶ | { | ||||||||||||||||||
|
Subroutine
expand_variables: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 101 | 9 | my ( $self, $site, $page, $data ) = (@_); | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | # | ||||||||||||||||||||
| 104 | # Get the page-variables in the template. | ||||||||||||||||||||
| 105 | # | ||||||||||||||||||||
| 106 | 9 | my %hash = %$data; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # | ||||||||||||||||||||
| 109 | # The mtime of the page-source. Cached. | ||||||||||||||||||||
| 110 | # | ||||||||||||||||||||
| 111 | 9 | my $mtime; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | # | ||||||||||||||||||||
| 114 | # Look for a value of "timestamp" in each key. | ||||||||||||||||||||
| 115 | # | ||||||||||||||||||||
| 116 | 9 | foreach my $key ( keys %hash ) | |||||||||||||||||||
| 117 | { | ||||||||||||||||||||
| 118 | 66 | ▶ | if ( $hash{ $key } =~ /^timestamp\((.*)\)/ ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | { | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # | ||||||||||||||||||||
| 122 | # The format to use. | ||||||||||||||||||||
| 123 | # | ||||||||||||||||||||
| 124 | 2 | my $ts = $1; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | # | ||||||||||||||||||||
| 127 | # Strip leading/trailing whitespace. | ||||||||||||||||||||
| 128 | # | ||||||||||||||||||||
| 129 | 2 | $ts =~ s/^\s+|\s+$//g; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | # | ||||||||||||||||||||
| 132 | # Get mtime of the source file - if we've not already done so. | ||||||||||||||||||||
| 133 | # | ||||||||||||||||||||
| 134 | 2 | ▶ | if ( !$mtime ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | { | ||||||||||||||||||||
| 136 | 1 | $mtime = ( stat( $page->source() ) )[9]; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | # | ||||||||||||||||||||
| 140 | # Store formatted modification time | ||||||||||||||||||||
| 141 | # | ||||||||||||||||||||
| 142 | 2 | my @mtime = localtime($mtime); | |||||||||||||||||||
| 143 | 2 | $hash{ $key } = POSIX::strftime $ts, @mtime; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | # | ||||||||||||||||||||
| 148 | # Return. | ||||||||||||||||||||
| 149 | # | ||||||||||||||||||||
| 150 | 9 | return ( \%hash ); | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | # | ||||||||||||||||||||
| 155 | # Register the plugin. | ||||||||||||||||||||
| 156 | # | ||||||||||||||||||||
| 157 | 11 | Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::TimeStamp"); | |||||||||||||||||||