File Coverage

blib/lib/App/Dapper/Utils.pm
Criterion Covered Total %
statement 24 71 33.8
branch 0 18 0.0
condition n/a
subroutine 8 17 47.0
pod 9 9 100.0
total 41 115 35.6


line stmt bran cond sub pod time code
1             package App::Dapper::Utils;
2              
3             =head1 NAME
4              
5             App::Dapper::Utils - Common utility functions used throughout Dapper.
6              
7             =cut
8              
9 2     2   11 use utf8;
  2         3  
  2         12  
10 2     2   55 use open ':std', ':encoding(UTF-8)';
  2         4  
  2         10  
11 2     2   797 use 5.8.0;
  2         7  
  2         64  
12 2     2   10 use strict;
  2         4  
  2         81  
13 2     2   22 use warnings FATAL => 'all';
  2         3  
  2         66  
14              
15 2     2   10 use POSIX;
  2         4  
  2         24  
16 2     2   15346 use Unicode::Normalize;
  2         7079  
  2         211  
17 2     2   20 use File::Spec::Functions qw/ canonpath /;
  2         4  
  2         2630  
18              
19             my $DEFAULT_LAYOUT = "index";
20              
21             =head2 read_file
22              
23             Read a file and return a scalar with the file's contents.
24              
25             =cut
26              
27             # Takes a file name and returns a string of the contents
28             sub read_file {
29 0     0 1   my ($file_name) = @_;
30 0           my $file_contents;
31              
32             #print "Reading contents of $file_name\n";
33              
34 0 0         open(FILE, "<:encoding(UTF-8)", "$file_name") or die("could not open file: $!\n");
35 0           foreach () { $file_contents .= $_; }
  0            
36 0 0         close(FILE) or die("could not close file: $!\n");
37              
38 0           return $file_contents;
39             }
40              
41             =head2 get_modified_time
42              
43             Takes a file and returns the last modified time of the file.
44              
45             =cut
46              
47             sub get_modified_time {
48 0     0 1   my ($file) = @_;
49 0           my $date = POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime((stat $file)[9]));
50 0           return $date;
51             }
52              
53             =head2 slugify
54              
55             Takes an input string (e.g. the title of a blog post) and "slugifies" it, meaning that
56             it removes non-ASCII characters, removes all non-word characters (e.g. '_', '-', etc.),
57             removes leading and trailing whitespace, replaces spaces with hyphens, and converts to
58             lowercase.
59              
60             =cut
61              
62             sub slugify {
63 0     0 1   my ($input) = @_;
64              
65 0 0         if (not defined $input) { return; }
  0            
66              
67             #print "Slugifying $input\n";
68              
69 0           $input = NFKD($input); # Normalize the Unicode string
70 0           $input =~ tr/\000-\177//cd; # Strip non-ASCII characters (>127)
71 0           $input =~ s/[^\w\s-]//g; # Remove all characters that are not word characters (includes _), spaces, or hyphens
72 0           $input =~ s/^\s+|\s+$//g; # Trim whitespace from both ends
73 0           $input = lc($input);
74 0           $input =~ s/[-\s]+/-/g; # Replace all occurrences of spaces and hyphens with a single hyphen
75              
76 0           return $input;
77             }
78              
79             =head2 find_template_statement
80              
81             Takes a string, returns