package App::Dapper::Utils; =head1 NAME App::Dapper::Utils - Common utility functions used throughout Dapper. =cut use utf8; use open ':std', ':encoding(UTF-8)'; use 5.8.0; use strict; use warnings FATAL => 'all'; use POSIX; use Unicode::Normalize; use File::Spec::Functions qw/ canonpath /; my $DEFAULT_LAYOUT = "index"; =head2 read_file Read a file and return a scalar with the file's contents. =cut # Takes a file name and returns a string of the contents sub read_file { my ($file_name) = @_; my $file_contents; #print "Reading contents of $file_name\n"; open(FILE, "<:encoding(UTF-8)", "$file_name") or die("could not open file: $!\n"); foreach () { $file_contents .= $_; } close(FILE) or die("could not close file: $!\n"); return $file_contents; } =head2 get_modified_time Takes a file and returns the last modified time of the file. =cut sub get_modified_time { my ($file) = @_; my $date = POSIX::strftime( "%Y-%m-%dT%H:%M:%S", localtime((stat $file)[9])); return $date; } =head2 slugify Takes an input string (e.g. the title of a blog post) and "slugifies" it, meaning that it removes non-ASCII characters, removes all non-word characters (e.g. '_', '-', etc.), removes leading and trailing whitespace, replaces spaces with hyphens, and converts to lowercase. =cut sub slugify { my ($input) = @_; if (not defined $input) { return; } #print "Slugifying $input\n"; $input = NFKD($input); # Normalize the Unicode string $input =~ tr/\000-\177//cd; # Strip non-ASCII characters (>127) $input =~ s/[^\w\s-]//g; # Remove all characters that are not word characters (includes _), spaces, or hyphens $input =~ s/^\s+|\s+$//g; # Trim whitespace from both ends $input = lc($input); $input =~ s/[-\s]+/-/g; # Replace all occurrences of spaces and hyphens with a single hyphen return $input; } =head2 find_template_statement Takes a string, returns