File Coverage

blib/lib/PDF/Collage.pm
Criterion Covered Total %
statement 71 80 88.7
branch 17 34 50.0
condition 5 15 33.3
subroutine 16 17 94.1
pod 5 5 100.0
total 114 151 75.5


line stmt bran cond sub pod time code
1             package PDF::Collage;
2 2     2   373834 use v5.24;
  2         12  
3 2     2   12 use warnings;
  2         8  
  2         147  
4 2     2   705 use experimental qw< signatures >;
  2         2125  
  2         12  
5 2     2   387 no warnings qw< experimental::signatures >;
  2         6  
  2         227  
6             { our $VERSION = '0.003' }
7              
8 2     2   16 use Carp;
  2         5  
  2         155  
9 2     2   1304 use English qw< -no_match_vars >;
  2         4623  
  2         14  
10 2     2   1754 use JSON::PP qw< decode_json >;
  2         23525  
  2         211  
11 2     2   17 use Scalar::Util qw< blessed >;
  2         6  
  2         118  
12 2     2   1371 use Data::Resolver qw< file_to_data >;
  2         9545  
  2         169  
13 2     2   1251 use PDF::Collage::Template ();
  2         10  
  2         66  
14 2     2   1427 use PDF::Collage::TemplatesCollection ();
  2         10  
  2         76  
15              
16 2     2   13 use Exporter 'import';
  2         4  
  2         1869  
17             our @EXPORT_OK = qw<
18             collage
19             collage_from_definition
20             collage_from_dir
21             collage_from_resolver
22             collage_from_tar
23             >;
24             our %EXPORT_TAGS = (all => [@EXPORT_OK],);
25              
26 6     6 1 544137 sub collage ($input, @args) {
  6         18  
  6         17  
  6         13  
27 6 50       45 my %args = @args == 0 ? (auto => $input) : ($input, @args);
28             return collage_from_resolver($args{resolver})
29 6 100       37 if defined $args{resolver};
30 5 50       20 return collage_from_dir($args{dir}) if defined $args{dir};
31 5 50       21 return collage_from_tar($args{tar}) if defined $args{tar};
32             return collage_from_definition($args{definition})
33 5 50       18 if defined $args{definition};
34 5 50       21 if (defined(my $auto = $args{auto})) {
35 5 50       16 return collage_from_resolver($auto) if blessed($auto);
36              
37 5         11 my $ar = ref($auto);
38 5 50 33     51 if ($ar eq '' && $auto =~ m{\A [\[\{]}mxs) {
39 0         0 $auto = decode_json($auto);
40 0         0 $ar = ref($auto);
41             }
42 5 50 33     82 return collage_from_definition($auto)
      33        
      33        
43             if ($ar eq '' && $ar =~ m{\A \s* [\[\{] }mxs)
44             || ($ar eq 'HASH')
45             || ($ar eq 'ARRAY');
46              
47 5 50       45 croak "cannot handle auto input of type $ar" if $ar ne '';
48 5 50       263 croak "cannot use provided automatic hint" unless -r $auto;
49              
50 5 100       106 return collage_from_dir($auto) if -d $auto;
51              
52 2 50       130 open my $fh, '<:raw', $auto or croak "open('$auto'): $OS_ERROR";
53 2         8 my $first = '';
54 2         224 my $n = read $fh, $first, 1;
55 2 50       12 croak "sysread() on '$auto': $OS_ERROR" unless defined $n;
56 2         35 close $fh;
57 2 50 33     23 return collage_from_definition(file_to_data($auto))
58             if ($first eq '{') || ($first eq '{');
59 2         12 return collage_from_tar($auto);
60             } ## end if (defined(my $auto =...))
61              
62 0         0 croak 'no useful input';
63             } ## end sub collage
64              
65 0     0 1 0 sub collage_from_definition ($def) {
  0         0  
  0         0  
66 0 0       0 $def = decode_json($def) unless ref $def;
67 0 0       0 $def = {commands => $def} if ref($def) eq 'ARRAY';
68 0         0 return PDF::Collage::Template->new($def->%*);
69             }
70              
71 3     3 1 7 sub collage_from_dir ($path) {
  3         8  
  3         5  
72 3         25 return collage_from_resolver(
73             {class => 'Data::Resolver::FromDir', root => $path});
74             }
75              
76 6     6 1 16 sub collage_from_resolver ($resolver) {
  6         13  
  6         10  
77 6         271 return PDF::Collage::TemplatesCollection->new(resolver => $resolver);
78             }
79              
80 2     2 1 4 sub collage_from_tar ($path) {
  2         7  
  2         5  
81 2         19 return collage_from_resolver(
82             {class => 'Data::Resolver::FromTar', root => $path});
83             }
84              
85             1;