File Coverage

blib/lib/XML/LibXSLT/Easy/Batch.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package XML::LibXSLT::Easy::Batch;
4 1     1   6086 use Moose;
  0            
  0            
5              
6             use Carp qw(croak);
7              
8             use XML::LibXSLT::Easy;
9              
10             use File::Glob;
11              
12             use MooseX::Types::Path::Class;
13              
14             use namespace::clean -except => [qw(meta)];
15              
16             has proc => (
17             isa => "XML::LibXSLT::Easy",
18             is => "rw",
19             lazy_build => 1,
20             handles => { "process_file" => "process" },
21             );
22              
23             sub _build_proc {
24             XML::LibXSLT::Easy->new;
25             }
26              
27             has files => (
28             isa => "ArrayRef[HashRef[Str|Path::Class::File]]",
29             is => "ro",
30             );
31              
32             sub process {
33             my ( $self, @files ) = @_;
34              
35             foreach my $entry ( @files ? @files : @{ $self->files || croak "No files to process" } ) {
36             $self->process_entry(%$entry);
37             }
38             }
39              
40             sub process_entry {
41             my ( $self, %args ) = @_;
42              
43             if ( -f $args{xml} and -f $args{xsl} ) {
44             $self->process_file(%args);
45             } elsif ( $args{xml} =~ /\*/ ) {
46             $self->process_file(%$_) for $self->expand(%args);
47             }
48             }
49              
50             sub expand {
51             my ( $self, %args ) = @_;
52              
53             my ( $xml_glob, $xsl_glob, $out_glob ) = @args{qw(xml xsl out)};
54              
55             ( $out_glob = $xml_glob ) =~ s/xml$/html/ unless $out_glob;
56             ( $xsl_glob = $xml_glob ) =~ s/xml$/xsl/ unless $xsl_glob;
57              
58             croak "output cannot be the same as input" unless $out_glob ne $xml_glob;
59             croak "xsl cannot be the same as input" unless $xsl_glob ne $xml_glob;
60              
61             # from Locale::Maketext:Lexicon
62             my $pattern = quotemeta($xml_glob);
63             $pattern =~ s/\\\*(?=[^*]+$)/\([-\\w]+\)/g or croak "bad glob: $xml_glob";
64              
65             # convert glob to regex
66             $pattern =~ s/\\\*/.*?/g; # foo*bar
67             $pattern =~ s/\\\?/./g; # foo?bar
68             $pattern =~ s/\\\[/[/g; # [a-z]
69             $pattern =~ s/\\\]/]/g; # [a-z]
70             $pattern =~ s[\\\{(.*?)\\\\}][ '(?:' . join('|', split(/,/, $1)). ')' ]eg; # {foo,bar}
71              
72             my @ret;
73              
74             foreach my $xml ( File::Glob::bsd_glob($xml_glob) ) {
75             $xml =~ /$pattern/ or next;
76             my $basename = $1;
77              
78             my ( $xsl, $out ) = ( $xsl_glob, $out_glob );
79              
80             s/\*/$basename/e for $xsl, $out;
81              
82             push @ret, { xml => $xml, xsl => $xsl, out => $out };
83             }
84              
85             return @ret;
86             }
87              
88             __PACKAGE__
89              
90             __END__
91              
92             =pod
93              
94             =head1 NAME
95              
96             XML::LibXSLT::Easy::Batch -
97              
98             =head1 SYNOPSIS
99              
100             use XML::LibXSLT::Easy::Batch;
101              
102             =head1 DESCRIPTION
103              
104             =cut
105              
106