| 1 | =head1 NAME | ||
| 2 | |||
| 3 | App::Basis::ConvertText2::Support | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | Support functions for L<App::Basis::ConvertText2> and its plugins | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | # ---------------------------------------------------------------------------- | ||
| 14 | |||
| 15 | package App::Basis::ConvertText2::Support; | ||
| 16 | $App::Basis::ConvertText2::Support::VERSION = '0.4'; | ||
| 17 | 1 | ▶ | use 5.10.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use Path::Tiny ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use Digest::MD5 qw(md5_hex); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 1 | ▶ | use Encode qw(encode_utf8); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 0 | ▶ | use GD; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 24 | use Exporter; | ||
| 25 | |||
| 26 | use vars qw( @EXPORT @ISA); | ||
| 27 | |||
| 28 | @ISA = qw(Exporter); | ||
| 29 | |||
| 30 | # this is the list of things that will get imported into the loading packages | ||
| 31 | # namespace | ||
| 32 | @EXPORT = qw( | ||
| 33 | cachefile | ||
| 34 | create_sig | ||
| 35 | create_img_src | ||
| 36 | ); | ||
| 37 | |||
| 38 | # ---------------------------------------------------------------------------- | ||
| 39 | # check if a file is in the cache, if so return the full file name | ||
| 40 | sub cachefile { | ||
| 41 | my ($cache, $filename) = @_; | ||
| 42 | |||
| 43 | # make sure we are working in the right dir | ||
| 44 | return $cache . "/" . path($filename)->basename; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | # ---------------------------------------------------------------------------- | ||
| 49 | # create a signature based on content and params to a element | ||
| 50 | sub create_sig { | ||
| 51 | my ( $content, $params ) = @_; | ||
| 52 | my $param_str = join( ' ', map { "$_='$params->{$_}'"; } sort keys %$params ); | ||
| 53 | |||
| 54 | return md5_hex( $content . encode_utf8($param_str) ); | ||
| 55 | } | ||
| 56 | |||
| 57 | # ---------------------------------------------------------------------------- | ||
| 58 | # create a HTML img element using the passed in filename, also grab the | ||
| 59 | # image from the file and add its width and height to the attributes | ||
| 60 | sub create_img_src { | ||
| 61 | my ( $file, $alt ) = @_; | ||
| 62 | |||
| 63 | return "" if ( !$file || !-f $file ); | ||
| 64 | |||
| 65 | my $out = "<img src='$file' "; | ||
| 66 | $out .= "alt='$alt' " if ($alt); | ||
| 67 | |||
| 68 | my $image = GD::Image->new($file); | ||
| 69 | if ($image) { | ||
| 70 | $out .= "height='" . $image->height() . "' width='" . $image->width() . "' "; | ||
| 71 | } | ||
| 72 | |||
| 73 | $out .= "/>"; | ||
| 74 | return $out; | ||
| 75 | } | ||
| 76 | |||
| 77 | # ---------------------------------------------------------------------------- | ||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | __END__ | ||